数据结构与算法实验报告

您所在的位置:网站首页 线性链表的实现的实验原理 数据结构与算法实验报告

数据结构与算法实验报告

2024-07-12 18:32| 来源: 网络整理| 查看: 265

实验一 链表

实验目的和要求 1.理解线性表的链式存储结构。 2.熟练掌握动态链表结构及有关算法的设计。 根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关 算法。

实验任务 1. 对任意输入的一组数据,建立一个递增有序的单链表。 2. 将单链表L中的奇数项和偶数项结点分解开,并分别连成一个单链表。 3. 用递增有序的链表A、B表示两个集合,判断B是否是A的子集。 4. 用递增有序的链表A、B表示两个集合,设计算法求它们的并集。 5. 设计算法判断单循环链表是否每个结点的值都是偶数。

实验内容 1实验用仪器,设备 Visual Stidio2019 2实验内容与步骤 实验代码: 1.

#include using namespace std; typedef int elementtype; enum errorcode { success, overflow, underflow, rangeerror }; typedef struct LinkNode { elementtype data; struct LinkNode* next; }node; class list { public: list(); ~list(); bool empty(); errorcode insert(const int i); errorcode print(); private: int count; node* head; }; list::list() { count = 0; head = new node; head->next = NULL; } list::~list() { } bool list::empty() { if (count == 0) return true; else return false; } errorcode list::insert(const int i) { node* p; p = head; node* s = new node; if (empty()) { s->data = i; s->next = head->next; head->next = s; count++; } else { while (p->data next != NULL) { if (p->next->data > i) break; else p = p->next; } s->next = p->next; p->next = s; s->data = i; count++; } return success; } errorcode list::print() { node* p = head; while (p->next != NULL) { cout datanext == NULL) cout x; if(x!=1000) chain.insert(x); } chain.print(); return 0; } int main() { int x; list chain; list odd; //奇数链表 list even; //偶数链表 for (int j = 0;j > x; chain.insert(x); if (x % 2 == 0) even.insert(x); if (x % 2 != 0) odd.insert(x); } cout next; return x; } bool list::compare(list m, list n) { elementtype x1 = 0; elementtype x2 = 0; int judge=0; x1=m.get_top(x1); x2=n.get_top(x2); if (m.count 0 && m.count > 0) { if (x1 == x2) { x1=m.get_top(x1); x2=n.get_top(x2); judge = 1; }; if (x1 > x2) { judge = 0;break; } if (x1 data % 2 != 0) { j = 0; break; } s = s->next; } if (s->next == head) { if (s->data % 2 == 0) j = 1; else j = 0; } return j; }

感想、体会、建议 1.构造单链表时要注意相关节点结构的构造,链表为空时插入新节点时要 注意头节点与新节点的关系;要注意设置结束插入新数据的符号; 2.输入元素时要注意链表是否已满; 3.注意get_top是否改变了x的值,是值传递还是引用,若是值传递要注意 及时改变相关值; 4.求并集时需要注意构造链表时采用的是尾插法还是头插法; 5.注意判断的条件,最后一个结点的判断。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3