博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++实现线性表的链接存储结构(单链表)
阅读量:7181 次
发布时间:2019-06-29

本文共 3687 字,大约阅读时间需要 12 分钟。

将线性表的抽象数据类型定义在链接存储结构下用C++的类实现,由于线性表的数据元素类型不确定,所以采用模板机制。

1 头文件linklist.h  2 #pragma once  3 #include 
4 // 单链表的节点 5 template
6 struct Node 7 { 8 T data;//数据域 9 Node
*next;// 指针域,指向后继节点 10 }; 11 // 单链表的类实现 12 template
13 class LinkList 14 { 15 public: 16 LinkList();// 无参构造函数,建立只有头节点的空链表 17 LinkList(T a[],int n);// 有参构造函数,建立有n个元素的单链表 18 ~LinkList();// 析构函数 19 int Length();// 求单链表的长度 20 T Get(int i);// 查找第i个元素 21 int Locate(T x);// 查找值为x的元素 22 void Insert(int i, T x);// 在第i个元素处插入x 23 T Delete(int i);// 删除第i个节点 24 void PrintList();// 遍历各个元素 25 private: 26 Node
* first;// 单链表的头节点 27 }; 28 29 template
30 inline LinkList
::LinkList() 31 { 32 first = new Node
; // 生成头节点 33 first->next = NULL; // 头节点指针域为空 34 } 35 36 // 头插法建立单链表 37 template
38 LinkList
::LinkList(T a[], int n) 39 { 40 first = new Node
; 41 first->next = NULL; // 初始化一个空链表 42 for (int i = 0; i < n; i++) 43 { 44 Node
* S = new Node
; 45 S->data = a[i]; // 为每个数据元素建立一个节点 46 S->next = first->next; 47 first->next = S; // 将节点S插入头节点之后 48 } 49 } 50 // 尾插法建立单链表 51 template
52 LinkList
::LinkList(T a[], int n) 53 { 54 first = new Node
;// 建立头节点 55 first->next = NULL; 56 Node
* r = first;// 尾指针初始化 57 for(int i = 0; i < n; i++) 58 { 59 Node
* S = new Node
; 60 S->data = a[i]; // 为每个数据元素建立一个节点 61 r->next = S; 62 r = S; // 插入节点S,并将尾指针指向S节点 63 } 64 r->next = NULL; // 单链表建立完毕之后,将尾指针置空 65 } 66 67 template
68 LinkList
::~LinkList() 69 { 70 while (first != NULL) 71 { 72 Node
* p = first; // 暂存将被释放节点 73 first = first->next; // 指向下一个节点 74 delete p; 75 } 76 } 77 78 template
79 int LinkList
::Length() 80 { 81 int count = 0; // 计数 82 Node
* p = first->next; // 将工作指针指向第一个节点 83 while (p != NULL) 84 { 85 count++; 86 p = p->next; 87 } 88 return count; 89 } 90 91 template
92 T LinkList
::Get(int i) 93 { 94 int count = 0; // 计数 95 Node
* p = first->next; // 将工作指针指向第一个节点 96 while (p != NULL) 97 { 98 count++; 99 if (count == i)100 return p->data;101 p = p->next;102 }103 return -1; // 越界104 }105 106 template
107 int LinkList
::Locate(T x)108 {109 int count = 0; // 计数110 Node
* p = first->next; // 将工作指针指向第一个节点111 while (p != NULL)112 {113 count++;114 if (p->data == x)115 return count;116 p = p->next;117 }118 return 0; // 查找失败119 }120 121 template
122 void LinkList
::Insert(int i, T x)123 {124 int count = 0; // 计数125 Node
* p = first; // 将工作指针指向头节点126 while (p != NULL)127 { 128 if (count == i - 1) // 找第i-1个节点129 {130 Node
* S = new Node
;131 S->data = x;132 S->next = p->next;133 p->next = S;134 }135 p = p->next;136 count++;137 }138 if (p == NULL)139 throw "位置越界";140 }141 142 template
143 T LinkList
::Delete(int i)144 {145 int count = 0; // 计数146 Node
* p = first; // 将工作指针指向头节点147 while (p != NULL)148 {149 if (count == i - 1)150 {151 Node
* q = p->next;// 暂存被删节点152 T x = q->data;153 p->next = q->next;154 delete q;155 return x;156 }157 p = p->next;158 count++;159 }160 return -1;161 }162 163 template
164 void LinkList
::PrintList()165 {166 Node
* p = first->next; // 将工作指针指向第一个节点167 while (p != NULL)168 {169 cout << p->data << " ";170 p = p->next;171 }172 }
主函数#include"linklist.h"using namespace std;int main(){    int arry[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };    LinkList
* linklist = new LinkList
(arry, 10); cout << linklist->Length() << endl; cout << linklist->Get(5) << endl; cout << linklist->Locate(6) << endl; linklist->Insert(3, 11); linklist->Delete(10); linklist->PrintList(); system("pause"); return 0;}

 运行结果如下:

 

转载于:https://www.cnblogs.com/smile233/p/8063788.html

你可能感兴趣的文章
第十周进度条
查看>>
IIS服务器WEB访问资源文件要求登录
查看>>
在ubuntu中启用ftp服务
查看>>
c基础总结
查看>>
python property装饰器
查看>>
ceph 之cgroup
查看>>
POJ1067 取石子游戏(威佐夫博弈)
查看>>
[DONE]error: field has incomplete type 'cocos2d::LuaValue'
查看>>
转:Linux 环境使用vim搭建php IDE -- 提高代码编写数度数倍!手把手教你打造程序员的上古神器VIM!...
查看>>
Flex 4.0及4.6发布的网络应用在内网内会访问很慢的解决方案
查看>>
zz 关于在VS2008和VS2010中禁用及卸载Visual Assist X的方法研究
查看>>
parseConf(配置文件解析器)
查看>>
Unity3D安卓程序中提示窗与常用静态方法封装
查看>>
code jam训练
查看>>
target与currentTarget的区别?
查看>>
java.util包详解
查看>>
Hadoop中的排序和连接
查看>>
Photoshop在网页设计中的妙用
查看>>
对react与node的初了解(一)
查看>>
LRU与MRU概念
查看>>