单链表增删改查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;
}
}