数据结构+单链表

(先贴代码,以后在施工。。)

单链表的初始化、头插法、尾插法、查找元素、删除、销毁。

#include <iostream>

using namespace std;
typedef struct listNode {
	int data;
	struct listNode* next;
}LinkNode,LinkList;

bool listInit(LinkList* &list) {  //构造一个空链表
	list = new LinkNode;

	if (!list) {
		return false; //生成失败
	}
	list->next = NULL;
	return true;
}

bool frontInsert(LinkList*& list,LinkNode *node) {   //头插法
	if (!list || !node) return false;

	node->next = list->next;
	list->next = node;
	return true;
}
  
//任意位置插法  确保正确的方法 
bool LinkInsert(LinkList* &L, int i, int &e)//单链表的插入
{ //在带头结点的单链表 L 中第 i 个位置插入值为 e 的新结点 
	int j; 
	LinkList *p, *s;
	p=L; j=0;
	while (p&&j<i-1) //查找第 i-1 个结点,p 指向该结点 
	{
		p=p->next; 
		j++; 
	}
	if (!p || j>i-1){
		//i>n+1 或者 i<1 
		return false; }
	s=new LinkNode; //生成新结点
	s->data=e; //将新结点的数据域置为 e 
	s->next=p->next; //将新结点的指针域指向结点 ai 
	p->next=s; //将结点 p 的指针域指向结点 s 
	return true; 
}

bool posInsert(LinkList*& list, LinkNode* node, int pos) {
	//找到前一个节点
	LinkNode* temp = list;
	if (!list || !node) return false;
	// s 1 2 3
	while (pos > 1 && temp->next) {
		temp = temp->next;
		pos--;
	}

	if (pos > 1 || pos <= 0) {
		cout << "位置不存在" << endl;
		return false;
	}
	
	node->next = temp->next;
	temp->next = node;

	return true;
}

bool backInsert(LinkList*& list, LinkNode* node) {  //尾插法
	if (!list || !node) return false;
	LinkNode* temp = list;
	while (temp->next) {
		temp = temp->next;
	}

	node->next = temp->next;
	temp->next = node;

	return true;
}

bool linkGetElem(LinkList*& list, int pos, int& e) {
	int index; //对遍历的节点进行计数

	LinkNode* temp = list->next;

	if (!list || !list->next) return false;
	index = 1;

	while (temp && index < pos) {
		temp = temp->next;
		index++;
	}

	if (!temp || index > pos) {   //pos > index  || pos <=0
		cout << "位置不合法" << endl;
		return false;
	}

	e = temp->data;
	return true;
}

bool linkFindElem(LinkList*& list, int elem,int &index) {
	if (!list || !list->next) {
		index = 0;
		return false;
	}
	
	LinkNode* temp;
	temp = list->next;
	index = 1;
	while (temp && temp->data != elem) {
		temp = temp->next;
		index++;
	}

	if (!temp) {
		index = -1;
		return false;
	}

	return true;
}

bool deleteElemPos(LinkList*& list, int pos) {
	if (!list || !list->next) return false;
	//找到前一个节点的位置
	LinkNode* temp;
	temp = list;
	int index = 0;

	while (temp->next && (index < pos - 1)) {
		temp = temp->next;
		index++;
	}

	if (!temp->next || (index > pos - 1)) {
		return false;
	}

	LinkNode* q = temp->next;
	temp->next = q->next;
	delete q;
	return true;
}

void LinkDestroy(LinkList*& L) //单链表的销毁 
{ //定义临时节点 p 指向头节点 
	LinkList *p = L; 
	cout<<"销毁链表!"<<endl; 
	while(p) {
		L=L->next; //L 指向下一个节点 
		cout<<"删除元素: "<<p->data<<endl;
		delete p; //删除当前节点 
		p=L; //p 移向下一个节点 
	} 
}

bool listPrint(LinkList* &list) {
	LinkNode* p = list->next;
	if (!list) cout << "链表为空";
	
	while (p) {
		cout << p->data << "\t";
		p = p->next;
	}
	cout << endl;
	return true;
}

int main(void) {
	LinkList* L = NULL;
	listInit(L);  //初始化一个空链表
	//使用头插法
	int num;

	cout << "使用头插法插入元素" << endl;
	cout << "请输入元素个数:" ;
	cin >> num;
	LinkNode* node = NULL;
	while (num-- > 0) {
		node = new LinkNode;
		cout << "请输入节点数据:";
		cin >> node->data;
		frontInsert(L, node);	
	}
	cout << endl;


	cout << "使用尾插法插入元素" << endl;
	cout << "请输入元素个数:";
	cin >> num;
	while (num-- > 0) {
		node = new LinkNode;
		cout << "请输入节点数据:";
		cin >> node->data;
		backInsert(L, node);
	}

	cout << "使用位置法插入元素" << endl;
	cout << "请输入元素个数:";
	cin >> num;
	while (num-- > 0) {
		node = new LinkNode;
		cout << "请输入节点数据:";
		cin >> node->data;
		cout << "请输入插入位置:";
		int pos;
		cin >> pos;
		posInsert(L, node, pos);
	}

	cout << endl;
	int elem = 0;
	if (linkGetElem(L, 2, elem)) {
		cout << "获取成功 " << elem<<endl;
	}

	listPrint(L);

	LinkDestroy(L);
	system("pause");
	return 0; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黯黯黯然了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值