数据结构C/C++线性表和单链表

本文介绍如何在C++中实现单链表的基本操作,包括创建、插入、删除及查找等,并展示了顺序表的动态增长过程。文章还讨论了在进行节点操作时可能出现的空指针情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在VS2005下面测试通过.最基本的

Code:
  1. #include "stdafx.h"  
  2. #include<stdlib.h>  
  3. #include "stdio.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6. typedef int Type;  
  7. typedef struct LNode{  
  8. Type data;//数据域  
  9. struct LNode *next;//指针域  
  10. }LNode,*LinkList;  
  11. LinkList Head;//头结点  
  12.   
  13. //创建单链表 头插法,栈  
  14. //返回链表头指针  
  15. LinkList CreatLinkList1(){  
  16. LinkList L=new LNode;  
  17. L->next=0;  
  18. LNode *s;  
  19. Type value;//数据类型  
  20. cin>>value;  
  21. while (value!=-1)//-1是结束标志  
  22.     {  
  23.     s=new LNode;  
  24.     s->data=value;  
  25.     s->next=L->next;  
  26.     L->next=s;  
  27.     cin>>value;  
  28.     }  
  29. return L;  
  30. }  
  31.   
  32. //创建单链表尾插法  
  33. //返回链表头指针  
  34. LinkList CreatLinkList2()  
  35. {  
  36. LinkList L=new LNode;  
  37. L->next=0;//空表  
  38. LNode *Node,*rear=L;  
  39. Type value;//数据类型  
  40. cin>>value;  
  41. while(value!=-1)//-1是结束标志  
  42.     {  
  43.     Node=new LNode;//??这里是否会失败?空指针?  
  44.     if(0==Node) break;//??  
  45.     Node->data=value;  
  46.     rear->next=Node;  
  47.     rear=Node;//r指向新的尾结点  
  48.     cin>>value;  
  49.     }  
  50. rear->next=0;  
  51. return L;  
  52. }  
  53.   
  54.   
  55. //求表长 表默认带头结点而且不包含头结点  
  56. int LengthLinkList(LinkList L)  
  57. {  
  58. LNode *p=L;//p指向头结点  
  59. int count=0;  
  60. while(p->next)  
  61.     {  
  62.         p=p->next;  
  63.         count++;  
  64.     }  
  65. return count;  
  66. }  
  67.   
  68. //查找  
  69. //按序号查找,返回指针值  
  70. LNode * getLinkList(LinkList L,int i){  
  71. LNode *p=L;  
  72. int j=0;  
  73. while(p->next!=0&&j<i)  
  74.     {p=p->next;j++;}  
  75.     if(j==i)return p;  
  76.     else return 0;//  
  77. }  
  78. //按值查找,返回第一个符合的值  
  79. LNode *LocateLink(LinkList L,Type x){  
  80.     LNode *p=L->next;  
  81.     while(p!=0&&p->data!=x)  
  82.         p=p->next;  
  83.     return p;  
  84. }  
  85.   
  86. //插入操作在第i个值之之后插入  
  87. bool insertLinkList(LinkList L,int i,Type value)  
  88. {  
  89.     bool returnvalue=false;  
  90.     LinkList p=getLinkList(L,i);  
  91.     LNode *Node=new LNode;//这里是否会失败??  
  92.     if(0!=p)   
  93.     {  
  94.         Node->next=p->next ;  
  95.         p->next=Node;  
  96.         Node->data=value;  
  97.         returnvalue=true;  
  98.     }  
  99.     return returnvalue;  
  100. }  
  101.   
  102. //打印链表所有值并打印长度  
  103. void printLink(LinkList L)  
  104. {     
  105.     int count=0;  
  106.     if(L->next) //从表头走到第一个元素  
  107.     {  
  108.         L=L->next;  
  109.         cout<<L->data<<endl;  
  110.     }  
  111.     L=L->next;//走到第二个元素  
  112.     while(L!=0)  
  113.     {  
  114.         //printf("%d",L->data);//假设是int值  
  115.         cout<<L->data<<endl;  
  116.         count++;  
  117.         L=L->next;  
  118.     }  
  119.     printf("表长度为%d",count);  
  120. }  
  121.   
  122. int main(){  
  123.     LinkList M;  
  124.     //声明  
  125.     LinkList CreatLinkList1();  
  126.     LinkList CreatLinkList2();  
  127.     int LengthLinkList(LinkList L);  
  128.     LNode * getLinkList(LinkList L,int i);  
  129.     bool insertLinkList(LinkList L,int i,Type value);  
  130.     void printLink(LinkList L);  
  131.     //  
  132.     //CreatLinkList1();//测试头插法  
  133.     M=CreatLinkList2();//测试尾插法  
  134.     //printf("长度为%d",LengthLinkList(M));测试长度代码  
  135.     //if(0!=getLinkList(M,3)) printf("找到");//按序号查找  
  136.     //if(LocateLink(M,3)) printf("找到");//测试按值查找  
  137.     //insertLinkList(M,2,8);  
  138.     cout<<"OK"<<endl;  
  139.     printLink(M);  
  140.     system("PAUSE");  
  141.     return 0;  
  142. }  
  143. /////  

