C++数据结构-实验1-线性表、栈和队列

数据结构实验1

1.线性表的链表实现

(1) 用随机函数生成10个3位整数(100~999),把这些整数存于链表中;
(2) 输出链表的内容;
(3) 读入一个整数,查看该整数是否在表中,若在,输出其位置(首位置为1);
(4) 读入一个整数,以及要插入的位置,把该整数插入到链表中,输出链表的内容(要求判断输入的位置是否合理);
(5) 读入一个整数,若该整数在链表里,删除该整数,输出链表的内容;
(6) 把链表的内容翻转,输出链表的内容。

#include<iomanip>
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct LNode {
	int data; //数据
	LNode* next; //指向下一个节点的指针
}*Head;
void CreatList(LNode*& Head, int n) {
	//用随机函数生成10个3位整数(100~999),把这些整数存于链表中 
	LNode* L = Head;
	for (int i = 1; i <= n; i++) {
		LNode* temp = new LNode();
		temp->data = rand() % 900 + 100;
		L->next = temp;
		L = temp;
	}
	L->next = NULL;
}
int LengthList(LNode* Head) {
	//返回链表长度 
	LNode* L = Head->next;  //保存和保证头节点,从头节点的下一个开始存储数据,以便于找到该链表
	int length = 0;
	while (L != NULL) {
		L = L->next; length++;
	}
	return length;
}
void ShowList(LNode* Head) {
	//输出链表的内容
	LNode* L = Head->next;
	while (L != NULL) {
		cout << L->data << " ";
		L = L->next;
	}
	cout << endl;
}
int FindValue(LNode* Head, int value) {
	//读入一个整数,查看该整数是否在表中,若在,输出其位置(首位置为1)
	LNode* L = Head;
	int index = 1;
	while (L != NULL && L->data != value) {
		index++; L = L->next;
	}
	return index <= LengthList(Head) ? index : -1;
}
int InsertList(LNode* Head, int value, int position) {
	//读入一个整数,以及要插入的位置,把该整数插入到链表中,
	//输出链表的内容(要求判断输入的位置是否合理)
	if (position<1 || position>LengthList(Head) + 1)return -1;
	LNode* L = Head;
	int index = 1;
	while (index < position) {
		index++; L = L->next;
	}
	LNode* temp = new LNode();
	temp->data = value;
	temp->next = L->next;
	L->next = temp;
	return 1;
}
void DeleteNode(LNode* Head, int value) {
	//读入一个整数,若该整数在链表里,删除该整数,输出链表的内容
	LNode* L = Head;
	while (L != NULL && value != L->data)L = L->next;
	if (L != NULL) {
		L->next = L->next->next;
		L = L->next;
	}
	ShowList(Head);
}
void ReverseList(LNode* Head) {
	//把链表的内容翻转,输出链表的内容。
	if (Head->next == NULL || Head->next->next == NULL)return;
	//定义l,r,temp三个临时结点,通过temp交换l,r的next方向,判断该节点的下一个节点是否为空,如果为空则代表到该链表的尽头,跳出遍历
	LNode* temp = NULL;
	LNode* l = Head->next, * r = l->next;     
	while (r != NULL) {
		temp = r->next;
		r->next = l;
		l = r;
		r = temp;
	}
	Head->next->next = NULL; //头节点的下一个节点才是存储数据的节点,翻转后,第一个节点指向空
	Head->next = l;  //此时的r已经是NULL 
	ShowList(Head);
}
int main() {
	Head = new LNode();//注意初始化头指针Head 
	Head->next = NULL;
	CreatList(Head, 10);
	ShowList(Head);
	cout << LengthList(Head) << endl;
	//cout<<FindValue(Head,100);
	ReverseList(Head);
}

2、栈的链式存储结构实现
(1) 用随机函数生成10个3位整数(100~999),把这些整数应用入栈操作存于堆栈中,在入栈接口处设置断点①,按“F5”启动调试,按“F10”逐句执行,直到数据全部入栈。程序暂停时观察栈顶数据和栈顶位置;
(2) 应用出栈操作输出堆栈的内容,在出栈接口处设置断点②,按“F5”启动调试,按“F10”逐句执行,直到所有数据完全出栈,程序暂停时观察栈顶数据和栈顶位置的变化。

