#include<cstdio>
#include<cstdlib>
#include <cstring>
using namespace std;
struct node
{
int num;
char name[10];
struct node *next;
};
struct node *createlink(int n)
{
int i;
struct node *head,*p,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=head;
q=head;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->num);
scanf("%s",p->name);
p->next=q->next;
q->next=p;
q=p;
}
q->next=head;
return head;
}
void disp(struct node *head)
{
if(head==NULL)
return;
struct node *p=NULL;
p=head->next;
while(p!=head)
{
printf("%d\n",p->num);
printf("%s\n",p->name);
p=p->next;
}
printf("\n\n");
}
int main()
{
struct node *head;
//struct node *data;
//scanf("%d",data->num);
//scanf("%s",data->name);
head=createlink(3);
//insertdata(head,data);
//sort(head);
disp(head);
return 0;
}
#include<cstdlib>
#include <cstring>
using namespace std;
struct node
{
int num;
char name[10];
struct node *next;
};
struct node *createlink(int n)
{
int i;
struct node *head,*p,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=head;
q=head;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->num);
scanf("%s",p->name);
p->next=q->next;
q->next=p;
q=p;
}
q->next=head;
return head;
}
void disp(struct node *head)
{
if(head==NULL)
return;
struct node *p=NULL;
p=head->next;
while(p!=head)
{
printf("%d\n",p->num);
printf("%s\n",p->name);
p=p->next;
}
printf("\n\n");
}
int main()
{
struct node *head;
//struct node *data;
//scanf("%d",data->num);
//scanf("%s",data->name);
head=createlink(3);
//insertdata(head,data);
//sort(head);
disp(head);
return 0;
}
本文介绍了一个使用C语言实现链表的过程,包括链表节点的结构定义、链表的创建及链表中元素的显示。通过具体示例展示了如何分配内存、输入数据并连接节点形成链表,最后遍历链表打印每个节点的内容。
2876

被折叠的 条评论
为什么被折叠?



