链表

#include<iostream>
#include<string>
#include<random>
using namespace std;
#define Type int
#define null NULL

struct nodeone{   //单向链表
	Type data;
	nodeone *next;
};
struct nodetwo {   //双向循环链表
	Type data;
	nodetwo*pre;
	nodetwo*next;
};

void initNode(nodeone *&L, int n) {   //初始化尾插法
	nodeone *q = L;
	nodeone *p = null;
	for (int i = 0; i < n; i++) {
		p = new nodeone;
		p->data = rand() % 100 + 1;
		q->next = p;
		q = p;
	}
	p->next = null;
}

void initNodeT(nodeone *&L, int n) {   //初始化头插法
	L = new nodeone;
	L->next = null;
	int da = 0;
	cout << "请输入一组有序数据:" << endl;
	for (int i = 0; i < n; i++) {
		nodeone *p = new nodeone;
		cin >> da;
		p->data = da;
		p->next = L->next;
		L->next = p;
	}
}

void initNode(nodetwo *&L, int n) {   
	L = new nodetwo;
	L->next = null;
	nodetwo *p = L;
	nodetwo *q = null;
	for (int i = 0; i < n; i++) {
		q = new nodetwo;
		q->data = rand() % 100 + 1;
		q->pre = p;
		p->next = q;
		p = q;
	}
	q->next = L;
	L->pre = q;
}

void insertNode(nodeone *&L, Type da, int position) {   //插入节点到具体位置
	nodeone *q = new nodeone;
	q->data = da;
	q->next = null;
	nodeone *p = L;
	for (int i = 0; i < position - 1; i++) {
		p = p->next;
	}
	q->next = p->next;
	p->next = q;
}
void insertNode(nodetwo *&L, Type da, int position) {
	nodetwo *p = L;
	nodetwo *q = new nodetwo;
	q->data = da;
	q->pre = null;
	q->next = null;
	for (int i = 0; i < position - 1; i++) {
		p = p->next;
	}
	q->next = p -> next;
	q->pre = p;
	p->next->pre = q;
	p->next = q;
}
Type getElem(nodeone *&L, int position) {   //返回固定位置的数据
	Type data;
	nodeone *p = L -> next;
	for (int i = 0; i < position - 1; i++) {
		p = p->next;
	}
	data = p->data;
	return data;
}
Type getElem(nodetwo *&L, int position) {
	Type data;
	nodetwo *p = L->next;
	for (int i = 0; i < position - 1; i++) {
		p = p->next;
	}
	data = p->data;
	return data;
}
void deleNode(nodeone *&L, int position) {        //删除固定位置的结点
	nodeone *p = L;
	for (int i = 0; i < position - 1; i++) {
		p = p->next;
	}
	p->next = p->next->next;
	p = null;
	delete p;
}
void deleNode(nodetwo *&L, int position) {
	nodetwo *p = L;
	for (int i = 0; i < position - 1; i++) {   //指向删除位置的前一个元素
		p = p->next;
	}
	p->next->next->pre = p;
	p->next = p->next->next;
	p = null;
	delete p;
}
void destroyList(nodeone *&L) {   //销毁
	while (L) {
		nodeone *p = L;
		L = L->next;
		delete p;
		p = L;
	}
	L = null;
}
void destroyList(nodetwo *&L) {
	nodetwo *p = L;
	while (L != p){
		nodetwo *p = L;
		L = L->next;
		delete p;
		p = L;
	}
	L = null;
	delete L;
}

void initArr(nodeone *&L) {
	cout << "请输入数据长度:" << endl;
	int num = 0;
	cin >> num;
	initNodeT(L, num);
}

nodeone* dealArr() {      
	nodeone *one = new nodeone, *two = new nodeone;
	one->next = null;
	two->next = null;

	initArr(one);
	initArr(two);
	nodeone* three = new nodeone;
	three->next = null;
	nodeone *tem = three;
	nodeone *q1 = null;
	one = one->next;
	two = two->next;
	while (one || two) {
		if (one && two) {
			q1 = new nodeone;
			if (one->data > two->data) {
				q1->data = two->data;
				two = two->next;
				tem->next = q1;
				tem = q1;      //随时更新tem位置,保持同步
			}
			else{
				q1->data = one->data;
				one = one->next;
				tem->next = q1;
				tem = q1;
			}
		}
		else {
			if (one) {
				tem->next = one;
				one = null;
			}
			if (two) {
				tem->next = two;
				two = null;
			}
		}
	}
	return three;
}

int main() {
	cout << "请输入两组整数数据:" << endl;
	nodeone *result = dealArr();
	nodeone *p = result->next;
	while (p) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
	nodetwo *t = new nodetwo;
	t->next = null;
	int num = 0;
	cout << "请输入要新建数据的长度:" << endl;
	cin >> num;
	initNode(t, num);
	int p1 = 0, p2 = 0;
	cout << "请输入要删除的结点的位置:" << endl;
	cin >> p1;
	deleNode(t, p1);
	cout << "请输入要插入0的结点的位置:" << endl;
	cin >> p2;
	insertNode(t, 0, p2);
	int c = 0;
	cout << "请输入需要返回位置的数据的值:" << endl;
	cin >> c;
	int data = getElem(t, c);
	cout << "该值为:" << data << endl;

	cout << "处理后的数据为:" << endl;

	nodetwo *r = t->next;
	while (r != t) {
		cout << r->data << " ";
		r = r->next;
	}
	cout << endl;
	destroyList(t);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值