数据结构单链表头文件
- 格式:docx
- 大小:14.60 KB
- 文档页数:2
10)调用头插法的函数,分别输入10,20,分别回车:
11)调用尾插法的函数,分别输入30,40
12)查找单链表的第四个元素:
13)主函数中传入参数,删除单链表的第一个结点:
14)主函数传入参数,删除第0个未位置的元素,程序报错:
15)最后,输出单链表中的元素:
return 0;
}
6)编译,连接,运行源代码:
7)输入8,回车,并输入8个数,用空格分隔开,根据输出信息,可以看出,链表已经拆分为两个
五、实验总结
1.单链表采用的是数据+指针的表示形式,指针域总是指向下一个结
点(结构体)的地址,因此,在内存中的地址空间可以是不连续的,操作比顺序存储更加的方便
2.单链表使用时,需要用malloc函数申请地址空间,最后,删除元
素时,使用free函数释放空间。
数据结构实验报告--单链表数据结构实验报告--单链表1.引言1.1 研究目的本实验旨在通过实践的方式,深入了解单链表的数据结构以及相关操作,提升对数据结构的理解和应用能力。
1.2 实验内容本实验主要包括以下几个方面的内容:●单链表的基本定义和实现●单链表的插入、删除、遍历操作●单链表的逆置操作●单链表的查找和修改操作2.理论基础2.1 单链表的定义单链表是一种常见的线性数据结构,它由一系列的节点组成,每个节点包含数据和指向下一个节点的指针。
2.2 单链表的基本操作①单链表的插入操作在单链表中,可以通过插入操作在指定位置插入一个新节点,该操作主要包括以下步骤:●创建一个新的节点,并为其赋值●将新节点的next指针指向插入位置的后一个节点●将插入位置的前一个节点的next指针指向新节点②单链表的删除操作在单链表中,可以通过删除操作删除指定位置的节点,该操作主要包括以下步骤:●将删除位置的前一个节点的next指针指向删除位置的后一个节点●释放删除节点的内存③单链表的遍历操作单链表的遍历操作主要是依次访问链表中的每一个节点,并执行相应的操作。
④单链表的逆置操作单链表的逆置操作可以将一个单链表中的节点顺序进行颠倒。
⑤单链表的查找操作在单链表中,可以通过查找操作找到指定值的节点。
⑥单链表的修改操作在单链表中,可以通过修改操作修改指定位置的节点的值。
3.实验过程3.1 实验环境本次实验使用C语言进行编程,需要先安装相应的编程环境,如gcc编译器。
3.2 实验步骤①单链表的创建和初始化首先创建一个空链表,并初始化链表的头指针。
②单链表的插入操作按照需求,在链表的指定位置插入一个新节点。
③单链表的删除操作按照需求,删除链表中的指定位置的节点。
④单链表的遍历操作依次访问链表中的每一个节点,并输出其值。
⑤单链表的逆置操作将单链表中的节点顺序进行逆置。
⑥单链表的查找操作按照需求,在链表中查找指定值的节点。
3.2.7 单链表的修改操作按照需求,修改链表中指定位置的节点的值。
数据结构上机实验源代码栈的应用十进制数转换为八进制数,逆序输出所输入的数实验代码://stack.h,头文件class stack{public:stack();bool empty()const;bool full()const;error_code gettop(elementtype &x)const;error_code push(const elementtype x);error_code pop();private:int count;elementtype data[maxlen];};stack::stack(){count=0;}bool stack::empty()const{return count==0;}bool stack::full()const{return count==maxlen;}error_code stack::gettop(elementtype &x)const{if(empty())return underflow;else{x=data[count-1];return success;}}error_code stack::push(const elementtype x){if(full())return overflow;data[count]=x;count++;return success;}error_code stack::pop(){if(empty())return underflow;count--;return success;}//主程序#include<iostream.h>enum error_code{overflow,underflow,success};typedef int elementtype;const int maxlen=20;#include"stack.h"void read_write() //逆序输出所输入的数{stack s;int i;int n,x;cout<<"please input num int n:";cin>>n;for(i=1;i<=n;i++){cout<<"please input a num:";cin>>x;s.push(x);}while(!s.empty()){s.gettop(x);cout<<x<<" ";s.pop();}cout<<endl;}void Dec_to_Ocx(int n) //十进制转换为八进制{stack s1;int mod,x;while(n!=0){mod=n%8;s1.push(mod);n=n/8;}cout<<"the ocx of the dec is:";while(!s1.empty()){s1.gettop(x);cout<<x;s1.pop();}cout<<endl;}void main(){int n;// read_write();cout<<"please input a dec:";cin>>n;Dec_to_Ocx(n);}队列的应用打印n行杨辉三角实验代码://queue.hclass queue{public:queue(){count=0;front=rear=0;}bool empty(){return count==0;}bool full(){return count==maxlen-1;}error_code get_front(elementtype &x){if(empty())return underflow;x=data[(front+1)%maxlen];return success;}error_code append(const elementtype x){if(full())return overflow;rear=(rear+1)%maxlen;data[rear]=x;count++;return success;}error_code serve(){if(empty())return underflow;front=(front+1)%maxlen;count--;return success;}private:int count;int front;int rear;int data[maxlen];};//主程序#include<iostream.h>enum error_code{overflow,underflow,success};typedef int elementtype;const int maxlen=20;#include"queue.h"void out_number(int n) //打印前n行的杨辉三角{int s1,s2;int i;int j;int k;queue q;for(i=1;i<=(n-1)*2;i++)cout<<" ";cout<<"1 "<<endl;q.append(1);for(i=2;i<=n;i++){s1=0;for(k=1;k<=(n-i)*2;k++)cout<<" ";for(j=1;j<=i-1;j++){q.get_front(s2);q.serve();cout<<s1+s2<<" ";q.append(s1+s2);s1=s2;}cout<<"1 "<<endl;q.append(1);}}void main(){int n;cout<<"please input n:";cin>>n;out_number(n);}单链表实验实验目的:实验目的(1)理解线性表的链式存储结构。
《数据结构》实验报告姓名:学号:班级:学院:实验一单链表实验(一)实验目的1.理解线性表的链式存储结构。
2.熟练掌握动态链表结构及有关算法的设计。
3.根据具体问题的需要,设计出合理的表示数据的链表结构,并设计相关算法。
(二)实验任务编写算法实现下列问题的求解1.求链表中第i个结点的指针(函数),若不存在,则返回NULL。
2.在第i个结点前插入值为x的结点。
3.删除链表中第i个元素结点。
4.在一个递增有序的链表L中插入一个值为x的元素,并保持其递增有序特性。
5.将单链表L中的奇数项和偶数项结点分解开,并分别连成一个带头结点的单链表,然后再将这两个新链表同时输出在屏幕上,并保留原链表的显示结果,以便对照求解结果。
6.求两个递增有序链表L1和L2中的公共元素,并以同样方式连接成链表L3。
(三)主要仪器设备PC机,Windows操作平台,Visual C++(四)实验分析顺序表操作:定义一个顺序表类,该类包括顺序表的存储空间、存储容量和长度,以及构造、插入、删除、遍历等操作的方法(五)源程序头文件文件名:linklist.h#include<iostream>using namespace std;struct node{int data;node *next;};class list{public:list();int length()const{return count; //求链表长度}~list();void create(); //链表构建,以0为结束标志void output(); //链表输出int get_element(const int i)const; //按序号取元素node *locate(const int x) const; //搜索对应元素int insert(const int i,const int x); //插入对应元素int delete_element(const int i); //删除对应元素node *get_head(){return head; //读取头指针}void insert2(const int x);friend void SplitList(list L1, list&L2, list &L3);friend void get_public(list L1, list L2, list &L3);private:int count;node *head;};list::list(){head=new node;head->next=NULL;count=0;}void list::create() //链表构建,以0为结束标志{int x;cout<<"请输入当前链表,以0为结束符。
单链表的操作方法一:package ch02;(1)建立结点类Node.javapublic class Node {public Object data;//存放结点数据值public Node next;//存放后继结点//无参构造函数public Node(){ this(null,null);}//只有结点值的构造函数public Node(Object data){ this(data,null);}//带有节点值和后继结点的构造函数public Node(Object data,Node next){ this.data=data;this.next=next;}}(2)建立链表及操作LinkList.javapackage ch02;import java.util.Scanner;public class LinkList implements IList{public Node head;//单链表的头指针//构造函数初始化头结点public LinkList(){head=new Node();}//构造函数构造长度为n的单链表public LinkList(int n,boolean Order) throws Exception{ this();if(Order)create1(n); //头插法顺序建立单链表elsecreate2(n); //尾插法逆序建立单链表}//头插法顺序建立单链表public void create1(int n) throws Exception{Scanner sc=new Scanner(System.in);System.out.println("请输入结点的数据(头插法):”);for(int i=0;i<n;i++){insert(0,sc.next());}}//尾插法逆序建立单链表public void create2(int n) throws Exception{Scanner sc=new Scanner(System.in);System. out.println("请输入结点的数据(尾插法):");for(int i=0;i<n;i++){insert(length(),sc.next());}}//将链表置空public void clear(){head.data=null;head.next=null;}//判断链表是否为空public boolean isEmpty(){return head.next==null;}//返回链表长度public int length(){Node p=head.next;int length=0;while(p!=null){p=p.next;length++;//返回P不空长度length加1}return length;}//读取并返回第i个位置的数据元素public Object get(int i) throws Exception {Node p=head.next;int j;//从首结点开始向后查找,直到9指向第i个结点或者p为nullfor(j=0;j<i&&p!=null;j++){ p=p.next;}if(j>i||p==null)//i不合法时抛出异常throw new Exception("第"+i+”个数据元素不存在”);return p.data;}//插入乂作为第i个元素public void insert(int i, Object x) throws Exception{ Node p=head;int j=-1;//寻找第i个结点的前驱i-1while(p!=null&&j<i-1){p=p.next;j++;}if(j>i-l||p==null)//i不合法时抛出异常throw new Exception("插入位置不合法”);Node s=new Node(x);s.next=p.next;p.next=s;}//删除第i个元素public void remove(int i) throws Exception{ Node p=head;int j=-1;while(p!=null&&j<i-1){//寻找第i-1 个节点p=p.next;j++;}if(j>i-1||p.next==null)throw new Exception("删除位置不合法”);p.next=p.next.next;}//返回元素x首次出现的位序号public int indexOf(Object x) {Node p=head.next;int j=0;while(p!=null&&!p.data.equals(x)){p=p.next;j++;if(p!=null)return j;elsereturn -1;}public void display(){Node p=head.next;while(p!=null){if(p.next==null)System.out.print(p.data);elseSystem.out.print(p.data+"f );p=p.next;}}}(3)建立测试类Test.javappublic class test {public static void main(String[] args) throws Exception { // TODO Auto-generated method stubScanner sc=new Scanner(System.in);boolean or;int xz,xx;System.out.println("请选择插入的方法:0、头插法,1、尾插法");xz=sc.nextInt();if(xz!=0)or=true;elseor=false;System. out.println("请插入的结点的个数:”);xx=sc.nextInt();LinkList L=new LinkList(xx,or);System.out.println("建立的链表为:");L.display();System.out.println();System.out.println("链表的长度:"+L.length());System. out.println(”请输入查找的结点的数据:”);Object x=sc.next();int position=L.indexOf(x);System.out.println("结点的数据为:"+x+"的位置为:"+position); System. out.println("请输入删除的结点的位置:”);int sr=sc.nextInt();L.remove(sr);L.display();System.out.println();System.out.println("链表的长度:"+L.length()); }品P rob I em & J a vs d oc / Declaration Q Error Log 里Con sole-M、、■=:termin8ted> test [3] [Java Application] C U &ert\Ad im i n i st rat o r\Ap p Data\L o cs I请选择插入.的方法:0、头插法,lv星插法请插入的特点的个数:请愉入结点的颓据(尾插法):A B C E D F建立的旌表为;A+B T C+E T D+F链表的长度:6请输入查找的结点的数据:结点的数据为:E的位置为:3请输入删除的结点的位置,R+B T E+DW道表的长度:S方法二(引入get和set方法)Package sy;import java.util.Scanner;//单链表的结点类public class Node {private Object data; //存放结点值private Node next; //后继结点的引用public Node() { //无参数时的构造函数this(null, null);}public Node(Object data) { // 构造值为data 的结点this(data, null);}public Node(Object data, Node next) {//构造值为data 和next 的结点构造函数this.data = data;this.next = next;}public Object getData() { return data;}public void setData(Object data) {this.data = data;}public Node getNext() { return next;public void setNext(Node next) { this.next = next;}}//实现链表的基本操作类public class LinkList {Node head=new Node();//生成一个带头结点的空链表//根据输入的一系列整数,以0标志结束,用头插法建立单链表public void creat() throws Exception {Scanner sc = new Scanner(System.in); //构造用于输入的对象for (int x=sc.nextInt(); x!=0; x=sc.nextInt()) //输入若干个数据元素的值(以0结束) insert(0, x);//生成新结点,插入到表头}//返回带头结点的单链表中第i个结点的数据域的值。
#include 〈stdio.h>#include <malloc。
h>#include 〈stdlib.h>/*数据结构C语言版线性表的单链表存储结构表示和实现P28—31编译环境:Dev-C++ 4。
9。
9。
2日期:2011年2月10日*/typedef int ElemType;// 线性表的单链表存储结构typedef struct LNode{ElemType data; //数据域struct LNode *next;//指针域}LNode, *LinkList;// typedef struct LNode *LinkList;// 另一种定义LinkList的方法// 构造一个空的线性表Lint InitList(LinkList *L){/*产生头结点L,并使L指向此头结点,头节点的数据域为空,不放数据的。
void *malloc(size_t)这里对返回值进行强制类型转换了,返回值是指向空类型的指针类型.*/(*L)= (LinkList)malloc(sizeof(struct LNode) );if( !(*L))exit(0);// 存储分配失败(*L)-〉next = NULL;// 指针域为空return 1;}// 销毁线性表L,将包括头结点在内的所有元素释放其存储空间。
int DestroyList(LinkList *L){LinkList q;// 由于单链表的每一个元素是单独分配的,所以要一个一个的进行释放while(*L ){q = (*L)—〉next;free(*L );//释放*L = q;}return 1;}/*将L重置为空表,即将链表中除头结点外的所有元素释放其存储空间,但是将头结点指针域置空,这和销毁有区别哦。
不改变L,所以不需要用指针。
*/int ClearList( LinkList L ){LinkList p,q;p = L—〉next;// p指向第一个结点while( p ) // 没到表尾则继续循环{q = p—>next;free( p );//释放空间p = q;}L—>next = NULL; // 头结点指针域为空,链表成了一个空表return 1;}// 若L为空表(根据头结点L—〉next来判断,为空则是空表),则返回1,// 否则返回0.int ListEmpty(LinkList L){if(L—>next ) // 非空return 0;elsereturn 1;}// 返回L中数据元素个数。
数据结构--数组、单链表和双链表介绍以及双向链表数组:数组有上界和下界,数组的元素在上下界内是连续的。
数组的特点是:数据是连续的;随机访问速度快。
数组中稍微复杂⼀点的是多维数组和动态数组。
对于C语⾔⽽⾔,多维数组本质上也是通过⼀维数组实现的。
⾄于动态数组,是指数组的容量能动态增长的数组;对于C语⾔⽽⾔,若要提供动态数组,需要⼿动实现;⽽对于C++⽽⾔,STL提供了Vector。
单向链表:单向链表(单链表)是链表的⼀种,它由节点组成,每个节点都包含下⼀个节点的指针。
表头为空,表头的后继节点是"节点10"(数据为10的节点),"节点10"的后继节点是"节点20"(数据为10的节点),"节点20"的后继节点是"节点30"(数据为20的节点),"节点30"的后继节点是"节点40"(数据为10的节点),......删除"节点30"删除之前:"节点20" 的后继节点为"节点30",⽽"节点30" 的后继节点为"节点40"。
删除之后:"节点20" 的后继节点为"节点40"。
在"节点10"与"节点20"之间添加"节点15"添加之前:"节点10" 的后继节点为"节点20"。
添加之后:"节点10" 的后继节点为"节点15",⽽"节点15" 的后继节点为"节点20"。
单链表的特点是:节点的链接⽅向是单向的;相对于数组来说,单链表的的随机访问速度较慢,但是单链表删除/添加数据的效率很⾼。
数据结构实验报告_单链表数据结构实验报告——单链表一、实验目的1.掌握单链表的基本概念和原理。
2.了解单链表在计算机科学中的应用。
3.掌握单链表的基本操作,如插入、删除、遍历等。
4.通过实验,加深对理论知识的理解,提高编程能力。
二、实验内容1.实验原理:单链表是一种线性数据结构,由一系列节点组成,每个节点包含数据域和指针域。
其中,指针域指向下一个节点,最后一个节点的指针域指向空。
单链表的主要操作包括插入、删除、遍历等。
2.实验步骤:(1)创建一个单链表。
(2)实现插入操作,即在链表的末尾插入一个新节点。
(3)实现删除操作,即删除链表中的一个指定节点。
(4)实现遍历操作,即输出链表中所有节点的数据。
3.实验代码:下面是使用Python语言实现的单链表及其基本操作的示例代码。
class Node:def __init__(self, data):self.data = dataself.next = Noneclass LinkedList:def __init__(self):self.head = Nonedef insert(self, data):new_node = Node(data)if self.head is None:self.head = new_nodeelse:current = self.headwhile current.next is not None:current = current.nextcurrent.next = new_nodedef delete(self, data):if self.head is None:returnif self.head.data == data:self.head = self.head.nextreturncurrent = self.headwhile current.next is not None and current.next.data != data:current = current.nextif current.next is None:returncurrent.next = current.next.nextdef traverse(self):current = self.headwhile current is not None:print(current.data)current = current.next4.实验结果:通过运行上述代码,我们可以看到单链表的基本操作得到了实现。
# define LIST_INIT_SIZE 100 // 顺序表(默认的)的初始分配最大容量
# define LISTINCREMENT 10 // (默认的)增补空间量
typedef struct {
ElemType *elem; // 存储数据元素的一维数组
int length; // 线性表的当前长度
int listsize; // 当前分配的数组容量(以ElemType为单位)int incrementsize; // 增补空间量(以ElemType为单位)
} SqList;
void InitList_Sq( SqList &L, int maxsize=LIST_INIT_SIZE,
int incresize=LISTINCREMENT )
{ // 构造一个最大容量为maxsize的顺序表L
L.elem=(ElemType *)malloc(maxsize*sizeof(ElemType));
// 为顺序表分配一个最大容量为maxsize 的数组空间if(!L.elem) exit(1); // 存储分配失败
L.length=0; // 顺序表中当前所含元素个数为0
L.listsize=maxsize; // 该顺序表可以容纳maxsize个数据元素
L.incrementsize=incresize; // 需要时可扩容incresize个元素空间
}// InitList_Sq
int ListLength_Sq(SqList L)
{
return L.length;
}// ListLength_Sq
int LocateElem_Sq( SqList L, ElemType e)
{
for(int i=0;i<L. length;i++)
if(L.elem[i]==e) return i; // 找到满足判定的数据元素为第i 个元素
return -1; // 该线性表中不存在满足判定的数据元素
}//LocateElem_Sq
bool ListInsert_Sq(SqList &L, int i, ElemType e)
{ // 在顺序表L的第i个元素之前插入新的元素e,
// 若表中当前容量不足,则按预定义的增量扩容
int j;
if(i<0||i>L.length) return false; // i值不合法
if(L.length>=L.listsize) { // 当前存储空间已满,增补空间
L.elem=(ElemType *)realloc(L.elem,(L.listsize+L.incrementsize)*sizeof(ElemType));
if(!L.elem) exit(1); // 存储分配失败
L.listsize+=L.incrementsize; // 当前存储容量增加
}
for(j=L.length;j>i;j--) // 被插入元素之后的元素左移
L.elem[j]=L.elem[j-1];
L.elem[i]=e; // 插入元素e
L.length++; // 表长增1
return true;
}// ListInsert_Sq
bool ListDelete_Sq(SqList &L,int i, ElemType &e)
{ // 在顺序表L中删除第i个元素,并用e返回其值
int j;
if(i<0||i>=L.length) return false; // i值不合法
if(L.length<=0) return false; // 表空无数据元素可删
e=L.elem[i]; // 被删除元素的值赋给e for(j=i+1;j<=L.length-1;j++) // 被删除元素之后的元素前移
L.elem [j-1]=L.elem [j];
L.length--; // 表长减1
return true;
}// ListDelete_Sq
bool GetElem_Sq(SqList L,int i, ElemType &e)
{ // 取出顺序表L中第i个元素,并用e返回其值。
if(i<0||i>L.length) return false; // i值不合法
if(L.length<=0) return false; // 表空无数据元素可取
e=L.elem[i]; // 被取元素的值赋给e return true;
}// GetElem_Sq
void ListTraverse_Sq(SqList L)
{
int i;
for(i=0;i<L.length;i++)
cout<<setw(6)<<L.elem[i];
cout<<endl;
}// ListTraverse_Sq
void DestroyList_Sq(SqList &L)
{ // 释放顺序表L所占存储空间
free(L.elem);
L.listsize=0;
L.length=0;
}// DestroyList_Sq。