算法导论之九链表

1.     与数组不同,链表的顺序是由各个对象里的指针决定的。双向链表L的每一个元素都是一个对象,每个对象有一个关键字key和两个指针next和prev。设x为链表的一个元素,x.next指向的它在链表中的后继元素,x.prev指向它的前驱元素。如果x.prev=NIL,

则元素x是链表的头,若x.next=NIL,则x是链表的尾。若L.head=NIL,则链表为空。

2.     循环链表中,表头元素的prev指针指向表尾元素,而表尾元素的next指针则指向表头元素。

3.     链表的搜索,过程LIST-SEARCH(L,k)采用简单的线性搜索方法,用于查找链表L中的第一个关键字为k的元素,并返回指向该元素的指针。

LIST-SERACH(L,k)

       x = L.head

       while x != NIL and x.key != k

              x = x.next

       return x

4.     链表的插入,过程LIST-INSERT(L,x)将x“连接入”到链表的前端

LIST-INSERT(L,x)

       x.next = L.head

       if L.head !=NIL

              L.head.prev = x

       L.head = x

       x.prev = NIL

5.     链表的删除,过程LIST-DELETE(L,x)将一个元素x从链表L中移除。

LIST-DELETE(L,x)

       if x.prev != NIL

              x.prev.next = x.next

       else L.head = x.next

       if x.next != NIL

              x.next.prev = x.prev

6.     单链表的程序代码如下:

#include <STDIO.H>

#include <STDLIB.H>

#include <STRING.H>

 

typedef int elemType;

 

//定义单链表的结点类型

typedef struct Node

{

       elemTypeelement;

       Node*next;

}Node;

 

 

//初始化线性表

void initList(Node **pNode)

{

       *pNode= NULL;

       printf("initList函数执行,初始化成功\n");

}

 

//创建线性表,此函数输入负数终止读取数据

Node *createList(Node *pHead)

{

       Node*p1,*p2;

       p1= p2 = (Node*)malloc(sizeof(Node));

 

       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)

       {

              if(pHead == NULL)

              {

                     pHead= p1;    //空表,接入表头

              }

              else

              {

                     p2->next= p1; //非空表,接入表尾

              }

 

              p2= p1;

 

              p1= (Node*)malloc(sizeof(Node));      //重新申请一个结点

              if(p1 == NULL || p2 == NULL)

              {

                     printf("内存分配失败\n");

                     exit(0);

              }

 

              memset(p1,0,sizeof(Node));

             

              scanf("%d",&p1->element);  //输入新结点

              p1->next= NULL;

       }

 

       printf("createList函数执行,链表创建成功\n");

       returnpHead;  //返回链表的头指针

}

 

//打印链表,链表的遍历

void printList(Node *pHead)

{

       if(pHead == NULL)

       {

              printf("printList函数执行,链表为空\n");

              return;

       }

       else

       {

              while(pHead != NULL)

              {

                     printf("%d",pHead->element);

                     pHead= pHead->next;

              }

              printf("\n");

       }

}

 

//清除链表中的所有元素,释放单链表L中所有的结点,使之成为一个空表

void clearList(Node *pHead)

{

       Node*pNext; //定义一个与pHead相邻节点

 

       if(pHead == NULL)

       {

              printf("printList函数执行,链表为空\n");

              return;

       }

       while(pHead->next!= NULL)

       {

              pNext= pHead->next;   //保存下一个结点的指针

              free(pHead);

              pHead= pNext;      //表头下移

       }

       printf("clearList函数执行,链表已清除\n");

}

 

//返回单链表的长度

int sizeList(Node *pHead)

{

       intsize = 0;

       while(pHead != NULL)

       {

              size++;    //遍历链表的size比链表的实际长度小1

              pHead= pHead->next;

       }

       printf("sizeList函数执行,链表长度%d \n",size);

       returnsize;

}

 

//检查单链表是否为空

int isEmptyList(Node *pHead)

{

       if(pHead == NULL)

       {

              printf("isEmptyList函数执行,链表为空\n");

              return1;

       }

       else

              printf("isEmptyList函数执行,链表非空\n");

       return0;

}

 

