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

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

简介

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

Link头文件(Link.h)

#pragma once
#include<iostream>
#include<string>
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 << "error\n" << 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"<<endl;
		return 1;
	}
};


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;
	}

}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值