|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
编写程序,实现链表的创建和遍历。输入a,b,c,d,e,f六个字符作为结点的数据,创建具有六个数据元素的链表,然后实现输出链表结点中的数据元素值。要求有交互界面,输入1为创建链表,输入0为退出程序,输入2为显示链表中的数据元素。 不会写这个,希望有大神能帮忙解决
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #define LEN sizeof(struct student)
- struct student *creat();//创建链表
- void print(struct student *head);//打印链表
- struct student
- {
- char num;
- struct student *next;
- };
- int n;//用来记录存放了多少数据
- int main()
- {
- int i,j;
- struct student *stu,*p;
-
- while(1)
- {
- printf("请输入数字1创建链表:");
- scanf("%d",&i);
- if(i==1)
- break;
- }
- while(i==1)
- {
- printf("请输入内容:(每个内容已回车隔开): \n");
- stu=creat();
- break;
- }
- while(1)
- {
- printf("请输入数字2显示链表:");
- scanf("%d",&j);
- if(j==2)
- break;
- }
- while(j==2)
- {
- p=stu;
- print(p);
- break;
- }
-
-
- printf("\n\n");
- system("pause");
- }
- struct student *creat()
- {
- struct student *head;
- struct student *p1,*p2;
- p1=p2=(struct student *)malloc(LEN);
- p1->num=getchar();
- head=NULL;
- n=0;
- while(p1->num!='0')
- {
- n++;
- if(n==1)
- {
- head=p1;
- }
- else
- {
- p2->next=p1;
- }
- p2=p1;
- p1=(struct student *)malloc(LEN);
- p1->num=getchar();
- }
- p2->next=NULL;
- return head;
- }
- void print(struct student *head)
- {
- struct student *p;
- p=head;
- if(head!=NULL)
- {
- do
- {
- printf("%3c",p->num);
- p=p->next;
- }while(p);
- }
- }
复制代码
|
|