您的位置:首页 > 理论基础 > 数据结构算法

【数据结构】 链表的19种基本操作(完整版)

2014-04-03 17:11 676 查看
基于这篇文章: http://www.cnblogs.com/lifuqing/archive/2011/08/20/List.html
作者最后只给出了前面12中操作的代码,这里我帮他补全

链表快速排序的思想参考了: http://blog.csdn.net/pinkrobin/article/details/5456094
转载本文前请注明出处,谢谢~

提供一个本文代码的下载地址:
http://download.csdn.net/detail/fanxingzju/7142179
提供一个测试样例



/*
=======================================================================================================
以下是关于线性表链接存储(单链表)操作的19种算法
By~fanxingzju			2014-04-03
1.初始化线性表,即置单链表的表头指针为空
2.创建线性表,此函数输入负数终止读取数据
3.打印链表,链表的遍历
4.清除线性表L中的所有元素,即释放单链表L中所有的结点,使之成为一个空表
5.返回单链表的长度
6.检查单链表是否为空,若为空则返回1,否则返回0
7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行
8.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL
9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0
10.向单链表的表头插入一个元素
11.向单链表的末尾添加一个元素
12.向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0
13.向有序单链表中插入元素x结点,使得插入后仍然有序
14.从单链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行
15.从单链表中删除表尾结点并返回它的值,若删除失败则停止程序运行
16.从单链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行
17.从单链表中删除值为x的第一个结点,若删除成功则返回1,否则返回0
18.交换2个元素的位置
19.将线性表进行快速排序
=========================================================================================================
*/
//基本操作参考了: http://www.cnblogs.com/lifuqing/archive/2011/08/20/List.html //链表的快速排序参考了: http://blog.csdn.net/pinkrobin/article/details/5456094 
#include <stdlib.h>
#include <stdio.h>

typedef int ElemType;

typedef struct Node
{
ElemType elem;
Node *next;
}Node;

//1.初始化线性表,即置单链表的表头指针为空
void InitList(Node **pNode)
{
*pNode = NULL;
printf("InitList函数执行,初始化成功\n");
}

