#include <stdio.h>
#include <stdlib.h>
typedef struct CLNode{
int data;
struct CLNode *next;
}CLNode,*LinkList;
void init(LinkList &L)
{
L = (CLNode *)malloc(sizeof(CLNode));
L->next = L;
}
void InsertNextNode(LinkList &L,int e)
{
CLNode *s = (CLNode *)malloc(sizeof(CLNode));
s->data = e;
s->next = L->next;
L->next = s;
}
void TailInsert(LinkList &L)
{
int e,n;
printf("输入要输入元素的个数:\n");
scanf("%d",&n);
printf("输入元素的值:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&e);
CLNode *p = (CLNode *)malloc(sizeof(CLNode));
p = L;
while(p->next != L)
{
if(L->next == L)
{
CLNode *p = (CLNode *)malloc(sizeof(CLNode));
L->next = p;
p->next = L;
p->data = e;
}
p = p->next;
}
InsertNextNode(p,e);
}
}
void InsertPriorNode(LinkList &L,int e)
{
CLNode *s = (CLNode *)malloc(sizeof(CLNode));
s->data = L->data;
s->next = L->next;
L->next = s;
L->data = e;
}
void HeadInsert(LinkList &L)
{
int n,e;
printf("输入要输入元素的个数:\n");
scanf("%d",&n);
printf("输入元素的值:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&e);
if(L->next == L)
{
CLNode *p = (CLNode *)malloc(sizeof(CLNode));
L->next = p;
p->next = L;
p->data = e;
}
else
{
InsertPriorNode(L->next,e);
}
}
}
void show(LinkList &L)
{
CLNode *p = L;
while(p->next != L)
{
p = p->next;
printf("%d ",p->data);
}
printf("\n");
}
int main(){
CLNode *L,*K;
init(L);
printf("采用尾插法:\n");
TailInsert(L);
show(L);
init(K);
printf("采用头插法:\n");
HeadInsert(K);
show(K);
return 0;
}