您的位置:首页 > 其它

寒假集训第二天——线性表

2014-01-15 01:34 309 查看
现在时间是北京时间1点23分,第二天集训。。。

昨天花了老长时间把线性表看了下,表示很有压力,不大会用。。。

先说下我学到的线性表的皮毛。。。

首先是链表的构建,构建有两种方式:

顺序链表(尾插法建单链表)

#include<stdio.h>
struct node{
int date;
struct node *next;
};
int main()
{
int i,n;
node *head=new node;
head->next=NULL;
node *tail=head;
scanf("%d",&n);
for(i=0;i<n;i++)
{
node *p=new node;
scanf("%d",&p->date);//对p构建
p->next=NULL;
tail->next=p;
tail=p;
}
for(node *p=head->next;p!=NULL;p=p->next)
printf("%d ",p->date);
}

尾插法的另一种实现方法

#include<stdio.h>
struct node
{
int date;
struct node *next;
};
int main()
{
int n,i;
node *head=NULL;
node *tail;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
node *p=new node;
if(head==NULL)
head=p;
else
tail->next=p;
p->date=i;//对p构建
p->next=NULL;
tail=p;
}
node *p;
p=head;
while(p!=NULL)
{
printf("%d ",p->date);
p=p->next;
}
}


逆序链表(头插法建单链表)

#include<stdio.h>
struct node{
int date;
struct node *next;
};
int main()
{
int i,n;
node *head=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
node *p=new node;
scanf("%d",&p->date);
p->next=head;
head=p;
}
for(node *p=head;p!=NULL;p=p->next)
printf("%d ",p->date);
}


今天可是很艰难的一天,也是很有激情的一天,链表不大会呀,想想明天还得栈与队列。。。

学了好多东西。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线性表 单链表