C++单链表增删查找的基本操作

#include<iostream>
using namespace std;

typedef int DataType;

class Node {
public:
int data;
Node * next;
};

public:
//创建一个单链表
//遍历线性链表
//获取链表长度
//判断单链表是否为空
bool isEmpty();
//寻找链表节点
Node *findNode(DataType data);

//尾部插入指定值
//在指定位置插入值
//在头部插入数值
//删除尾部数据库
//删除所有数据
//删除指定数据
//在指定位置删除数据
//删除头部位置的数据

//翻转链表所有的数据

private:
};

}

}

Node *pnew, *ptemp;
if (n < 0) {
cout << "输入的节点个数有误" << endl;
exit(EXIT_FAILURE);
}

for (int i = 0; i < n; i++) {
pnew = new Node;
cout << "请输入第" << i + 1 << "个值: ";
cin >> pnew->data;
pnew->next = NULL;
ptemp->next = pnew;
ptemp = pnew;

}
}

cout << "链表为空表" << endl;
}

while (ptemp->next != NULL) {
ptemp = ptemp->next;
cout << ptemp->data << " ";
}
cout << endl;
}

//获取链表的长度
int count = 0;
while (ptemp->next != NULL) {
ptemp = ptemp->next;
count++;
}
return count;
}

//判断链表是否为空
return true;
}
return false;
}

if (ptemp == NULL) {
cout << "此链表为空链表" << endl;
return NULL;
}

while (ptemp->next != NULL)
{
if (ptemp->data == data) {
return ptemp;
}
ptemp = ptemp->next;

}

return NULL;

}

//在链表尾部插入数据
Node *newNode = new Node;
newNode->data = data;
newNode->next = NULL;

}
else {
while (ptemp->next != NULL)
{
ptemp = ptemp->next;
}
ptemp->next = newNode;
}
}

//在链表指定位置插入数据
if (index < 1 || index> getLinkListLength()){
cout << "输入的值错误" << endl;
}
Node * newNode = new Node;
newNode->data = data;
newNode->next = NULL;

int i = 1;
while (i < index) {
ptemp = ptemp->next;
i++;
}
newNode->next = ptemp->next;

ptemp->next = newNode;
}

//在链表头部后面插入数据
Node *newNode = new Node;
newNode->data = data;
newNode->next = NULL;
}

newNode->next = ptemp->next;
ptemp->next = newNode;
}

//在尾部删除数据
Node *ptemp = NULL;
if (p == NULL || p->next == NULL) {
cout << "单链表空" << endl;
}
else {
while (p->next != NULL) {
ptemp = p;
p = p->next;
}
delete p;
p = NULL;
ptemp->next = NULL;
}
}

//删除所有的数据
Node *ptemp = new Node;
while (p != NULL){
ptemp = p;
p = p->next;
ptemp->next = NULL;
delete ptemp;
}

}

//删除指定数据
Node *ptemp = findNode(data);
}
else{
while (p->next != ptemp)
{
p = p->next;
}
p->next = ptemp->next;
delete ptemp;
ptemp = NULL;
}
}

//在头部删除数据
if (p == NULL || p->next == NULL){
cout << "该链表为空表" << endl;
}
else{
Node *ptemp = NULL;
p = p->next;
ptemp = p->next;
delete p;
p = NULL;
}
}

//在指定位置删除数据
if (index <1 || index > getLinkListLength()){
cout << "输入的删除位置错误" << endl;
}
if (p ==NULL || p->next == NULL){
cout << "链表为空" << endl;
}
int i = 0;
Node *ptemp = NULL;
while (i < index) {
ptemp = p;
p = p->next;
i++;
}

ptemp->next = p->next;
delete p;
p = NULL;

}

/*
//翻转链表
if (p == NULL || p->next == NULL){
cout << "链表为空" << endl;
}

Node *reverseList = new Node;
reverseList->next = NULL;
reverseList->data = 0;

Node *ptemp = NULL;
while (p != NULL){
ptemp = p->next;

//reverseList前插入数据
p->next = reverseList->next;
reverseList->next = p;
p = ptemp;
}
return reverseList;

}
*/

int main(){
int i;
cout << "1.创建单链表   2.遍历单链表\n";
cout << "3.获取单链表的长度    4.判断单链表是否为空\n";
cout << "5.获取节点   6.在尾部插入指定元素\n";
cout << "7.在指定位置插入指定元素   8.在头部插入指定元素\n";
cout << "9.在尾部删除元素    10.删除所有元素\n";
cout << "11.删除指定元素    12.在头部删除元素\n";
cout << "13.删除指定位置的数据    0.退出" << endl;

do{
cout << "请输入要执行的操作: ";
cin >> i;
switch (i)
{
case 1:
int n;
cout << "请输入链表的长度" << endl;
cin >> n;
break;
case 2:
break;
case 3:
break;
case 4:
cout << "该单链表是空表" << endl;
}
else
{
cout << "该单链表不是空表" << endl;
}
break;
case 5:
int data;
cout << "请输入要获取节点的值: ";
cin >> data;
cout << "该节点的值为：" << linklist.findNode(data)->data << endl;
break;
case 6:
int endData;
cout << "请输入要在尾部插入的值: ";
cin >> endData;
break;
case 7:
DataType pointData;
int index;
cout << "请输出要插入的值" << endl;
cin >> pointData;
cout << "请输出要插入值的位置" << endl;
cin >> index;
break;
case 8:
cout << "请输出在头部要插入的值" << endl;
break;
case 9:
cout << "尾部删除数据操作完成"<< endl;
break;
case 10:
cout << "删除所有数据操作完成" << endl;
break;
case 11:
DataType pointDeleteData;
cout << "请输出要删除的指定值" << endl;
cin >> pointDeleteData;
break;
case 12:
cout << "删除头部数据操作完成" << endl;
break;
case 13:
int deleteIndex;
cout << "请输出要删除值的位置" << endl;
cin >> deleteIndex;
break;
default:
break;
}
} while (i != 0);

system("pause");
return 0;
}
01-27 4万+

01-02 3132
04-05 1580
11-18 2106
08-07 1万+
04-16 688
07-11 189
09-02 177