#include
#include
#include
typedef struct list
{
int num;
struct list *next;
}List;
int n=0;
List *creat()
{
List *head,*p1,*p2;
int i;
if((head=(List *)malloc(sizeof(List)))==NULL)
{
printf("Error");
exit(0);
}
p1=p2=head;
printf("输入创建链表的长度:");
scanf("%d",&head->num);//创建列表,带头结点,头结点数据域表示输入的个数
if(head->num==0)
{
head->next=NULL;
printf("已创建带头结点的空链表");
}
else
{
printf("输入数据:\n");
for(i=0;i
{
if((p1=(List *)malloc(sizeof(List)))==NULL)
{
printf("Error");
exit(0);
}
scanf("%d",&p1->num);
p2->next=p1;
p2=p1;
}
p1->next=head;
}
return(head);
}
void print(List *head)
{
List *p;
p=head->next;
for(;p!=head;)
{
printf("%d ",p->num);
p=p->next;
}
printf("\n");
}
void main()
{
List *head;
head=creat();
print(head);
}
/*
* 双向循环链表
*/
#include
#include
#define MAXSIZE 10
typedef int Elemtype;
typedef struct LNode {
Elemtype data;
struct LNode *pre;
struct LNode *next;
}LNode, *DoubleLinkList;
void insert(DoubleLinkList *tail, Elemtype data) {
DoubleLinkList head = (*tail)->next;
(*tail)->next = (DoubleLinkList)malloc(sizeof(LNode));
(*tail)->next->data = data;
(*tail)->next->pre = (*tail);
(*tail)->next->next = head;
(*tail) = (*tail)->next;
head->pre = (*tail);
}
void main() {
DoubleLinkList head, tail;
head = (DoubleLinkList)malloc(sizeof(LNode));
head->data = 0;
head->pre = head;
head->next = head;
tail = head;
insert(&tail, 1);
insert(&tail, 2);
insert(&tail, 3);
insert(&tail, 4);
insert(&tail, 5);
DoubleLinkList pr = head;
while (1) {
printf("%d\n", pr->data);
if (pr->next == head) {
break;
}
pr = pr->next;
}
return;
}