创建一个双向链表或双向循环链表
其他
2018-05-29 11:00:06
阅读次数: 1
- #include <stdio.h>
- #include <stdlib.h>
- #define len sizeof(struct list)
- struct list
- {
- int x;
- struct list *pre,*next;
- };
- struct list * create()
- {
- struct list *p,*p1,*head;
- head=p=(struct list *)malloc(len);
- p->pre=NULL;
- scanf("%d",&p->x);
- int n=0;
- while(p->x!=-1)
- {
- if(n==0)
- n++;
- else
- p1->next=p,p->pre=p1;
- p1=p;
- p=(struct list *)malloc(len);
- scanf("%d",&p->x);
- }
- p1->next=NULL;
-
- return head;
- }
- int main()
- {
- struct list *l1;
- printf("请输入链表A,以-1结束:");
- l1=create();
- printf("\n");
- while(l1!=NULL)
- {
- printf("%d ",l1->x);
- l1=l1->next;
- if(l1!=NULL)
- printf("%d ",l1->pre->x);
- }
- printf("\n");
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #define len sizeof(struct list)
- struct list
- {
- int x;
- struct list *pre,*next;
- };
- struct list * create()
- {
- struct list *p,*p1,*head;
- head=p=(struct list *)malloc(len);
- p->pre=NULL;
- scanf("%d",&p->x);
- int n=0;
- while(p->x!=-1)
- {
- if(n==0)
- n++;
- else
- p1->next=p,p->pre=p1;
- p1=p;
- p=(struct list *)malloc(len);
- scanf("%d",&p->x);
- }
- p1->next=NULL;
-
- return head;
- }
- int main()
- {
- struct list *l1;
- printf("请输入链表A,以-1结束:");
- l1=create();
- printf("\n");
- while(l1!=NULL)
- {
- printf("%d ",l1->x);
- l1=l1->next;
- if(l1!=NULL)
- printf("%d ",l1->pre->x);
- }
- printf("\n");
- return 0;
- }
转载自blog.csdn.net/zzwdkxx/article/details/78848533