?? 雙向循環(huán).c
字號:
#include<stdio.h>
#include<malloc.h>
typedef struct node
{int data;
struct node *next,*prior;
}Node,*lklist,*pointer;
lklist setup()
{lklist head;
pointer p,q;
int x;
head=(lklist)malloc(sizeof(Node));
p=head;
scanf("%d",&x);
while(x!=32767)
{q=(lklist)malloc(sizeof(Node));
q->data=x;
p->next=q;
p->prior=0;
p=q;
scanf("%d",&x);
}
p->next=0;
return head;
}
void display(lklist head)
{pointer p;
p=head->next;
while(p)
{printf("%5d",p->data);
p=p->next;
}
}
lklist merg(lklist head) //將單鏈表變成雙向循環(huán)鏈表
{pointer p,q,s;
p=head;
while(p->next)
p=p->next;
p->next=head->next;
free(head);
s=p;
q=p->next;
while(q!=p)
{q->prior=s;
s=q;
q=q->next;
}
q->prior=s;
return p;
}
void display1(lklist p) //用NEXT來遍歷
{pointer q;
q=p->next;
while(q!=p)
{printf(" %d",q->data);
q=q->next;
}
printf(" %d",q->data);
}
void display2(lklist q)//用PRIOR來遍歷
{pointer p;
p=q->next;
while(q!=p)
{printf(" %d",q->data);
q=q->prior;
}
printf(" %d",p->data);
}
void main()
{lklist head;
head=setup();
display(head);
printf("\n\n");
head=merg(head); //調(diào)用單鏈表變雙向循環(huán)鏈表函數(shù)
display1(head);
printf("\n\n");
display2(head);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -