单链表增删改查C++实现

单链表增删改查C++实现,第1张

单链表增删改查C++实现

单链表增删改查C++实现
  • 简介
  • link头文件(link.h)
  • main函数(link.cpp)

简介

此单链表摈弃了传统的结构体实现,完全采用面向对象,包含了结点类和链表类。

link头文件(link.h)
#pragma once
#include
#include
using namespace std;
class Node		//结点类
{
public:
	string name;
	long id;
	int points;
	Node* next;
};
class linkList		//链表类
{	
public:
	Node* head;		//头指针
	int size;

	linkList() 
	{
		head = new Node;
		head->name = "head";
		head->id = 0;
		head->points = 0;
		head->next = NULL;
		size = 0;
	}
	~linkList()
	{
		delete head;
		cout << "The link was deleted!" << endl;
	}
	int createlink()		//创建链表
	{
		int x;
		cout << "How much node do you want input?" << endl;
		cin >> x;
		if (x <= 0)
		{
			cout << "errorn" << endl;
			return 0;
		}
		size = x;
		Node* pnew = NULL;
		Node* ptemp = this->head;
		for (int i = 0; i < x; i++)
		{
			pnew = new Node;
			pnew->next = NULL;
			cout << "input the " << i + 1 << "th data:" << endl;
			cin>>pnew->name>>pnew->id>>pnew->points;
			ptemp->next = pnew;
			ptemp = pnew;
		}
		cout << "ok!n" << endl;

	}
	void showlink()		//展示完整链表
	{
		cout << "This is a " << size << " node's link:n" << endl;
		Node* L = this->head->next;
		for (int i = 0; i < size; i++)
		{
			cout << L->name << "  " << L->id << "  " << L->points;
			L = L->next;
			cout << "n" << endl;
		}
	}
	int addNode()		//增加结点
	{
		cout << "Select a location:n" << endl;
		int x; cin >> x;
		if (x <= 0 || x > size)
		{
			cout << "Out of the link orange!n" << endl;
			return 0;
		}
		Node* ptemp = this->head;
		for (int i = 1; i < x; i++) 
		{
			ptemp = ptemp->next;
		}		
		Node* pnew = new Node;
		cout << "Input the " << x << "th node:n" << endl;
		cin >> pnew->name >> pnew->id >> pnew->points;
		pnew->next = ptemp->next;
		ptemp->next = pnew;
		size++;
		cout << "Increase Success!n" << endl;
		return 1;
	}
	int deleteNode()		//删除结点
	{
		cout << "Select a location:n" << endl;
		int x; cin >> x;
		if (x <= 0 || x > size)
		{
			cout << "Out of the link orange!n" << endl;
			return 0;
		}
		Node* ptemp = this->head;
		Node* pd = NULL;
		for (int i = 1; i < x; i++)
		{
			ptemp = ptemp->next;
		}
		pd = ptemp->next;
		ptemp->next = ptemp->next->next;
		delete pd;
		size--;
		cout << "Deleted successfully!n" << endl;
		return 1;
	}
	int reviseNode()		//修改结点
	{
		cout << "Select a location:n" << endl;
		int x; cin >> x;
		if (x <= 0 || x > size)
		{
			cout << "Out of the link orange!n" << endl;
			return 0;
		}
		Node* ptemp = this->head;
		for (int i = 0; i < x; i++)
		{
			ptemp = ptemp->next;
		}
		cout << "Revise the " << x << "th node:n" << endl;
		cin >> ptemp->name >> ptemp->id >> ptemp->points;
		cout << "Revise successfully!n" << endl;
		return 1;
	}
	int findNode()		//查找结点
	{
		cout << "Select a location:n" << endl;
		int x; cin >> x;
		if (x <= 0 || x > size)
		{
			cout << "Out of the link orange!n" << endl;
			return 0;
		}
		Node* ptemp = this->head;
		for (int i = 0; i < x; i++)
		{
			ptemp = ptemp->next;
		}
		cout << "The " << x << "th node is:n" 
			 << ptemp->name << "  " << ptemp->id << "  " << ptemp->points 
			 << "n"< 
main函数(link.cpp) 
#include "link.h"
int main()
{
	linkList list;
	int x;
begin:
	cout << "Select Operation:n"
		<< "*************1.Create a link!*************n"
		<< "*************2.Show the link!*************n"
		<< "*************3.Find the node!*************n"
		<< "*************4.Add the node! *************n"
		<< "*************5.Delete the node!***********n"
		<< "*************6.Revise the node!***********n";
	cin >> x;
	switch (x)
	{
	case 1:
		list.createlink();
		goto begin;
	case 2:
		list.showlink();
		goto begin;
	case 3:
		list.findNode();
		goto begin;
	case 4:
		list.addNode();
		goto begin;
	case 5:
		list.deleteNode();
		goto begin;
	case 6:
		list.reviseNode();
		goto begin;
	default:
		cout << "No corresponding operation. Please select again!n" << endl;
		goto begin;
	}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/3970119.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存