无头结点链表
可以应用在管理系统里
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int a;
struct Node *pNext;
};
struct Node* pHead=NULL;
struct Node*pEnd=NULL;
void AddListTail(int a)
{
struct Node*pTemp=(struct Node*)malloc(sizeof(struct Node));
pTemp->a=a;
pTemp->pNext=NULL;
if(NULL=pHead||NULL==pEnd)
{
pHead=pTemp;
pEnd=pTemp;
}
else
{
pEnd->pNext=pTemp;
pEnd=pTemp;
}
}
void AddListHead(int a)
{
struct Node*pTemp=(struct Node*)malloc(sizeof(struct Node));
pTemp->a=a;
pTemp->pNext=NULL;
if(NULL=pHead||NULL==pEnd)
{
pHead=pTemp;
pEnd=pTemp;
}
else
{
pTemp->pNext=pHead;
pHead=pTemp;
int main (void)
{
pHead;
AddListTail(5);
AddListHesd(2);
struct Node *pT=SelectNode(int a);
{
if(pT!=NULL)
{
printf("%d\n",pT->a);
}
else
{
printf("没找到\n");
}
}
system("pause");
return 0;
}
void Scanlist()
{
struct Node *pTemp=pHead;
while(pTemp!=NULL)
{
printf("%d\n",pTemp->a);
pTemp=pTemp->pNext;
}
}
struct Node *SelectNode(int a)
{
struct Node *pTemp=pHead;
while(pTemp!=NULL)
{
if(a==pTemp->a)
{
return pTemp;
}
return NULL;
}
}
void FreeList()
{
struct Node *pTemp=pHead;
while(pTemp!=NULL)
{
struct Node *pT=pTemp;
pTemp=pTemp->Next;
free(pT);
}
pHead=NULL;
PEnd=NULL;
}
void AddListRand(int index,int a)
{
if(pHead==NULL||pEnd==NULL)
{
printf("链表没有结点\n");
return 0;
}
struct Node *pT=SelectNode(int index);
if(pT==NULL)
{
printf("没有指定结点\n");
return 0;
}
struct Node*pTemp=(struct Node*)malloc(sizeof(struct Node));
pTemp->a=a;
pTemp->pNext=NULL;
if(pT==pEnd)
{
pEnd->Next=pTemp;
pEnd=pTemp;
}
else
{
pTemp->Next=pT->Next;
pT->Next=pTemp;
}
}
void DeleteListHead()
{
if(pHead==NULL||pEnd==NULL)
{
printf("链表为空\n");
return 0;
}
struct Node *pT=pHead;
pHead=pHead->Next;
}
void DeleteListTail()
{
if(pHead==NULL||pEnd==NULL)
{
printf("链表为空\n");
return 0;
}
if(pHead=pEnd)
{
free(pHead);
pHead==NULL;
pEnd==NULL;
}
else
{
struct Node *pTemp=pHead;
while(pTemp->Next!=pEnd)
{
pTemp=pTemp->Next;
}
free(pEnd);
pEnd=pTemp;
pEnd->Next=NULL;
}
}
void DeleteListRand(int a)
{
if(pHead==NULL||pEnd==NULL)
{
printf("链表为空\n");
return 0;
}
struct Node*pTemp=SelectNode(a);
if(pTemp==NULL)
{
printf("查无此结点\n");
return 0;
}
if(pHead==pEnd)
{
free(pHead);
pHead==NULL;
pEnd==NULL;
}
else if(pHead->Next==pEnd)
{
if(pTemd==pHead)
{
DeleteListHead();
}
else
{
DeleteListTail();
}
}
else
{
if(pTemd==pHead)
{
DeleteListHead();
}
else if(pTemp==pEnd)
{
DeleteListTail();
}
else
{
struct Node *pT=pHead;
while(pT->Next!=pTemp)
{
pT=pT->Next;
}
pT->Next=pTemp->Next;
free(pTemp);
}
}
}