//2.创建线性表,此函数输入负数终止读取数据
Node *CreastList(Node *pHead)
{
Node *p1;
Node *p2;
p1 = p2 = new Node;
if (NULL == p1 || NULL == p2)
{
printf("内存分配失败,程序终止\n");
system("pause");
exit(0);
}
//memset(p1,0,sizeof(Node));
scanf("%d",&p1->elem);
p1->next = NULL;
while (p1->elem > 0)
{
if (pHead == NULL)
{
pHead = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = new Node;
if (NULL == p1 || NULL == p2)
{
printf("内存分配失败,程序终止\n");
system("pause");
exit(0);
}
//memset(p1,0,sizeof(Node));
scanf("%d",&p1->elem);
p1->next = NULL;
}
printf("CreatList函数执行,列表创建成功\n");
return pHead;
}

//3.打印链表,链表的遍历
void PrintList(Node *pHead)
{
if (NULL == pHead)
{
printf("PrintList函数执行,链表为空\n");
}
else
{
while (NULL != pHead)
{
printf("%d ",pHead->elem);
pHead = pHead->next;
}
printf("\n");
}
}

//4.清除线性表L中的所有元素,即释放单链表L中所有的结点,使之成为一个空表
void ClearList(Node *pHead)
{
Node *pNext;
if (NULL == pHead)
{
printf("ClearList函数执行,链表为空\n");
}
else
{
while (pHead->next != NULL)
{
pNext = pHead->next;
delete pHead;
pHead = pNext;
}
printf("ClearList函数执行,链表已经清除\n");
}
}

//5.返回单链表的长度
int SizeList(Node *pHead)
{
int size = 0;
while (pHead != NULL)
{
++size;
pHead = pHead->next;
}
printf("SizeList函数执行,链表长度 %d \n",size);
return size;
}

//6.检查单链表是否为空,若为空则返回1,否则返回0
int IsEmptyList(Node *pHead)
{
if (NULL == pHead)
{
printf("IsEmptyList函数执行,链表为空\n");
return 1;
}
else
{
printf("IsEmptyList函数执行,链表非空\n");
return 0;
}
}

//7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行
ElemType GetElement(Node *pHead, int pos)
{
int i = 0;
if (pos < 1)
{
printf("GetElement函数执行,pos值 %d 非法\n", pos);
return 0;
}
if (NULL == pHead)
{
printf("GetElement函数执行,链表为空\n");
return 0;
}

while(pHead != NULL)
{
++i;
if (i == pos)
{
break;
}
pHead = pHead->next;
}
if (i < pos)
{
printf("GetElement函数执行,pos值 %d 超出链表长度\n", pos);
return 0;
}

return pHead->elem;
}

// 8.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL
ElemType *GetElemAddr(Node *pHead, ElemType x)
{
if (NULL == pHead)
{
printf("GetElemAddr函数执行,链表为空\n");
return NULL;
}
if (x < 0)
{
printf("GetElemAddr函数执行,给定值 %d 不合法\n", x);
return NULL;
}
while((pHead->elem != x)&&(NULL != pHead->next))
{
pHead = pHead->next;
}
if ((pHead->elem != x)&&(pHead != NULL))
{
printf("GetElemAddr函数执行,在链表中未找到 %d 的值\n", x);
return NULL;
}
if (pHead->elem == x)
{
printf("GetElemAddr函数执行,值为 %d 元素的地址为 0x%x\n", x, &(pHead->elem));
}
return &(pHead->elem);
}

//9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0
int ModifyElem(Node *pNode, int pos, ElemType x)
{
Node *pHead;
pHead = pNode;
int i = 0;

if (NULL == pHead)
{
printf("ModifyElem函数执行,链表为空\n");
}
if (pos < 1)
{
printf("ModifyElem函数执行,第 %d 个节点位置非法\n", pos);
}
while(NULL != pHead)
{
++i;
if (i == pos)
{
break;
}
pHead = pHead->next;
}
if (i < pos)
{
printf("ModifyElem函数执行,第 %d 个节点位置超出链表长度\n", pos);
return 0;
}
pNode = pHead;			//why
pNode->elem = x;
printf("ModifyElem函数执行,第 %d 个节点的值修改为 %d 成功\n", pos, x);
return 1;
}

//10.向单链表的表头插入一个元素
int InsertHeadList(Node **pNode, ElemType insertElem)
{
Node *pInsert;
pInsert = new Node;
//memset(pInsert,0,sizeof(Node));
pInsert->elem = insertElem;
pInsert->next = *pNode;
*pNode = pInsert;
printf("InsertHeadList函数执行,向表头插入元素成功\n");
return 1;
}

//11.向单链表的末尾添加一个元素
int InsertLastList(Node **pNode, ElemType insertElem)
{
Node *pInsert;
Node *pHead;
Node *pTemp;
pHead = *pNode;
pTemp = pHead;
pInsert = new Node;
//memset(pInsert,0,sizeof(Node));			//保证最后一个元素的next指向NULL
pInsert->elem = insertElem;
pInsert->next = NULL;

while(pHead->next != NULL)
{
pHead = pHead->next;
}
pHead->next = pInsert;
*pNode = pTemp;
printf("InsertLastList函数执行,向表尾插入元素成功\n");
return 1;
}

//12.向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0
int InsertPosList(Node **pNode, int pos, ElemType insertElem)
{
if (pos < 1)
{
printf("InsertPosElem函数执行,pos值 %d 非法\n", pos);
return 0;
}
Node *pInsert;
Node *pHead;
int i = 1;			//to be modified

pHead = *pNode;
pInsert = new Node;
pInsert->elem = insertElem;

while (pHead != NULL)
{
++i;
if (i == pos)
{
break;
}
pHead = pHead->next;
}
if (pos > i)
{
printf("InsertPosElem函数执行,pos值 %d 超出链表长度\n", pos);
return 0;
}
pInsert->next = pHead->next;
pHead->next = pInsert;
printf("InsertPosElem函数执行,向第 %d 个节点位置插入元素成功\n", pos);
return 1;
}

//13.向有序单链表中插入元素x结点,使得插入后仍然有序
void InsertOrder(Node **pNode, ElemType x)
{
Node *pFir = *pNode;
Node *pInsert = new Node;
pInsert->elem = x;
pInsert->next = NULL;
if (NULL == pFir)
{
*pNode = pInsert;
}
else
{
if (pFir->elem > x)
{
pInsert->next = pFir;
*pNode = pInsert;
}
else
{
Node *pSec;
while(NULL != pFir)
{
pSec = pFir->next;
if ((NULL == pSec)||(pSec->elem >= x))			// 短路,不能颠倒条件顺序
{
pFir->next = pInsert;
pInsert->next = pSec;
break;
}
pFir = pSec;
}
}
}
printf("InsertOrder函数执行,向有序链表插入成功\n");
}

//14.从单链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行
ElemType DeleteHeadList(Node **pNode)
{
if (NULL == *pNode)
{
printf("DeleteHeadList函数执行,链表为空,头结点删除失败\n");
system("pause");
exit(0);
}
Node *pTemp = *pNode;
ElemType elemvalue = pTemp->elem;
*pNode = pTemp->next;
delete pTemp;

printf("DeleteHeadList函数执行,头结点删除成功\n");
return elemvalue;
}

//15.从单链表中删除表尾结点并返回它的值,若删除失败则停止程序运行
ElemType DeleteLastList(Node **pNode)
{
Node *pHead;
Node *pTemp;
ElemType elemvalue;
pHead = *pNode;
if (NULL == pHead)
{
printf("DeleteHeadList函数执行,链表为空,表尾结点删除失败\n");
system("pause");
exit(0);
}
pTemp = pHead;
while(pHead->next != NULL)
{
pTemp = pHead;
pHead = pHead->next;
}

elemvalue = pTemp->elem;
pTemp->next = NULL;
delete pHead;
printf("DeleteLastList函数执行,表尾结点删除成功\n");

return elemvalue;
}

//16.从单链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行
ElemType DeletePosList(Node **pNode, int pos)
{
Node *pHead = *pNode;
ElemType elemvalue;
if (NULL == pHead)
{
printf("DeletePosList函数执行,链表为空,删除失败\n");
system("pause");
exit(0);
}
if (1 == pos)
{
elemvalue = pHead->elem;
*pNode = pHead->next;
delete pHead;
printf("DeletePosList函数执行,删除第 %d 个节点成功\n", pos);
return elemvalue;
}
Node *pTemp = pHead;
int i = 1;
while(pHead->next != NULL)
{
pTemp = pHead;
pHead = pHead->next;
++i;
if (i == pos)
{
break;
}
}
if (pos != i)
{
printf("DeletePosList函数执行,找不到第 %d 个节点位置,删除失败\n", pos);
system("pause");
exit(0);
}

elemvalue = pHead->elem;
pTemp->next = pHead->next;
delete pHead;

printf("DeletePosList函数执行,删除第 %d 个节点成功\n", pos);

return elemvalue;
}

//17.从单链表中删除值为x的第一个结点,若删除成功则返回1,否则返回0
int DeleteValuexList(Node **pNode, ElemType x)
{
Node *pHead = *pNode;

if (NULL == pHead)
{
printf("DeleteValuexList函数执行,链表为空,删除失败\n");
return 0;
}

Node *pTemp = pHead;

while((NULL != pHead->next)&&(pHead->elem != x))
{
pTemp = pHead;
pHead = pHead->next;
}
if (pHead->elem != x)
{
printf("DeleteValuexList函数执行,未找到相应节点,删除失败\n");
return 0;
}

if (pTemp->elem != pHead->elem)
{
pTemp->next = pHead->next;
delete pHead;
}
else
{
//二者依然相等,说明第一个节点即为删除的目标位置
*pNode = pHead->next;
delete pHead;
}

printf("DeleteValuexList函数执行,链表中第一个值为 %d 的节点删除成功\n", x);
return 1;
}

//18.交换2个元素的位置
void SwapList(Node **pNode, ElemType elem1, ElemType elem2)
{
Node *pHead = *pNode;
Node *pTemp1 = NULL;
Node *pTemp2 = NULL;
while(NULL != pHead)
{
if (pHead->elem == elem1)
{
pTemp1 = pHead;
}
if (pHead->elem == elem2)
{
pTemp2 = pHead;
}
pHead = pHead->next;
}
if ((pTemp1 != NULL)&&(pTemp2 != NULL))
{
ElemType Temp = pTemp1->elem;
pTemp1->elem = pTemp2->elem;
pTemp2->elem = Temp;
printf("SwapList函数执行,交换元素(%d 和 %d)的位置成功\n", elem1, elem2);
}
else
{
printf("SwapList函数执行,在链表中找不到相应的部分或全部元素(%d 和 %d)\n", elem1, elem2);
}
}

//19.将线性表进行快速排序
void QuickSortList(Node **pHead, Node *pEnd)
{
Node *pRight;
Node **pLeft_walk, **pRight_walk;
Node *pPivot, *pOld;
if (*pHead == pEnd)			//pEnd可以是NULL,但不一定是NULL,此处pEnd不可用NULL代替
{
return ;
}
pPivot = *pHead;
pLeft_walk = pHead;
pRight_walk = &pRight;
//取第一个节点作为比较的基准,小于基准的在左边的子链表中,大于基准的在右边的子链表中
for(pOld = (*pHead)->next; pOld != pEnd; pOld = pOld->next)
{
if (pOld->elem < pPivot->elem)
{
*pLeft_walk = pOld;
pLeft_walk = &(pOld->next);
}
else
{
*pRight_walk = pOld;
pRight_walk = &(pOld->next);
}
}
//合并链表
*pRight_walk = pEnd;
*pLeft_walk = pPivot;
pPivot->next = pRight;
//分治、递归
QuickSortList(&(pPivot->next), pEnd);
QuickSortList(pHead, pPivot);

//下面这段代码纯粹是为了保证函数风格,与排序无关
static bool flag = true;
if (flag)
{
printf("QuickSortList函数执行,链表排序成功\n");
flag = false;
}
}
int main()
{
Node *pList = NULL;

// 1.初始化线性表,即置单链表的表头指针为空
InitList(&pList);
PrintList(pList);

// 2.创建线性表,此函数输入负数终止读取数据
pList = CreastList(pList);
PrintList(pList);

// 5.返回单链表的长度
int length = SizeList(pList);
PrintList(pList);

// 6.检查单链表是否为空,若为空则返回1,否则返回0
IsEmptyList(pList);

// 7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行
ElemType posElem;
posElem = GetElement(pList, 3);
printf("GetElement函数执行,位置3中的元素为%d\n",posElem);
PrintList(pList);

// 8.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL
GetElemAddr(pList, 5);

// 9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0
ModifyElem(pList, 4, 10);
PrintList(pList);

// 10.向单链表的表头插入一个元素
InsertHeadList(&pList, 5);
PrintList(pList);

// 11.向单链表的末尾添加一个元素
InsertLastList(&pList, 10);
PrintList(pList);

// 12.向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0
InsertPosList(&pList, 3, 18);
PrintList(pList);

// 14.从单链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行
DeleteHeadList(&pList);
PrintList(pList);

// 15.从单链表中删除表尾结点并返回它的值,若删除失败则停止程序运行
DeleteLastList(&pList);
PrintList(pList);

// 16.从单链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行
DeletePosList(&pList, 6);
PrintList(pList);

// 17.从单链表中删除值为x的第一个结点,若删除成功则返回1,否则返回0
DeleteValuexList(&pList, 3);
PrintList(pList);

// 18.交换2个元素的位置
SwapList(&pList, 2, 7);
PrintList(pList);

// 19.将线性表进行快速排序
QuickSortList(&pList, NULL);
PrintList(pList);

// 13.向有序单链表中插入元素x结点,使得插入后仍然有序
InsertOrder(&pList, 5);
PrintList(pList);

// 4.清除线性表L中的所有元素,即释放单链表L中所有的结点,使之成为一个空表
ClearList(pList);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: