链表的操作

// xuanze-sort.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#define MAXSIZE 100 //定义线性表的最大长度
#include "stdio.h"
#include <stdlib.h>
#include "string.h"

typedef int elemType ;


/************************************************************************/
/* 以下是关于线性表链接存储(单链表)操作的18种算法 */

/* 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.将线性表进行快速排序 */


/************************************************************************/
typedef struct Node{ /* 定义单链表结点类型 */
elemType element;
Node *next;//链表的指针域
}Node;

/* 11.向单链表的末尾添加一个元素,成功插入返回1 */
int insertLastList(Node *pHead,elemType insertElem)
{
Node *pNode = pHead;
Node* pInsert = (Node *)malloc(sizeof(Node)); //申请一个新节点


pInsert->element = insertElem;//插入的数据
pInsert->next = NULL;//链表尾端
if(pNode->next==NULL)
{
pNode->next = pInsert;
}
else
{
pNode = pNode->next;
while(pNode->next != NULL)
{
pNode = pNode->next;
}
pNode->next = pInsert;
}
printf("insertLastList函数执行,向表尾插入元素成功\n");
return 1;
}
/* 1.初始化线性表,即置单链表的表头指针为空 */
Node* initList()
{
Node* pNode=(Node*)malloc(sizeof(Node));
if (pNode!=NULL)
{
printf("initList函数执行,初始化成功\n");
pNode->next = NULL;
return pNode;
}
else
printf("initList函数执行,初始化失败\n");
return NULL;
}
/*2、向链表中插入数据*/
void creatList(Node *pHead)
{
//Node* temp;
Node* pNode = pHead;
while(1)
{
elemType data;
printf("请输入数据,输出为0时退出!\n");
scanf_s("%d",&data);
if (data == 0)
break;
else
int x = insertLastList(pHead, data);
}

}


/* 3.打印链表,链表的遍历,头链表没有数据元素,链表末没有指针地址*/
void printList(Node *pHead)
{
if( pHead->next == NULL) //链表为空
{
printf("PrintList函数执行,链表为空\n");
}
else
{
while(NULL != pHead->next)
{
pHead = pHead->next;
printf("%d ",pHead->element);
}
printf("\n");
}
}

/* 4.清除线性表L中的所有元素,即释放单链表L中所有的结点,使之成为一个空表 */
void clearList(Node *pHead)
{
Node* pNode = pHead;
Node* pTemp;
if(pNode->element==NULL)
{
printf("空链表\n");
}
else
{

pNode = pNode->next;
while(pNode->next!=NULL)
{
pTemp = pNode;//保存当前节点
pNode = pNode->next;//指向下一个节点
free(pTemp);
}
free(pNode);
pHead->next = NULL;
printf("清除链表中元素成功,返回一个空链表\n");

}
}

/* 5.返回单链表的长度 */
int sizeList(Node *pHead)
{
Node* pNode = pHead;
int size = 0;

while(pNode->next != NULL)
{
size++; //遍历链表size大小比链表的实际长度小1
pNode = pNode->next;
}
printf("sizeList函数执行,链表长度 %d \n",size);
return size; //链表的实际长度
}

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

return 0;
}

/* 7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行 */
elemType getElement(Node *pHead, int pos)
{
int i=0;
Node* pNode = pHead;

if(pos < 1)
{
printf("getElement函数执行,pos值非法\n");
return 0;
}
if(pNode->next == NULL)
{
printf("getElement函数执行,链表为空\n");
return 0;
//exit(1);
}
while(pNode->next != NULL)
{
pNode=pNode->next;
++i;
if(i == pos)
{
return pNode->element;
}
if(i>=pos)
{
printf("error(有错误)!\n");
return -1;
}

}
if(i>=pos)
{
printf("error(溢出)!\n");
return 0;
}
else
return 1;

}

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

