#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct LinkNode
{
DataType data;
struct LinkNode* next;
}LinkNode,*pLinkNode;
typedef struct LinkList
{
pLinkNode pHead;
}LinkList,*pLinkList;
void PrintList(pLinkList pList);//打印函数
void InitLinkList(pLinkList pList);//初始化函数
void PushBack(pLinkList pList,DataType x);//尾插
void PopBack(pLinkList pList);//尾删
void PushFront(pLinkList pList,DataType x);//头插
void PopFront(pLinkList pList);//头删
pLinkNode Create_Node(DataType x);//创建结点
pLinkNode Find_Node(DataType x);//找结点
void Insert(pLinkList pList,pLinkNode pos,DataType x);//指定为插入
void Remove(pLinkList pList,DataType x);//指定数删除
void RemoveAll(pLinkList pList,DataType x);//删除所有
void BubbleSort(pLinkList pList);//排序
void Erase(pLinkList pList,pLinkNode pos);//指定位删除
void Destroy(pLinkList pList);//销毁
#endif //__LINKLIST_H__
void PrintList(pLinkList pList)//打印
{
assert(pList);
pLinkNode cur=NULL;
cur=pList->pHead;
while(cur!=NULL)
{
printf("%d->",cur->data);
cur=cur->next;
}
printf("NULL\n");
}
void InitLinkList(pLinkList pList)//初始化
{
assert(pList);
pList->pHead=NULL;
}
pLinkNode Create_Node(DataType x)//创建新结点
{
pLinkNode newNode=(pLinkNode)malloc(sizeof(pLinkNode));
if(NULL==newNode)
{
printf("out of memory\n");
exit(EXIT_FAILURE);
}
newNode->data=x;
newNode->next=NULL;
return newNode;
}
void Destroy(pLinkList pList)//释放
{
assert(pList);
pLinkNode cur=NULL;
cur=pList->pHead;
while(cur)//?
{
pLinkNode tmp=cur;
cur=cur->next;
free(tmp);
tmp=NULL;
}
pList->pHead=NULL;
}
void PushBack(pLinkList pList,DataType x)//尾插
{
assert(pList);
pLinkNode cur=NULL;
pLinkNode newNode=NULL;
cur=pList->pHead;
newNode=Create_Node(x);
if(cur=NULL)
{
pList->pHead=newNode;
}
else
{
while(cur->next)//?
{
cur=cur->next;
}
cur->next=newNode;
}
}
void PopBack(pLinkList pList)//尾删
{
assert(pList);
pLinkNode cur=NULL;
pLinkNode tmp=NULL;
cur=pList->pHead;
if(pList->pHead==NULL)
{
return ;
}
else if(pList->pHead->next==NULL)
{
free(pList->pHead);
pList->pHead=NULL;
}
else
{
while(cur->next)//?
{
tmp=cur;
cur=cur->next;
}
free(cur);
cur=NULL;
tmp->next=NULL;
}
}
void PushFront(pLinkList pList,DataType x)//头插
{
assert(pList);
pLinkNode newNode=NULL;
newNode=Create_Node(x);
newNode=pList->pHead;
pList->pHead=newNode;
}
void PopFront(pLinkList pList)//头删
{
pLinkNode tmp=NULL;
if(pList->pHead==NULL)
{
return;
}
else
{
tmp=pList->pHead;
pList->pHead=tmp->next;
free(tmp);
tmp=NULL;
}
}
void Insert(pLinkList pList,pLinkNode pos,DataType x)//指定位后插入元素
{
assert(pList);
pLinkNode newNode=Create_Node(x);
pLinkNode cur=pList->pHead;
if(pList->pHead->next==NULL)
{
PushFront(pList,x);
return;
}
else if(pos==NULL)
{
printf("no position\n");
}
else
{
while(cur!=pos)
{
cur=cur->next;
}
newNode->next=pos->next;
pos->next->newNode;
}
}
pLinkNode Find_Node(pLinkList pList,DataType x)//找结点
{
assert(pList);
pLinkNode cur=pLinkList->pHead;
while(cur)
{
if(cur->data==x)
{
return cur;
}
cur=cur->next;
}
return NULL;
}
void Remove(pLinkList pList,DataType x)//指定数删除
{
assert(pList);
pLinkNode cur=pList->pHead;
pLinkNode ret=NULL;
if(pList->pHead==NULL)
{
return ;
}
else
{
ret=Find_Node(pList,x);
while(cur->next!=ret)
{
cur=cur->next;
}
cur->next=ret->next;
free(ret);
ret=NULL;
}
}
void RemoveAll(pLinkList pList,DataType x)//删除所有指定数
{
pLinkNode ret=NULL;
pLinkNode cur=plist->phead;
assert(plist);
if(NULL == plist->phead)
{
return ;
}
else
{
while(cur->next != NULL)
{
ret=Find_NUM(plist,x);
while(cur->next != ret)
{
cur=cur++;
}
cur->next=ret->next;
free(ret);
ret=NULL;
cur=cur->next;
}
}
}
void BubbleSort(pLinkList pList)//排序
{
pLinkNode cur=plist->pHead;
pLinkNode cmp=plist->pHead;
DataType flag=0;
while(cur->next != NULL)
{
flag=0;
while(cmp->next != NULL)
{
if(cmp->data > cmp->next->data) //升序排列
{
DataType tmp=cmp->data;
cmp->data=cmp->next->data;
cmp->next->data=tmp;
flag=1;
}
cmp=cmp->next;
}
cur=cur->next;
if(flag == 0)
break;
}
}
void Erase(pLinkList pList,pLinkNode pos)//指定位删除
{
pLinkNode cur=plist->pHead;
assert(plist);
while(cur->next != pos)
{
cur=cur->next;
}
cur->next=pos->next;
free(pos);
pos=NULL;
}
顺序表的链式储存
最新推荐文章于 2023-09-13 23:04:19 发布