#include<iomanip>
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct StackNode {
	int data;
	StackNode* next;  //线性链表存储栈的数据
};
struct StackList {
	StackNode* top; //头指针 
	void Push(int data) {
		StackNode* NewNode = new StackNode(); //创建新节点,后插入新节点
		NewNode->data = data;
		//保证新节点的指向旧的头节点,以便于pop弹出的时,头指针top可以回溯到旧节点
		NewNode->next = top;  
		top = NewNode;   //移动头指针
	}
	// 用随机函数生成10个3位整数(100~999),把这些整数应用入栈操作存于堆栈中,
	void RandInit() {
		for (int i = 0; i < 10; i++) {
			int number = rand() % 900 + 100;
			this->Push(number);
			cout << number << " ";
		}
		cout << endl;
	}
	int Pop() {   //弹出栈中最上面的头节点
		int tempDate = top->data;
		StackNode* tempNode = new StackNode();
		tempNode = top;
		top = top->next;
		delete tempNode;
		return tempDate;
	}
	int Top() { return top->data; }   //返回栈中最上面的头节点
	bool isEmpty() { return top == NULL ? 1 : 0; }  //判断栈是否为空,可用于避免栈空时进行pop弹出操作
};
int main() {
	StackList* stack = new StackList();
	cout << "输出创建新节点的顺序:" << endl;
	stack->RandInit();
	cout << "输出栈弹出节点的顺序:" << endl;
	while (!stack->isEmpty()) 
		cout << stack->Pop() << " ";
	
}

3、队列的链式存储结构的实现
(1) 用随机函数生成10个3位整数(100~999),把这些整数应用入队操作存于队列中;
(2) 应用遍历操作输出队列的内容;
(3) 把队列的内容翻转,应用出队操作输出队列的内容。

#include<iomanip>
#include<stdio.h>
#include<iostream>
using namespace std;
struct QueueNode {
public:
	int data;
	QueueNode() { this->next = NULL; }
	QueueNode* next;
	
};
struct Queue {
public:
	QueueNode* begin, * end;  //设置头指针begin,尾指针end,左开右闭,移动end指针存储数据
	Queue() {
		//定义构造函数,初始化指针begin,尾指针end
		begin = new QueueNode; 
		end = begin;
		end->next = NULL;
	}
	//返回第一个节点的数据
	int front() {
		return begin->next->data;
	}
	//返回最后一个节点的数据
	int back() {
		return end->data;
	}
	//判断队列是否为空,由于初始化时,从首指针begin的下一个节点开始存储数据,只需判断头尾是否相等就可以判断是否为空
	bool isEmpty() {
		return begin==end? 1 : 0;
	}
	//往队列中添加新节点数据
	void push(int data) {
		QueueNode* tempNode = new QueueNode();
		tempNode->data = data;
		end->next = tempNode;
		end = tempNode;
		end->next = NULL;
	}
	//弹出队列首个节点
	int pop() {
		if (isEmpty())return -1;
		QueueNode* tempNode = this->begin->next;
		//移动队列中第一个有效的节点位置
		this->begin->next = this->begin->next->next;
		//判断该节点是否是唯一个的节点,若是则弹出后队列为空,尾指针end=头指针begin
		if (tempNode == this->end) this->end = this->begin;
		return tempNode->data;
	}
	//应用遍历操作输出队列的内容;
	void show() {
		QueueNode* tempNode = this->begin->next;
		while (tempNode!=NULL) {
			cout << tempNode->data << " ";
			tempNode = tempNode->next;
		}
		cout << endl;
	}
	//用随机函数生成10个3位整数(100~999),把这些整数应用入队操作存于队列中;
	void RandInit() {
		for (int i = 0; i < 10; i++) {
			int number = rand() % 900 + 100;
			this->push(number);
			cout << number << " ";
		}
		cout << endl;
	}
	//把队列的内容翻转,应用出队操作输出队列的内容。
	void reverse() {
		if (this->begin->next == NULL)return;
		if (this->begin->next->next == NULL) {
			cout << this->begin->next->data << endl; return;
		}
		end = begin->next;
		QueueNode* tempNode, * l = this->begin->next, * r = l->next;
		for (; r != NULL; r = tempNode) {
			tempNode = r->next;
			r->next = l;
			l = r;
		}
		begin->next->next = NULL;
		begin->next = l;
		show();
	}
};
int main() {
	Queue* queue = new Queue(); 
	//queue->begin->next = queue->end->next = NULL;
	cout << "输出随机创建的队列:" << endl;
	queue->RandInit();
	cout << "输出队列的首元素:" << endl;
	cout << queue->front() << endl;
	cout << "输出队列的尾元素:" << endl;
	cout << queue->back() << endl;

	cout << "输出翻转后的队列:" << endl;
	queue->reverse();
	cout << "输出队列翻转后的首元素:" << endl;
	cout << queue->front() << endl;
	cout << "输出队列翻转后的尾元素:" << endl;
	cout << queue->back() << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值