数据结构(6)

实验步骤:

任务一:

编写算法实现带头结点单链表的就地逆置,即利用原带头结点单链表的结点空间把元素序列 a0,al,......,an-i 逆置为 an-1,......,al, a0

程序参数设计 定义了一个带头结点的单链表结构体,并提供了初始化、尾部插入、打印、就地逆置和释放链表的函数。在主函数中,首先初始化链表,然后添加一些元素,打印原始链表,执行就地逆置,最后打印逆置后的链表并释放链表的空间。

代码如下:

cpp 复制代码
#include<stdio.h>
#include<malloc.h>
typedef int DataType;
#include "LinList.h"
int main(void){
	SLNode*head,*p,*q,*temp;
	int i,j,x;
	ListInitiate(&head);
	for(i=0;i<10;i++)
		ListInsert(head,i,i+1);
	printf("原来链表:"); 
	for(i=0;i<ListLength(head);i++){
		ListGet(head,i,&x);
		printf("%d  ",x);
	}
	printf("\n");
	for(i=1;i<=ListLength(head)-1;i++){
		temp=head;
		p=head->next;
		q=p->next;
		for(j=1;j<=ListLength(head)-i&&q!=NULL;j++){
			temp->next=q;
			temp=q;
			p->next=q->next;
			q->next=p;
			q=p->next;
		}
	}		
	printf("当前链表:");
	for(i=0;i<ListLength(head);i++){
		ListGet(head,i,&x);
		printf("%d  ",x);
	}
	Destroy(&head);
}
头文件:

typedef struct Node{
	DataType data;
	struct Node *next;
}SLNode;
void ListInitiate(SLNode**head){
	*head=(SLNode *)malloc(sizeof(SLNode));
	(*head)->next=NULL;
}
int ListLength(SLNode *head){
	SLNode *p=head;
	int size=0;
	while(p->next!=NULL){
		p=p->next;
		size++;
	}
	return size;
}
int ListInsert(SLNode *head,int i,DataType x){
	SLNode *p,*q;
	int j;
	p=head;
	j=-1;
	while(p->next!=NULL&&j<i-1){
		p=p->next;
		j++;
	}
	if(j!=i-1){
		printf("插入元素位置参数错!");
		return 0;
	}
	q=(SLNode *)malloc(sizeof(SLNode));
	q->data=x;
	q->next=p->next;
	p->next=q;
	return 1;
}
int ListDelete(SLNode *head,int i,DataType *x){
	SLNode *p,*s;
	int j;
	p=head;
	j=-1;
	while(p->next!=NULL&&p->next->next!=NULL&&j<i-1){
		p=p->next;
		j++;
	}
	if(j!=i-1){
		printf("删除元素位置参数错!");
		return 0;
	}
	s=p->next;
	*x=s->data;
	p->next=p->next->next;
	free(s);
	return 1;
}
int ListGet(SLNode *head,int i,DataType *x){
	SLNode *p;
	int j;
	p=head;
	j=-1;
	while(p->next!=NULL&&j<i){
		p=p->next;
		j++;
	}
	if(j!=i){
		printf("取出元素位置参数错!");
		return 0;
	}
	*x=p->data;
	return 1;
}
void Destroy(SLNode **head){
	SLNode *p,*p1;
	p=*head;
	while(p!=NULL){
		p1=p;
		p=p->next;
		free(p1);
	}
	*head=NULL;
}

任务二:

设计循环单链表。要求:

(1)循环单链表的操作,包括初始化,求元素个数,插入、删除、取元素。

(2) 设计一个测试主函数验证所设计循环单链表的正确性。

程序参数分析 定义 Node 结构体来表示循环单链表的结点,头结点的 next 指针指向链表的首结点,而最后一个结点的 next 指针指向头结点,形成循环。程序提供了初始化、求元素个数、插入、删除、取元素、打印、释放链表空间等函数。在主函数中,我们演示了插入、删除、获取元素、打印链表长度和释放链表的操作。

代码如下:

cpp 复制代码
#include<stdio.h>   
#include<malloc.h>
typedef int DataType;
#include"LinListO.h"
int main(void){
    SLNode *head,*p;
    int i,x;
    ListInitiate(&head);
    for(i=0;i<10;i++){
        ListInsert(head,i,i+1);
    }
   	//ListDelete(head,4,&x);
   	printf("链表中的元素:");
    for(i=0;i<ListLength(head);i++){
        ListGet(head,i,&x);
        printf("%d   ",x);
    }
    //printf("\n%d ",head->next->data);
    //printf("\n%d ",head->next->next->next->next->next->next->data);
    int j=-1;
    p=head;
    while(p->next!=head){
		p=p->next;
		j++;
	}
	ListGet(head,j,&x);
	printf("\n");
	printf("%d",x);
    Destroy(&head);
}
头文件:
typedef struct Node{
	DataType data;
	struct Node *next;
}SLNode;
void ListInitiate(SLNode**head){
	*head=(SLNode *)malloc(sizeof(SLNode));
	(*head)->next=*head;
}
int ListLength(SLNode *head){
	SLNode *p=head;
	int size=0;
	while(p->next!=head){
		p=p->next;
		size++;
	}
	return size;
}
int ListInsert(SLNode *head,int i,DataType x){
	SLNode *p,*q;
	int j;
	p=head;
	j=-1;
	while(p->next!=head&&j<i-1){
		p=p->next;
		j++;
	}
	if(j!=i-1){
		printf("插入元素位置参数错!");
		return 0;
	}
	q=(SLNode *)malloc(sizeof(SLNode));
	q->data=x;
	q->next=p->next;
	p->next=q;
	return 1;
}
int ListDelete(SLNode *head,int i,DataType *x){
	SLNode *p,*s;
	int j;
	p=head;
	j=-1;
	while(p->next!=head&&p->next->next!=head&&j<i-1){
		p=p->next;
		j++;
	}
	if(j!=i-1){
		printf("删除元素位置参数错!");
		return 0;
	}
	s=p->next;
	*x=s->data;
	p->next=p->next->next;
	free(s);
	return 1;
}
int ListGet(SLNode *head,int i,DataType *x){
	SLNode *p;
	int j;
	p=head;
	j=-1;
	while(p->next!=head&&j<i){
		p=p->next;
		j++;
	}
	if(j!=i){
		printf("取出元素位置参数错!");
		return 0;
	}
	*x=p->data;
	return 1;
}
void Destroy(SLNode **head){
	SLNode *p,*p1;
	p=*head;
	while(p!=NULL){
		p1=p;
		p=p->next;
		free(p1);
	}
	*head=NULL;
}
相关推荐
炸膛坦客4 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
延凡科技9 小时前
多场景落地复盘:端边云架构无人机智能巡检系统设计与实践
大数据·数据结构·人工智能·科技·架构·无人机·能源
程序员Rock11 小时前
上位机开发-MODBUS面试常见问题
c语言·c++·面试·职场和发展·上位机
皓月斯语14 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
春日见15 小时前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表
萌动的小火苗15 小时前
嵌入式开发中的栈与队列:任务调度为什么依赖数据结构
数据结构·c++·单片机·嵌入式硬件
叩码以求索15 小时前
统计按位或能得到最大值的子集数目(一)
数据结构·算法
tachibana216 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
星恒随风16 小时前
C++ STL 栈详解:stack 的使用、经典题目与简单模拟实现
开发语言·数据结构·c++·笔记·学习
txzrxz16 小时前
单调队列讲解
数据结构·c++·算法·单调队列