顺序表的动态增长

Code:
  1. #include "stdafx.h"  
  2. #include<stdlib.h>  
  3. #define initSize 100//表的初始定义  
  4. #define ListIncrement 10  
  5. typedef int ElemType;  
  6. typedef struct{  
  7. ElemType *elem;//数据元素  
  8. int length;//使用长度  
  9. int listsize;//总长度  
  10. }SqList;  
  11. bool InitSqList(SqList &L){  
  12.     bool trueorfalse=false;  
  13.     L.elem=new ElemType[initSize];  
  14.     if(!L.elem ) {}  
  15.     else {  
  16.     L.listsize=initSize;  
  17.     L.length=0;  
  18.     trueorfalse=true;  
  19.     }  
  20.     return trueorfalse;  
  21. }  
  22. bool insertSqList(SqList &L,int i ,int value){  
  23.     bool trueorfalse=false;  
  24.     if(i<0||i>L.length){}  
  25.     else{  
  26.     int j;  
  27.     for( j=L.length;j>i;j--)  
  28.          L.elem[j]=L.elem[j-1];  
  29.   
  30.     L.elem[j]=value;      
  31.     trueorfalse=true;  
  32.     }  
  33.     return trueorfalse ;  
  34. }  
  35. bool deleteSqList(SqList &L,int i){  
  36.     bool trueorfalse=false;  
  37.     if(i<0||i>L.length) {}  
  38.     else{  
  39.     int j=L.length;  
  40.     for(i;i<j;i++)  
  41.         L.elem[i]=L.elem[i+1];  
  42.   
  43.     L.length--;  
  44.     trueorfalse=true;  
  45.     }  
  46.     return trueorfalse;  
  47. }  
  48. int locateSqList(SqList L,int i)  
  49. {  
  50.     int trueorfalse=-1;  
  51.     if(i<0||i>L.length) {}  
  52.     else{  
  53.     trueorfalse=L.elem[i];  
  54.     }  
  55.     return trueorfalse;  
  56. }  
  57. int main()  
  58. {     
  59.     SqList M;  
  60.     //声明部分  
  61.     bool InitSqList(SqList &L);  
  62.     bool insertSqList(SqList &L,int i ,int value);  
  63.     bool deleteSqList(SqList &L,int i);  
  64.     //声明结束  
  65.     if(InitSqList(M) ) printf("InitTrue/n");  
  66.     if(insertSqList(M,0,1)) printf("inserttrue/n");  
  67.     if(deleteSqList(M,0))printf("deletetrue/n");  
  68.   
  69.     system("PAUSE");  
  70.     return 0;  
  71. }  
  72. ////  

单链表有空指针的可能.

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值