当前位置:
文档之家› C语言单链表实现19个功能完全详解
C语言单链表实现19个功能完全详解
int i = 0;
if(NULL == pHead)
{
printf("modifyElem函数执行,链表为空\n");
}
if(pos < 1)
{
printf("modifyElem函数执行,pos值非法\n");
return 0;
}
while(pHead !=NULL)
{
++i;
if(i == pos)
return 1;
}
/* 10.向单链表的表头插入一个元素 */
int insertHeadList(Node **pNode,elemType insertElem)
{
Node *pInsert;
pInsert = (Node *)malloc(sizeof(Node));
memset(pInsert,0,sizeof(Node));
{
printf("getElement函数执行,pos值超出链表长度\n");
return 0;
}
return pHead->element;
}
/* 8.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL */
elemType *getElemAddr(Node *pHead, elemType x)
/* 10.向单链表的表头插入一个元素 */
/* 11.向单链表的末尾添加一个元素 */
/* 12.向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0 */
/* 13.向有序单链表中插入元素x结点,使得插入后仍然有序 */
/* 14.从单链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行 */
return size; //链表的实际长度
}
/* 6.检查单链表是否为空,若为空则返回1,否则返回0 */
int isEmptyList(Node *pHead)
{
if(pHead == NULL)
{
printf("isEmptyList函数执行,链表为空\n");
return 1;
}
printf("isEmptyList函数执行,链表非空\n");
{
if(pHead == NULL) //空表,接入表头
{
pHead = p1;
}
else
{
p2->next = p1; //非空表,接入表尾
}
p2 = p1;
p1=(Node *)malloc(sizeof(Node)); //再重申请一个节点
if(p1 == NULL || p2 ==NULL)
void initList(Node **pNode)
{
*pNode = NULL;
printf("initList函数执行,初始化成功\n");
}
/* 2.创建线性表,此函数输入负数终止读取数据*/
Node *creatList(Node *pHead)
{
Node *p1;
Node *p2;
p1=p2=(Node *)malloc(sizeof(Node)); //申请新节点
pInsert->element = insertElem;
while(pHead->next != NULL)
{
pHead = pHead->next;
}
pHead->next = pInsert; //将链表末尾节点的下一结点指向新添加的节点
*pNode = pTmp;
printf("insertLastList函数执行,向表尾插入元素成功\n");
{
Node *pInsert;
Node *pHead;
Node *pTmp; //定义一个临时链表用来存放第一个节点
pHead = *pNode;
pTmp = pHead;
pInsert = (Node *)malloc(sizeof(Node)); //申请一个新节点
memset(pInsert,0,sizeof(Node));
}
if(pHead == NULL)
{
printf("getElement函数执行,链表为空\n");
return 0;
//exit(1);
}
while(pHead !=NULL)
{
++i;
if(i == pos)
{
break;
}
pHead = pHead->next; //移到下一结点
}
if(i < pos) //链表长度不足则退出
{
printf("内存分配失败\n");
exit(0);
}
memset(p1,0,sizeof(Node));
scanf("%d",&p1->element);
p1->next = NULL;
}
printf("creatList函数执行,链表创建成功\n");
return pHead; //返回链表的头指针
}
/* 5.返回单链表的长度 */
int sizeList(Node *pHead)
{
int size = 0;
while(pHead != NULL)
{
size++; //遍历链表size大小比链表的实际长度小1
pHead = pHead->next;
}
printf("sizeList函数执行,链表长度 %d \n",size);
/* 6.检查单链表是否为空,若为空则返回1,否则返回0 */
/* 7.返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行 */
/* 8.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL */
/* 9.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0 */
pInsert->element = insertElem;
pInsert->next = *pNode;
*pNode = pInsert;
printf("insertHeadList函数执行,向表头插入元素成功\n");
return 1;
}
/* 11.向单链表的末尾添加一个元素 */
int insertLastList(Node **pNode,elemType insertElem)
if(p1 == NULL || p2 ==NULL)
{
printf("内存分配失败\n");
exit(0);
}
memset(p1,0,sizeof(Node));
scanf("%d",&p1->element); //输入新节点
p1->next = NULL; //新节点的指针置为空
while(p1->element > 0) //输入的值大于0则继续,直到输入的值为负
{
if(NULL == pHead)
{
printf("getElemAddr函数执行,链表为空\n");
return NULL;
}
if(x < 0)ห้องสมุดไป่ตู้
{
printf("getElemAddr函数执行,给定值X不合法\n");
return NULL;
}
while((pHead->element != x) && (NULL != pHead->next)) //判断是否到链表末尾,以及是否存在所要找的元素
/************************************************************************/
typedef struct Node{ /* 定义单链表结点类型 */
elemType element;
Node *next;
}Node;
/* 1.初始化线性表,即置单链表的表头指针为空 */
{
pHead = pHead->next;
}
if((pHead->element != x) && (pHead != NULL))
{
printf("getElemAddr函数执行,在链表中未找到x值\n");
return NULL;
}
if(pHead->element == x)
{
printf("getElemAddr函数执行,元素 %d 的地址为 0x%x\n",x,&(pHead->element));
#include "stdafx.h"
#include "stdio.h"
#include <stdlib.h>
#include "string.h"
typedef int elemType ;
/************************************************************************/
/* 以下是关于线性表链接存储(单链表)操作的18种算法 */