//返回单链表中的第pos个节点中的元素

elemType getElement(Node *pHead,int pos)

{

       inti = 0;

 

       if(pos < 1)

       {

              printf("getElement函数执行,pos值非法\n");

              return0;

       }

 

       if(pHead == NULL)

       {

              printf("getElement函数执行,链表为空");

              return0;

       }

 

       while(pHead != NULL)

       {

              ++i;

              if(i == pos)

              {

                     break;

              }

              pHead= pHead->next;   //移到下一节点

       }

 

       if(i < pos)

       {

              printf("getElement函数执行,pos值超出链表长度\n");

              return0;

       }

 

       returnpHead->element;

}

 

//从单链表中查找具有给定值x的第一个元素,成功则返回该结点data的存储地址,否则返回NULL

elemType *getElemAddr(Node*pHead, elemType x)

{

       if(pHead == NULL)

       {

              printf("getElemAddr函数执行,链表为空");

              return0;

       }

 

       if(x < 0)

       {

              printf("getElemAddr函数执行,给定x值非法\n");

              return0;

       }

 

       //判断是否到链表末尾,以及是否存在所要找的元素

       while((pHead->element != x) && (NULL != pHead->next))

       {

              pHead= pHead->next;

       }

 

       if((pHead->element != x) && pHead != NULL)

       {

              printf("getElemAddr函数执行,在链表中未找到x值\n");

              return0;

       }

 

       if(pHead->element == x)

       {

              printf("getElemAddr函数执行,元素%d 的地址为0x%x\n",x,&(pHead->element));

       }

 

       return&(pHead->element);

}

 

//把单链表中的第pos个节点的值修改为x的值

int modifyElem(Node *pNode, intpos, elemType x)

{

       Node*pHead;

       pHead= pNode;

       inti = 0;

 

       if(pHead == NULL)

       {

              printf("modifyElem函数执行,链表为空\n");

              return0;

       }

 

       if(pos < 1)

       {

              printf("modifyElem函数执行,pos值非法\n");

              return0;

       }

 

       while(pHead != NULL)

       {

              ++i;

              if(i == pos)

              {

                     break;

              }

              pHead= pHead->next;

       }

 

       if(i < pos)

       {

              printf("modifyElem函数执行,pos值超出链表长度\n");

              return0;

       }

 

       pNode= pHead;

       pNode->element= x;

       printf("modifyElem函数执行\n");

       return1;

}

 

//向单链表的表头插入一个元素

int insertHeadList(Node **pNode,elemType insertElem)

{

       Node*pInsert;

       pInsert= (Node*)malloc(sizeof(Node));

       memset(pInsert,0,sizeof(Node));

 

       pInsert->element= insertElem;

       pInsert->next= *pNode;

       *pNode= pInsert;

 

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

 

       return1;

}

 

//向单链表的末尾添加一个元素

int insertLastList(Node**pNode,elemType insertElem)

{

       Node*pInsert,*pHead,*pTemp;

 

       pHead= *pNode;

       pTemp= pHead;

       pInsert= (Node*)malloc(sizeof(Node));

       memset(pInsert,0,sizeof(Node));

 

       pInsert->element= insertElem;

 

       while(pHead->next != NULL)

       {

              pHead= pHead->next;

       }

 

       pHead->next= pInsert;

       *pNode= pTemp;

 

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

 

       return1;

}

 

//

int main()

{

       Node*pList = NULL;

       intlength = 0;

 

       elemTypeposElem;

 

       initList(&pList);

       printList(pList);

 

       pList= createList(pList);

       printList(pList);

 

       sizeList(pList);

 

       isEmptyList(pList);

 

       posElem= getElement(pList,3);

       printf("getElement函数执行,位置3 中的元素为 %d\n",posElem);

       printList(pList);

 

       getElemAddr(pList,5);

 

       modifyElem(pList,4,1);

       printList(pList);

 

       insertHeadList(&pList,12);

       printList(pList);

 

       insertLastList(&pList,10);

       printList(pList);

 

       clearList(pList);

       system("pause");

 

       return0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mengrennwpu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值