数据结构(严版)课本代码重敲——第二章

复习笔记 数据结构 第二章 线性表

说明:数据结构高分笔记上的经典编程题

  1 #include <iostream>
  2 #include <string.h>
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 #define dataSize 20
  6 #define ERROR -1
  7 using namespace std;
  8 /*
  9     题目:数据结构 cha2 线性表
 10     内容:1. 顺序表的操作
 11     日期:2018/3/9
 12     时间:tomato * 2
 13     笔记:1. 线性表包括顺序表和链表
 14     2. 顺序表是逻辑和存储结构均为线性连续的数组形式,一般用数组表示
 15     3.链表分为单链表和双链表,包括带头结点和不带头结点两种类型,也包括循环和非循环两种类型
 16     还有一种特殊的“静态链表”
 17 */
 18 /* 顺序表的定义方式
 19     注意:typedef的含义是给这个结构体起的名字叫做Sqlist,是一种用户自定义的数据类型
 20     而平时编程中struct student {}stu[20];则是C++中的简略写法,stu[20]即为创建的
 21     数据类型为Student的数组变量
 22 */
 23 typedef struct
 24 {
 25     int data[dataSize];
 26     int length;
 27 }Sqlist;
 28 
 29 
 30 typedef int Elemtype;
 31 
 32 
 33 /* (1)以下为顺序表的基本操作
 34     1. 初始化
 35     2. 按元素值的查找
 36     3. 求指定位置元素
 37     4. 插入元素
 38     5. 删除元素
 39     笔记:1.是否用&取决于改结构体的内容
 40 */
 41 void initSqlist(Sqlist &l)
 42 {
 43     l.length = 0;
 44 }
 45 int findElem(Sqlist l,Elemtype x)
 46 {
 47     for (int i=0;i<l.length;i++)
 48     {
 49         if (l.data[i] == x)
 50             return i;
 51     }
 52     return ERROR;
 53 }
 54 Elemtype getElem(Sqlist l, int loc)
 55 {
 56     if (loc < 0 || loc > l.length-1)
 57         return ERROR;
 58     return l.data[loc];
 59 }
 60 void insertSqlist(Sqlist &l,Elemtype x,int loc)
 61 {
 62     if (loc < 0 || loc >l.length-1)
 63         cout<<"error";
 64     for (int i=l.length-1 ; i >= loc ; i--  )
 65     {
 66         l.data[i+1] = l.data[i];
 67     }
 68     l.data[loc] = x;
 69     l.length++; // ★★★ 勿忘!
 70 }
 71 Elemtype deleteSqlist(Sqlist &l,int loc,Elemtype &x)
 72 {
 73     if (loc<0||loc>l.length-1)
 74         {cout << "error";return 0;}
 75     x = l.data[loc];
 76     for (int i=loc;i<l.length-1;i++)
 77     {
 78         l.data[i] = l.data[i+1];
 79     }
 80     l.length -- ;
 81     return x;
 82 }
 83 // 单链表结点定义
 84 typedef int Elemtype;
 85 typedef struct LNode
 86 {
 87     Elemtype data;
 88     struct LNode *next;
 89 }LNode;
 90 
 91 /*
 92     (2)单链表的操作
 93     1. 创建单链表 尾插法和头插法
 94     2. 归并单链表 递增和递减(头插法)
 95     3. 查找单链表
 96     4. 插入单链表
 97     5. 删除
 98     6. 打印链表内容
 99     7. 查找x并删除
100 */
101 // 头插法
102 void createLNode(LNode *&h,int n)
103 {
104     // 1. 创建头结点
105     h = (LNode *)malloc(sizeof(LNode));
106     h->next = NULL;
107     Elemtype x;
108     cout<<"输入线性表数据"<<endl;
109     LNode *q;
110     q = h;
111     for(int i=0;i<n;i++)
112     {
113         cin >> x;
114         q = (LNode *)malloc(sizeof(LNode));
115         q->data = x;
116         q->next = h->next;
117         h->next = q;
118     }
119 
120 }
121 // 尾插法
122 void createLNodeH(LNode *&h,int n)
123 {
124     h = (LNode *)malloc(sizeof(LNode));
125     h->next = NULL;
126     LNode *p,*q;
127     q = h;
128     int x;
129     cout <<"数据:"<<endl;
130     for (int i=0;i<n;i++)
131     {
132         cin >> x;
133         p = (LNode *) malloc(sizeof(LNode));
134         p->data = x;
135         q->next = p;
136         q = p;
137     }
138     p->next = NULL; // 最后赋值即可
139 
140 }
141 void print(LNode *h)
142 {
143     LNode *t = h;
144     while (t->next!=NULL)
145     {
146         t = t->next;
147         cout<<t->data<<' ';
148     }
149     cout << endl;
150 }
151 void mergeLNode(LNode *&a,LNode *&b,LNode *&c)
152 {
153     LNode *pa,*pb,*pc;
154     pa = a->next;pb=b->next;
155     pc = (LNode *)malloc(sizeof(LNode));
156     c = pc ;
157     while (pa!=NULL && pb!=NULL)
158     {
159         cout <<"test"<<endl;
160         if (pa->data >= pb->data)
161         {
162             pc->next = pb;
163             pb = pb->next;
164             pc = pc->next;
165         }
166         else
167         {
168             pc->next = pa;
169             pa = pa->next;
170             pc = pc->next;
171         }
172     }
173     pc->next = NULL;
174     if (pa!=NULL)
175     {
176         pc->next = pa;
177     }
178     if (pb!=NULL)
179     {
180         pc->next = pb;
181     }
182 
183 }
184 void merge_2(LNode *&a,LNode *&b,LNode *&c)
185 {
186     LNode *pa,*pb,*pc;
187     pa = a->next; pb = b->next;
188     c = (LNode *)malloc(sizeof(LNode));
189     pc = c;
190 
191     while (pa!=NULL && pb!=NULL)
192     {
193         if (pa->data >= pb->data)
194         {
195             pc = pb;
196             pb = pb->next;
197             pc -> next = c->next;
198             c->next = pc;
199         }
200         else
201         {
202             pc = pa;
203             pa = pa->next;
204             pc -> next = c->next;
205             c->next = pc;
206         }
207     }
208     while (pa!=NULL)
209     {
210         pc = pa;
211         pa = pa->next;
212         pc -> next = c->next;
213         c->next = pc;
214     }
215     while (pb!=NULL)
216     {
217         pc = pb;
218         pb = pb->next;
219         pc -> next = c->next;
220         c->next = pc;
221     }
222 }
223 
224 int findAndDelete(LNode *l,Elemtype x)
225 {
226     LNode *p = l,*q;
227 
228     while (p->next!=NULL)
229     {
230        if (p->next->data == x)
231        {
232            q = p->next ;
233            p->next = q->next;
234            return 1;
235        }
236        p = p->next;
237     }
238     return 0;
239 }
240 // 双链表结点定义
241 typedef struct DLNode
242 {
243     Elemtype data;
244     struct DLNode *prior;
245     struct DLNode *next;
246 }DLNode;
247 
248 /*
249     1.双链表的建立
250     2. 查找结点
251     3. 插入结点,在p之后
252 */
253 void createDLNode(DLNode *&dl,int n)
254 {
255     dl = (DLNode *)malloc(sizeof(DLNode));
256     dl ->prior = NULL;
257     DLNode *p,*q;
258     p = dl;
259     cout<<"data:"<<endl;
260     int x;
261     for (int i=0;i<n;i++)
262     {
263         q = (DLNode *)malloc(sizeof(DLNode));
264         cin >> x;
265         q->data = x;
266         p->next = q;
267         q->prior = p;
268         p = q;
269     }
270     p->next = NULL;
271 
272 }
273 
274 int main()
275 {
276     DLNode *dl;
277     int n;
278     cout<<"数据个数n:"<<endl;
279     cin>>n;
280     createDLNode(dl,n);
281     print(dl);
282     return 0;
283 }

 

