【数据结构实验一】线性表

本文介绍了一个基于C语言的线性表实验,包括线性表的创建、元素的插入及输出操作。实验使用顺序存储结构,并实现了动态扩展功能。

写在前面:

教材:数据结构(C语言版) 严蔚敏 吴伟民 编著 清华大学出版社

实验跟随教材编写的顺序

实验一 线性表

实验目的

理解线性表的创建、插入和删除操作;掌握顺序表的定义、插入和删除操作时对数据元素的移动。

实验内容

1. 通过结构体声明顺序存储的线性表;

2. 在初始化操作中,对上述线性表分配存储空间,如可容纳100个元素;

3. 编写插入元素的操作;

4. 编写输出元素的操作;

5. 在主函数中声明一个顺序存储的线性表,通过不断插入新元素,构建保存元素的线性表;依次输出各元素,验证线性表结构的正确性。

实验要求

1. 对重要语句进行备注,以表明对程序的正确理解。

源码:

首先将以后所要用到的宏和头文件写在VS的stdafx.h文件中。

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

//#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <cstdlib>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10



// TODO: reference additional headers your program requires here


开始编写主体代码。

#include <iostream>
#include "stdafx.h"
using namespace std;

typedef int Status;
typedef int ElemType;

//定义一个SqList结构体
typedef struct{
	ElemType *elem;
	int length;
	int listsize;
	}SqList;

//初始化函数
Status InitList_Sq(SqList& L){
	L.elem = (ElemType *)malloc(LIST_INIT_SIZE *sizeof(ElemType));
	if(!L.elem)
		exit(OVERFLOW);
	L.length = 0;
	L.listsize = LIST_INIT_SIZE;
	return OK;
	}

//插入函数,添加报错机制
Status ListInert_Sq(SqList& L, int i, ElemType e){
    if (i < 1 || i > L.length + 1)
        return ERROR;
    if (L.length >= L.listsize)
    {

        ElemType *newbase = (ElemType *)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
        if (!newbase)
            exit(OVERFLOW);
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    ElemType *p, *q;
    q = &(L.elem[i-1]);
	for(ElemType* p = &(L.elem[L.length - 1]); p >= q; --p){
		*(p + 1) = *(p);
	    }
		*q = e;
		++L.length;

		return OK;
	}

//主函数
int main()
{
    int i;
	SqList L1;
	InitList_Sq(L1);
	//调用Inert函数顺序插入10个数
	for(i = 1; i <= 10; i++){
		ListInert_Sq(L1, i, i * 2);
	}
	//显示2到20
	cout << "The total numbers are:" << endl;
	for(i = 0; i < 10; i++){
		cout << L1.elem[i] << " ";
		//printf("%d ", L1.elem[i]);
	}
	cout << endl;
	InitList_Sq(L1);
	//调用Inert函数逆序插入10个数
	for(i = 1; i <= 10; i++){
		ListInert_Sq(L1, 1, i * 2);
	}
	//显示20到2
	cout << "The total numbers are:" << endl;
	for(i = 0; i < 10; i++){
		cout << L1.elem[i] << " ";
	}
	cout << endl;
	return 0;
}

由于是第一个实验,所以代码还是比较少的,具体注释已经写在上面代码中了。


运行结果:


### 实验指导 线性表种基本且重要的数据结构,在数据结构实验中,线性表实验通常包含顺序表和链表两种实现方式。 #### 实验目的 - 理解线性表的基本概念和操作,如插入、删除、查找等。 - 掌握顺序表和链表的存储结构和实现方法。 - 对比顺序表和链表在不同操作下的时间复杂度和空间复杂度。 #### 实验要求 - 实现顺序表和链表的基本操作,包括初始化、插入、删除、查找等。 - 编写测试代码,验证顺序表和链表的正确性。 - 分析顺序表和链表在不同操作下的性能。 ### 代码示例 #### 顺序表实现 ```python class SeqList: def __init__(self): self.data = [] def insert(self, index, value): if 0 <= index <= len(self.data): self.data.insert(index, value) else: print("插入位置不合法") def delete(self, index): if 0 <= index < len(self.data): del self.data[index] else: print("删除位置不合法") def search(self, value): if value in self.data: return self.data.index(value) return -1 # 测试顺序表 seq_list = SeqList() seq_list.insert(0, 1) seq_list.insert(1, 2) print(seq_list.search(2)) seq_list.delete(0) ``` #### 链表实现 ```python class Node: def __init__(self, value=0, next=None): self.value = value self.next = next class LinkedList: def __init__(self): self.head = None def insert(self, index, value): new_node = Node(value) if index == 0: new_node.next = self.head self.head = new_node else: current = self.head for i in range(index - 1): if current is None: print("插入位置不合法") return current = current.next new_node.next = current.next current.next = new_node def delete(self, index): if index == 0: if self.head is not None: self.head = self.head.next else: current = self.head for i in range(index - 1): if current is None or current.next is None: print("删除位置不合法") return current = current.next if current.next is not None: current.next = current.next.next def search(self, value): current = self.head index = 0 while current is not None: if current.value == value: return index current = current.next index += 1 return -1 # 测试链表 linked_list = LinkedList() linked_list.insert(0, 1) linked_list.insert(1, 2) print(linked_list.search(2)) linked_list.delete(0) ``` ### 实现方法 #### 顺序表 - 顺序表使用数组来存储元素,元素在内存中是连续存储的。 - 插入操作:需要将插入位置之后的元素依次后移,时间复杂度为 $O(n)$。 - 删除操作:需要将删除位置之后的元素依次前移,时间复杂度为 $O(n)$。 - 查找操作:可以通过数组下标直接访问元素,时间复杂度为 $O(1)$。 #### 链表 - 链表使用节点来存储元素,每个节点包含个数据域和个指针域,指针域指向下个节点。 - 插入操作:只需要修改指针的指向,时间复杂度为 $O(1)$(不考虑查找插入位置的时间)。 - 删除操作:只需要修改指针的指向,时间复杂度为 $O(1)$(不考虑查找删除位置的时间)。 - 查找操作:需要从头节点开始遍历链表,时间复杂度为 $O(n)$。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值