赞
踩
- #include<assert.h>
- typedef struct SLnode
- {
- int data;
- struct SLnode* prior;
- struct SLnode* next;
- }SLnode,*SLnodelist;
- //创建结点
- SLnode* createhead(int data)
- {
- SLnode* newnode = (SLnode*)malloc(sizeof(SLnode));
- newnode->data = data;
- newnode->next = newnode->prior = newnode;
- return newnode;
- }
- //初始化哨兵位
- SLnode* ListInit()
- {
- SLnode* newnode = createhead(0);
- }
- //尾插
- void ListPushBank(SLnode* plist, int x)
- {
- assert(plist);
- SLnode* ret = plist->prior;
- SLnode* newnode = createhead(x);
-
- ret->next = newnode;
- newnode->next = plist;
- newnode->prior = ret;
- plist->prior = newnode;
- }
- void printlist(SLnode* plist)
- {
- SLnode* ret = plist->next;
- while (ret != plist)
- {
- printf("%d->", ret->data);
- ret = ret->next;
- }
- printf("NULL\n");
- }
- void change(SLnodelist p)
- {
- SLnodelist q;
- q = p->prior;
-
- q->prior->next = p;
- p->prior = q->prior;
-
- q->next = p->next;
- p->next->prior = q;
-
- q->prior = p;
- p->next = q;
-
- }
- int main()
- {
- int n, num;
- SLnode* plist = ListInit();
- printf("请输入链表的长度\n");
- scanf("%d", &n);
- printf("请输入元素\n");
- for (int i = 0; i < n; i++)
- {
- scanf("%d", &num);
- ListPushBank(plist,num);
- }
- printf("双链表的元素如下\n");
- printlist(plist);
- printf("请输入你要交换的的结点的值\n");
- SLnodelist q = (SLnodelist)malloc(sizeof(SLnode));
- scanf("%d", &q->data);
- SLnodelist p;
- p = plist->next;
- while (p != plist)
- {
- if (p->data == q->data)
- {
- change(p);
- break;
- }
- else
- {
- p = p->next;
- }
- }
- printf("交换后\n");
- printlist(plist);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。