C++实现简单的链表操作,包括链表创建的基本方法、插入和删除等最基本链表操作,以下是源代码和效果演示。。之前发过一篇类似的,中间有些细微区别,这篇链表的头节点的数据域不为空
#include <iostream>
#include <stdlib.h>
using namespace std;
class Node {
public:
Node();
Node(int data);
void setData(int x);
friend class Linklist;
private:
int data;
Node* next;
};
Node::Node()
{
}
Node::Node(int data)
{
this->data = data;
this->next = NULL;
}
void Node::setData(int x)
{
data = x;
next = NULL;
}
class Linklist {
public:
Linklist();
Linklist(Node node);
~Linklist();
int getIndex(int x); //返回第x个元素的值
void addAtHead(int data); //头插法
void addAtTail(int data); //尾插法
void addAtIndex(int x,int data); //根据下标插入节点
void deleteIndex(int x); //根据下标删除节点
void print(); //打印链表
int getLenght(); //返回链表的长度
void inform(); //提示消息
private:
Node* head; //头节点
};
Linklist::~Linklist(){}
Linklist::Linklist()
{
head = NULL;
}
Linklist::Linklist(Node node)
{
head->data=node.data;
head->next = node.next;
}
void Linklist::inform()
{
cout << "操作成功,是否打印出链表?y|n" << endl;;
char ch;
cin >> ch;
switch (ch)
{
case 'y' :print(); break;
case 'n':break;
default:break;
}
}
int Linklist::getLenght()
{
Node* p = head;
int i = 1;
if (head == NULL)
{
i = 0;
}
else {
while (p->next != NULL)
{
i++;
p = p->next;
}
}
return i;
}
void Linklist::addAtHead(int data)
{
Node* node=new Node(data);
if (this->head == NULL)
{
head = node;
}
else
{
node->next = head;
head = node;
}
inform();
}
void Linklist::addAtTail(int data)
{
Node* node=new Node();
node->setData(data);
if (this->head == NULL)
{
head = node;
}
else
{
Node* p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = node;
}
inform();
}
void Linklist::addAtIndex(int x,int data)
{
int n = getLenght();
if (x > n+1||x<=0 )
{
cout << "该位置不存在哦" << endl;;
return;
}
else if(x==1||n==0)
{
addAtHead(data);
return;
}
else if (x == n+1)
{
addAtTail(data);
return;
}
else
{
Node* node = new Node(data);
Node* p = head;
int i = 1;
while (i<x-1)
{
i++;
p = p->next;
}
node->next = p->next;
p->next = node;
}
inform();
}
void Linklist::deleteIndex(int x)
{
int n = getLenght();
if (n < x)
{
cout << "该位置不存在哦" << endl;
return;
}
if (x == 1)
{
head = head->next;
}
else
{
int i = 1;
Node* p = head;
while (i < x-1)
{
i++;
p = p->next;
}
p->next = p->next->next;
}
inform();
}
int Linklist::getIndex(int x)
{
int n = getLenght();
if (n < x||x==0)
{
cout << "该位置还没有数据" << endl;
return -1;
}
else
{
Node* p = head;
int i = 1;
while (i < x)
{
p = p->next;
i++;
}
cout <<"该处数据为:"<< p->data << endl;
return p->data;
}
}
void Linklist::print()
{
Node* node = this->head;
while (node!= NULL)
{
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
int main()
{
Linklist list;
cout << "空链表链表已准备就绪,请选择以下操作:\n";
cout << "\n";
cout << "\t1.头插法........ \t2.尾插法........ ";
cout << "\n";
cout << "\n";
cout << "\t3.指定位置插入........ \t4.指定位置删除节点........ ";
cout << "\n";
cout << "\n";
cout << "\t5.打印链表........ \t6.查询某个位置的值........ ";
cout << "\n";
cout << "\n";
cout << "\t7.退出程序........ ";
cout << "\n";
while (1) {
int index;
cin >> index;
switch (index)
{
case 1:int data; cout << "请输入要添加的数:"; cin >> data; list.addAtHead(data); break;
case 2:int data1; cout << "请输入要添加的数:"; cin >> data1; list.addAtTail(data1); break;
case 3:int index1, data2; cout << "请输入要添加的位置和数:"; cin >> index1 >> data2; list.addAtIndex(index1, data2); break;
case 4:int index2; cout << "请输入要删除的位置:"; cin >> index2; list.deleteIndex(index2); break;
case 5:list.print(); break;
case 6:int index3; cout << "请输入要查询的位置:"; cin >> index3; list.getIndex(index3); break;
case 7:exit(0);
}
cout << "请选择以下操作:\n";
cout << "\n";
cout << "\t1.头插法........ \t2.尾插法........ ";
cout << "\n";
cout << "\n";
cout << "\t3.指定位置插入........ \t4.指定位置删除节点........ ";
cout << "\n";
cout << "\n";
cout << "\t5.打印链表........ \t6.查询某个位置的值........ ";
cout << "\n";
cout << "\n";
cout << "\t7.退出程序........ ";
cout << "\n";
}
}