转载于:https://www.cnblogs.com/twomeng/p/9509556.html

数据结构》(C语言版) 算法源码及运行演示系统使用说明 一、启动演示系统 双击演示系统应用程序文件“DS_VC_ALGO.EXE”启动演示系统,出现图1所示界面。 图1 《数据结构》(C语言版)算法源码及运行演示系统主界面 二、演示系统使用步骤 除了个别算法之外,演示系统给出了《数据结构》(C语言版)书中算法对应的程序代码(CPP文件)和测试运行程序(VC++6.0的EXE文件)。通过本系统,可以显示算法的源代码以及运行结果。具体操作步骤如下: 1.选择相应章 单击演示系统界面右侧章选择按钮。 例如,要选择第6章,则单击“第6章”选择按钮。 当相应章被选择后,窗口的右侧部分将列出本章的算法选择按钮。 例如,选择第6章后,窗口的右侧部分将显示第6章中的算法6.1-6.13和6.15的选择按钮。由于书中的算法6.14和6.16只是示意性算法,故未给出源码,其按钮上的文字为灰色,处于“无效”状态。 2.选择相应章中的算法 单击窗口右侧部分所列举的本章某个算法选择按钮,被选择的算法的源码将在窗口左侧空白区域中显示。对于较长的源码,单击显示区域后,可用键盘的光标键和翻页键浏览源码。 例如,选择了第6章中的算法6.5后界面如图2所示: 图2 选择算法6.5 3.运行测试程序 单击窗口上部的“运行”按钮,将弹出运行窗口,运行所选算法的测试程序。若运行按钮为灰色,表示该算法无单独测试程序。 例如,算法6.5的测试运行窗口如图3所示: 图3 测试运行窗口 测试运行说明: 测试运行窗口显示程序的执行过程及结果。若在显示过程中出现运行窗口无法正常演示的情况,只需调节运行窗口大小即可正常显示(调节最小化按钮或窗口最大化/还原按钮“ ”)。 三、退出演示系统 使用完毕后,单击窗口右上角关闭按钮“ ”退出演示系统。 四、测试程序示例 在《数据结构》的课程教学中,各抽象数据类型的设计与实现是重要的学习和实践环节。为此,本系统只给出了各算法源码的测试程序的可执行文件。在此,给出算法6.5的测试程序示例,以供参考。 算法6.5是中序遍历线索二叉树的非递归算法,要对其源码进行测试,可首先调用算法6.6及6.7建立中序线索二叉树。以下是测试程序的源码,相关类型和辅助函数定义在文件include06.h和include06.cpp中,此略。 // test0605.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "include06.h" // 相关类型和辅助函数的定义 BiThrTree pre; // 线索二叉树遍历辅助变量 #include "algo0607.cpp" // 算法6.7源码 #include "algo0606.cpp" // 算法6.6源码 #include "algo0605.cpp" // 算法6.5源码 int main(int argc, char* argv[]) { char gl_str[64]; BiThrTree T; BiThrTree Thrt; printf("*******************************************\n"); printf("* 《数据结构》(C语言版)严蔚敏,吴伟民 *\n"); printf("* 算法6.5, 6.6 & 6.7 *\n"); printf("*******************************************\n"); srand((unsigned)time(NULL)); // 随机函数初始化 T=NULL; // 空二叉树T for (int pass=0; pass<5; pass++) { // 测试运行5次,第一次为空树 outBiThrTree(T,gl_str); // 以类广义表的形式输出二叉树T到gl_str printf("T = %s\n", gl_str); // 显示 pre = NULL; Status r = InOrderThreading(Thrt, T); // 算法6.6,6.7,中序线索化 printf("InOrderThreading(Thrt, T) : %s\n", (r) ? "OK" : "ERROR"); initVisitStr(); // 将visitStr清为空串 InOrderTraverse_Thr(Thrt, v
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值