while((pNode->element != x) && ( pNode->next!=NULL)) //判断是否到链表末尾,以及是否存在所要找的元素
{
pNode = pNode->next;
}
if((pNode->element != x) )
{
printf("getElemAddr函数执行,在链表中未找到x值\n");
return NULL;
}
else
return &(pNode->element);

}

/* 9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0 */
int modifyElem(Node *pHead,int pos,elemType x)
{
Node *pNode = pHead;
int i = 0;
if(pNode->next==NULL)
{
printf("modifyElem函数执行,链表为空\n");
return 0;
}
if(pos < 1)
{
printf("modifyElem函数执行,pos值非法\n");
return 0;
}


while(pNode->next !=NULL )//存在下一个节点
{
pNode = pNode->next;//指向下一个节点
++i;
if(i == pos)
{
pNode->element = x;
return 1;
}
}
if(i < pos) //链表长度不足则退出
{
printf("modifyElem函数执行,pos值超出链表长度\n");
return 0;
}

}

/* 10.向单链表的表头插入一个元素 */
int insertHeadList(Node *pNode,elemType insertElem)
{
Node *pInsert;
pInsert = (Node *)malloc(sizeof(Node));//为分配的新节点分配内存
pInsert->element = insertElem;
pInsert->next = pNode->next;
pNode->next = pInsert;

printf("insertHeadList函数执行,向表头插入元素成功\n");

return 1;
}

/* 11、找出链表中的倒数第k个元素的值 */
elemType findList(Node* pHead,int k)
{
int i ;
Node* pNode1 = pHead;
Node* pNode2 = pHead;
for(i=0;i<k;i++)
{
pNode2 = pNode2->next; //遇到
}
while(pNode2->next!=NULL)
{
i++;
pNode1=pNode1->next;
pNode2 = pNode2->next;
}
return pNode1->next->element;

}

/* 12、实现单链表反转*/
void reverseNode(Node* pHead)
{
Node* pNode1=pHead;
Node* pNode2 = pHead;

if(pNode1->next!=NULL)
{
pNode2->next = NULL;
pNode2->element = pHead->next->element;//pNode2为最后一个节点
pNode1 = pNode2;
while(pNode1->next!=NULL)
{
pNode1 = pNode1->next;//指向下一个节点


}
}
else
{
printf("空链表\n");
}

}


/* 12.向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0 */


/* 13.向有序单链表中插入元素x结点,使得插入后仍然有序 */
/* 14.从单链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行 */
/* 15.从单链表中删除表尾结点并返回它的值,若删除失败则停止程序运行 */
/* 16.从单链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行 */
/* 17.从单链表中删除值为x的第一个结点,若删除成功则返回1,否则返回0 */
/* 18.交换2个元素的位置 */
/* 19.将线性表进行快速排序 */

/******************************************************************/
int main(int argc, char* argv[])
{
Node *pList=NULL;
int length = 0;


int result =0;

pList = initList(); //链表初始化,就是给结构体指针动态分配内存,且plist->next=NULL;
//遍历链表,打印链表

result = insertLastList(pList,10); //表尾插入元素10
printList(pList);
result = insertLastList(pList,11); //表尾插入元素11
printList(pList);
result = insertLastList(pList,12); //表尾插入元素12
printList(pList);
result = insertLastList(pList,13); //表尾插入元素13
printList(pList);
creatList(pList);//插入元素,输入为0时的时候返回
printList(pList);
result = insertHeadList(pList,0);//表头插入元素
printList(pList);
result = modifyElem(pList,1,-1);//链表第一个数字改为-1
printf("返回链表中的数为11的地址%p\n",getElemAddr(pList, 10));//返回链表中的数为11的地址
printf("链表的长度为:%d\n",sizeList(pList));//返回链表的长度
clearList(pList);
printf("链表的长度为:%d\n",sizeList(pList));//返回链表的长度

 

 

 





return 0;
}
//ABD##E##CF##G##

posted @ 2016-07-31 19:20  Axel_uestc  阅读(236)  评论(0编辑  收藏  举报