#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LNode* init_list();
int Insert_Elem(LNode * LL,unsigned int ii,ElemType* ee);
void printlinklist(LNode * LL);
int LengthList(LinkList LL);
int IsEmpty(LinkList LL);
LNode *LocateNode(LinkList LL, unsigned int ii);
LNode *LocateElem(LinkList LL, ElemType *ee);
int InsertNextNode(LNode *pp, ElemType *ee);
int InsertPriorNode(LNode *pp, ElemType *ee);
void main()
{
int i=0;
ElemType ee;
LinkList LL=init_list();
LNode* pp=LL->next;
printf("判断链表是否为空\n");
printf("%d\n",IsEmpty(LL));
printf("LL=%p\n",LL);
printf("在表中插入元素1,2,3,4,5,6,7,8,9\n");
for(i=1;i<10;i++)
{
ee=i;
Insert_Elem(LL,i,&ee);
}
printf("打印链表的全部数值\n");
printlinklist(LL);
pp=pp->next;
pp=pp->next;
ee=15;InsertPriorNode(pp,&ee);
printf("打印链表的全部数值\n");
printlinklist(LL);
printf("判断链表是否为空\n");
printf("%d\n",IsEmpty(LL));
printf("打印链表的全部数值\n");
printlinklist(LL);
printf("计算链表的长度\n");
printf("链表的长度为 %d \n",LengthList(LL));
pp=LocateNode(LL,5);
printf("第5个元素的数值为%d\n",pp->next);
ee=0;LocateElem(LL,&ee);
ee=0;InsertNextNode(LL,&ee);
LocateElem(NULL,&ee);
ee=0;LocateElem(LL,&ee);
ee=1;LocateElem(LL,&ee);
ee=2;LocateElem(LL,&ee);
ee=3;LocateElem(LL,&ee);
ee=4;LocateElem(LL,&ee);
ee=5;LocateElem(LL,&ee);
ee=6;LocateElem(LL,&ee);
ee=7;LocateElem(LL,&ee);
ee=8;LocateElem(LL,&ee);
ee=9;LocateElem(LL,&ee);
ee=10;LocateElem(LL,&ee);
printf("打印链表的全部数值\n");
printlinklist(LL);
pp=pp->next;
ee=15;InsertPriorNode(pp,&ee);
printf("打印链表的全部数值\n");
printlinklist(LL);
system("pause");
}
LNode* init_list()
{
LNode* head=(LNode*)malloc(sizeof(LNode));
if(head==NULL)
{
printf("内存空间不足");
return NULL;
}
head->next=head;
return head;
}
int Insert_Elem(LinkList LL,unsigned int ii,ElemType* ee)
{
unsigned int kk=1;
LNode* pp=NULL;
LNode* tem=(LNode*)malloc(sizeof(LNode));
if(LL==NULL || ee==NULL){printf("链表或者元素为空");return 0;}
if(ii<1) {printf("插入位置不合法,应该ii>=1");return 0;}
if(ii==1)
{
pp=LL;
}
else
{
pp=LL->next;
while(pp!=LL && kk<ii-1)
{
pp=pp->next;
kk++;
}
if(pp==LL){printf("插入位置超过表长");return 0;}
}
if(tem==NULL){printf("内存空间不足");return 0;}
tem->data=*ee;
tem->next=pp->next;
pp->next=tem;
return 1;
}
void printlinklist(LNode * LL)
{
LNode* pp=LL;
pp=pp->next;
while(pp!=LL) {printf(" %d ",pp->data); pp=pp->next;}
}
void DestroyList1(LinkList LL)
{
LNode *head=LL;
LNode *tem=NULL;
LL=LL->next;
while(tem!=LL)
{
tem=LL->next;
free(tem);
LL=tem;
}
free(head);
return;
}
int LengthList(LinkList LL)
{
LNode* pp=LL;
int length=0;
pp=pp->next;
while(pp!=LL){length++; pp=pp->next;}
return length;
}
int IsEmpty(LinkList LL)
{
if(LL==NULL) return 0;
if(LL->next==LL) return 1;
return 0;
}
LNode *LocateNode(LinkList LL, unsigned int ii)
{
unsigned int i=0;
LNode* pp=LL;
if(LL==NULL) {printf("链表为空");return NULL;}
for(i=0;i<ii;i++)
{
pp=pp->next;
if(pp==LL){printf("位置超过表长"); return NULL;}
}
printf("从头结点开始算起,第%d个节点地址为%p\n",ii,pp);
return pp;
}
LNode *LocateElem(LinkList LL, ElemType *ee)
{
LNode* pp=LL;
if(LL==NULL){printf("链表为空\n"); return NULL;}
pp=pp->next;
while(pp!=LL)
{
if(pp->data==*ee){printf("找到元素地址 %p \n",pp); return pp;}
pp=pp->next;
}
printf("没有找到链表之中对应的元素\n");
return NULL;
}
int InsertNextNode(LNode *pp, ElemType *ee)
{
LNode * newlnode=(LNode*)malloc(sizeof(struct LNode));
if(pp==NULL){printf("节点为空无法插入\n"); return 0;}
newlnode->data=*ee;
newlnode->next=pp->next;
pp->next=newlnode;
printf("节点插入成功\n");
printf("节点地址为 %p \n",newlnode);
return 1;
}
int InsertPriorNode(LNode *pp, ElemType *ee)
{
LNode * tem=(LNode*)malloc(sizeof(struct LNode));
if(pp==NULL){printf("节点为空无法插入\n"); return 0;}
if(tem==NULL){printf("新的节点创建失败\n"); return 0;}
memcpy(&tem->data,&pp->data,sizeof(ElemType));
tem->next=pp->next;
pp->data=*ee;
pp->next=tem;
printf("前向节点插入成功\n");
return 1;
}