河南大学数据结构实验-单链表的操作

计算机与信息工程学院实验报告

姓名:杨馥瑞  学号:2212080042 专业:数据科学与大数据技术 

年级:2022

课程:数据结构  主讲教师:袁彩虹老师 辅导教师:_______                 

实验时间:2024年 __月 ___日 __午__时至__时,实验地点_512_

实验题目:单链表的操作

实验目的:本实验项目可以支撑“实验目标1. 熟练掌握线性表(包括顺序表、链表、栈和队列)的存储方式及其操作实现。”。

本实验通过验证方式引导学生深入理解单链表的逻辑结构、物理结构等概念,掌握单链表的基本操作,为后续学习打下基础,以便更好地达成后续更高层次的课程目标。

实验环境(硬件和软件)Dev c++ & windows11 x64

实验内容:

(1)编程实现单链表的基本操作:建立单链表,查找单链表,插入单链表,删除单链表;

(2)采用单链表结构编程实现:两个有序单链表的归并运算。

实验步骤:

实验内容(1):

结点的定义

链表的初始化

链表的打印

得到链表长度

链表的插入

链表的删除

链表的按值查找和按位查找

实验内容(2):

先初始化list1 list2这两个链表

接着进行归并操作

最后释放内存

实验数据记录:

第一个为list1 第二个为list2 第三个是上面二者归并的结果

问题讨论:

感觉没有很多的问题,因为之前写过挺多的

编程导航算法通关村(链表)leval-1 – Yangfree的可达鸭 (yangfreeyyds.club)

之前博客上写过总结一些了

感觉最需要注意的是头节点要保留,别总是用着它去next,那就回不来了哈哈

其次就是c的指针大多是用结构体来写的,但是像是py,java,c++更多是用类来写的,比如

class ListNode:

def __init__(self, val=0, next=None):

self.val = val

self.next = next

listnode = ListNode(1)

个人还是更喜欢用类来替代结构体作为结点。

源代码:

#include<iostream>
#include <cstdlib>
#define ElemType int

using namespace std;

typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode; 

struct LNode* initLink(){//链表的创建 
	//创建头指针
	struct LNode* head = NULL;
	//创建头节点 
	struct LNode* temp = (struct LNode*)malloc(sizeof(struct LNode));
	temp->data = 0;
	temp->next = NULL;
	//头指针指向头节点
	head = temp;
	return head;
	 
}

void printLink(struct LNode* head){//打印链表 
	
	struct LNode* temp = head;//敢直接用head遍历的就是头铁
	while(temp){
		printf("%d ",temp->	data);
		temp = temp->next;
	}
	printf("\n");
} 

int getLength(struct LNode* head){//得到链表长度 
	struct LNode* temp = head;
	int length = 0;
	while(temp){
		length++;
		temp = temp->next;
	} 
	return length;
}

struct LNode* insertNode(struct LNode* head, struct LNode* nodeInsert, int position) {//插入 
    if (head == NULL) {
        printf("空链表无法插入\n");
        return nodeInsert;
    }
    int size = getLength(head);
    if (position > size + 1 || position < 1) {
        printf("位置参数越界\n");
        return head;
    }
    // 插入节点到头部
    if (position == 1) {
        nodeInsert->next = head;
        head = nodeInsert;
        return head;
    }

    struct LNode* pNode = head;
    int count = 1;
    // 遍历链表,找到插入位置的前一个节点
    while (count < position - 1) {
        pNode = pNode->next;
        count++;
    }
    nodeInsert->next = pNode->next;
    pNode->next = nodeInsert;

    return head;
}

struct LNode* deleteNode(struct LNode*head, int position) {//删除 
    if (head == NULL) {
        return NULL;
    }
    int size = getLength(head);
    if (position > size || position < 1) {
        printf("输入的参数有误\n");
        return head;
    }
    if (position == 1) {
        struct LNode*curNode=head;
        head= head->next;
        free(curNode);
        return head;
    } else {
        struct LNode* cur = head;
        int count = 1;
        while (count < position - 1) {
            cur = cur->next;
            count++;
        }
        struct LNode*tmp = cur->next;
        cur->next = tmp->next;
        free(tmp);
        return head;
    }
}

int LocateElem(struct LNode* head,int val){//按照值查找位置(非贪婪)
	struct LNode* pNode = head;
	int count = 1;
	while(pNode){
		if(pNode->data == val){
			return count;
		}
		pNode=pNode->next;
		count++;
	}
	printf("not find");
	return -1;
}
int GetElem(struct LNode* head,int position){//按照位置查找值 
	struct LNode* pNode = head;
	int count = 1;
	while(pNode){
		count++;
		pNode = pNode->next;
		if(count>=position){
			return pNode->data;
		}
	} 
	
} 
int main() {
	//有序链表list1 
    struct LNode* list1 = NULL;
    struct LNode* p1 = (struct LNode*)malloc(sizeof(struct LNode));
    p1->data = 2;
    struct LNode* p2 = (struct LNode*)malloc(sizeof(struct LNode));
    p2->data = 6;
    struct LNode* p3 = (struct LNode*)malloc(sizeof(struct LNode));
    p3->data = 9;
    struct LNode* p4 = (struct LNode*)malloc(sizeof(struct LNode));
    p4->data = 12;
    struct LNode* p5 = (struct LNode*)malloc(sizeof(struct LNode));
    p5->data = 14;
    struct LNode* p6 = (struct LNode*)malloc(sizeof(struct LNode));
    p6->data = 16;
    printf("create list:\n");

    list1 = initLink();
    list1 = insertNode(list1, p1, 1);
    list1 = insertNode(list1, p2, 2);
    list1 = insertNode(list1, p3, 3);
    list1 = insertNode(list1, p4, 4);
    list1 = insertNode(list1, p5, 5);
    list1 = insertNode(list1, p6, 6);
    deleteNode(list1,7);
    printLink(list1);
    int length1 = getLength(list1);
    printf("list1 length: %d\n", length1);
    
    //有序链表list2 
    struct LNode* list2 = NULL;
    struct LNode* p7 = (struct LNode*)malloc(sizeof(struct LNode));
    p7->data = 1;
    struct LNode* p8 = (struct LNode*)malloc(sizeof(struct LNode));
    p8->data = 5;
    struct LNode* p9 = (struct LNode*)malloc(sizeof(struct LNode));
    p9->data = 19;
    
    printf("create list:\n");

    list2 = initLink();
    list2 = insertNode(list2, p7, 1);
    list2 = insertNode(list2, p8, 2);
    list2 = insertNode(list2, p9, 3);
    deleteNode(list2,4);
    printLink(list2);
    int length2 = getLength(list2);
    printf("list2 length: %d\n", length2);
    
    struct LNode* add = (struct LNode*)malloc(sizeof(struct LNode)); // 虚拟头节点
	struct LNode* head = add;
	add->data = -1;
	add->next = NULL;

	struct LNode* temp1 = list1;
	struct LNode* temp2 = list2;
	while (temp1 != NULL && temp2 != NULL) {
    	if (temp1->data <= temp2->data) {
        	add->next = temp1;
        	temp1 = temp1->next;
    	} else {
        	add->next = temp2;
        temp2 = temp2->next;
    }
    add = add->next;
	}

	if (temp1 != NULL) {
    	add->next = temp1;
	}

	if (temp2 != NULL) {
    	add->next = temp2;
	}
    printf("归并后的链表为:\n");
	printLink(head->next);

	free(head);
    free(p1);
    free(p2);
    free(p3);
    free(p4);
    free(p5);
    free(p6);
    free(p7);
    free(p8);
    free(p9);
    return 0;
}
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值