数据结构实验报告-实验1 线性表(2)

本文由用户“xiao8lu”分享发布 更新时间:2021-11-22 11:45:05 举报文档

以下为《数据结构实验报告-实验1 线性表(2)》的无排版文字预览,完整格式请下载

下载前请仔细阅读文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。

数据结构实验报告

实验名称:实验1—线性表

学生:梅某某

班级:人工201

学号:***53

日期:11.9

实验要求

1.1.实验目的:熟悉C语言得基本编程方法,掌握集成编译环境的调试方法;学习指针使用;掌握线性表的操作的实现方法;学习使用线性表解决实际问题的能力。

1.2.实验内容:

题目1:顺序表的基本功能:

1、插入:完成顺序表的插入功能

3、删除:删除原顺序表中指定元素

4、查找:查找顺序表中的元素并返回下标值

5、获取顺序表长度

6、更改:更改顺序表中指定的某一元素值

7、其他:可自行定义

编写函数测试线性表的正确性。

顺序表定义为:typedef struct Table {

int* head;

int length;

int size;

}table;

#include

#include

#define Size 5

typedef struct Table {

int * head;

int length;

int size;

}table;

table initTable() {

table t;

t.head = (int*)malloc(Size * sizeof(int));

if (!t.head)

{

printf("初始化失败");

exit(0);

}

t.length = 0;

t.size = Size;

return t;

}

table addTable(table t, int elem, int add)

{

int i;

if (add > t.length + 1 || add < 1) {

printf("插入位置有问题");

return t;

}

if (t.length >= t.size) {

t.head = (int *)realloc(t.head, (t.size + 1) * sizeof(int));

if (!t.head) {

printf("存储分配失败");

}

t.size += 1;

}

for (i = t.length - 1; i >= add - 1; i--) {

t.head[i + 1] = t.head[i];

}

t.head[add - 1] = elem;

t.length++;

return t;

}

table delTable(table t, int add) {

int i;

if (add > t.length || add < 1) {

printf("被删除元素的位置有误");

exit(0);

}

for (i = add; i < t.length; i++) {

t.head[i - 1] = t.head[i];

}

t.length--;

return t;

}

int selectTable(table t, int elem) {

int i;

for (i = 0; i < t.length; i++) {

if (t.head[i] == elem) {

return i + 1;

}

}

return -1;

}

table amendTable(table t, int elem, int newElem) {

int add = selectTable(t, elem);

t.head[add - 1] = newElem;

return t;

}

void displayTable(table t) {

int i;

for (i = 0; i < t.length; i++) {

printf("%d ", t.head[i]);

内容过长,仅展示头部和尾部部分文字预览,全文请查看图片预览。 0;

//生成链表

for (i = 1; i < 5; i++) {

link *a = (link*)malloc(sizeof(link));

a->elem = i;

a->next = NULL;

temp->next = a;

temp = temp->next;

}

return p;

}

link * insertElem(link * p, int elem, int add) {

link * temp = p;//创建临时结点temp

link * c = NULL;

int i = 0;

//首先找到要插入位置的上一个结点

for (i = 1; i < add; i++) {

if (temp == NULL) {

printf("插入位置无效\n");

return p;

}

temp = temp->next;

}

//创建插入结点c

c = (link*)malloc(sizeof(link));

c->elem = elem;

//向链表中插入结点

c->next = temp->next;

temp->next = c;

return p;

}

link * delElem(link * p, int add) {

link * temp = p;

link * del = NULL;

int i = 0;

//遍历到被删除结点的上一个结点

for (i = 1; i < add; i++) {

temp = temp->next;

}

del = temp->next;//单独设置一个指针指向被删除结点,以防丢失

temp->next = temp->next->next;//删除某个结点的方法就是更改前一个结点的指针域

free(del);//手动释放该结点,防止内存泄漏

return p;

}

int selectElem(link * p, int elem) {

link * t = p;

int i = 1;

while (t->next) {

t = t->next;

if (t->elem == elem) {

return i;

}

i++;

}

return -1;

}

link *amendElem(link * p, int add, int newElem) {

int i = 0;

link * temp = p;

temp = temp->next;//tamp指向首元结点

//temp指向被删除结点

for (i = 1; i < add; i++) {

temp = temp->next;

}

temp->elem = newElem;

return p;

}

void display(link *p) {

link* temp = p;//将temp指针重新指向头结点

//只要temp指针指向的结点的next不是Null,就执行输出语句。

while (temp->next) {

temp = temp->next;

printf("%d ", temp->elem);

}

printf("\n");

}

int getLength(link* p) {

int i =1;

link*temp=p;

temp = temp->next;

while(temp!=NULL){

temp = temp->next;

i+=1;

}

return i;

}

结果分析:指针和链表一开始没理解透彻,但是明白之后就比较好做了,写函数的时候要区分链表与顺序表的区别

/

[文章尾部最后300字内容到此结束,中间部分内容请查看底下的图片预览]请点击下方选择您需要的文档下载。

  1. 《Java Web开发技术》 实 验 报 告模板
  2. 实验一 顺序表、单链表的基本操作(答案)
  3. 实验18、 数据存储(3)
  4. 线性表实验报告
  5. 链表的建立课件
  6. 线性表的基本操作(1)实验报告
  7. 计算机等级考试公共基础知识超强总结,栈、队列、树
  8. 公共基础知识点精简版
  9. 黄毛数据结构模拟练习
  10. (数据结构)实验报告单
  11. 复习提纲_6月_测绘工程
  12. 实验1-线性表的基本操作

以上为《数据结构实验报告-实验1 线性表(2)》的无排版文字预览,完整格式请下载

下载前请仔细阅读上面文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。

图片预览