您的位置:首页 > 其它

我的日记——链表的创建

2015-09-12 10:44 211 查看
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include<malloc.h>
#define ERROR 0
using namespace std;
typedef struct student{
int date;
struct student *next;
}node,*Linklist;
Linklist create()
{
Linklist p1,head,p2;
p1=(Linklist)malloc(sizeof(node));
if(p1==NULL)
return NULL;
int n=0;
cin>>p1->date;
while(p1->date!=0)
{
n+=1;
if(n==1)
{
head=p1;
p2=p1;
p2->next=NULL;
}
else
{
p2->next=p1;
}
p2=p1;
if(n==5)
p1->date=555555;
p1=(Linklist)malloc(sizeof(node));//重新分配指针指向的地址和空间
cin>>p1->date;

}
free(p1);
p2->next=NULL;//释放内存和指针
p1->next=NULL;
return head;
}
void printfstudent(Linklist head)
{
int i=0;
while(head->next!=NULL)
{
i+=1;
printf("%d %d\n",i,head->date);
head=head->next;
}
printf("%d %d\n",i+1,head->date);
}
int n;
int main()
{
Linklist head;
head = create();
printfstudent(head);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: