前言
单链表的基本操作:定义、头插法、尾插法、插入节点、删除节点、按值查找、按位查找、打印单链表
一、代码
#include<iostream>
#include<stdlib.h>
using namespace std;
//定义单链表
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
//头插法建立单链表
LinkList List_HeadInsert(LinkList &L) {
int x;
LNode* s;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
cout << "请输入:(输入9999结束)" << endl;
cin >> x;
while (x!=9999) {
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
cin >> x;
}
return L;
}
//尾插法建立单链表
LinkList List_TailInsert(LinkList &L) {
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode *s,*r=L;
cout << "请输入:(输入9999结束)" << endl;
cin >> x;
while (x != 9999) {
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
cin >> x;
}
r->next = NULL;
return L;
}
//按序号查找节点值
LNode* GetElem(LinkList L, int i) {
int j = 1;
LNode *s;
s = L->next;
if (i == 0) {
return L;
}
if (i < 0) {
return NULL;
}
while (s != NULL && j<i) {
s = s->next;
j++;
}
return s;
}
//按值查找表节点
LNode* LocateElem(LinkList L, int e) {
LNode* s;
s = L->next;
while (s!=NULL && s->data!=e) {
s = s->next;
}
return s;
}
//插入节点操作
void Insert(LinkList L, int i,LNode* p) {
LNode* s = GetElem(L, i - 1);
if (s==NULL)
{
cout << "插入位置无效" << endl;
return;
}
p->next = s->next;
s->next = p;
cout << "在 " << i << " 位置插入成功" << endl;
}
//删除节点操作
void Delect(LinkList L, int i) {
LNode* s = GetElem(L, i - 1);
if (s == NULL)
{
cout << "删除位置无效" << endl;
return;
}
LNode* p = s->next;
s->next = p->next;
free(p);
cout << "成功删除在 " << i << " 位置的节点" << endl;
}
//打印单链表
void PrintList(LinkList L) {
LNode* s;
s = (LNode*)malloc(sizeof(LNode));
int i = 1;
s = L->next;
while (s)
{
cout<<"第"<<i++<<"节点 " << s->data <<endl;
s = s->next;
}
}
//打印顺序表长度
//太简单不写了
//菜单
void menu(LinkList &L) {
int n, e, i;
LNode* p;
do
{
cout << "\t\t顺序表操作菜单\t\t" << endl;
cout << "\t\t1.头插法建立单链表\t\t" << endl;
cout << "\t\t2.尾插法建立单链表\t\t" << endl;
cout << "\t\t3.打印单链表\t\t" << endl;
cout << "\t\t4.按序号查找节点值\t\t" << endl;
cout << "\t\t5.按值查找表节点\t\t" << endl;
cout << "\t\t6.插入节点操作\t\t" << endl;
cout << "\t\t7.删除节点操作.\t\t" << endl;
cout << "\t\t8.退出\t\t" << endl;
cout << "请输入序号:" << endl;
cin >> n;
switch (n)
{
case 1:List_HeadInsert(L);//头插法建立单链表
cout << "插入完成..." << endl;
break;
case 2:List_TailInsert(L);//尾插法建立单链表
cout << "插入完成..." << endl;
break;
case 3:PrintList(L);//打印单链表
cout << "打印完成..." << endl;
break;
case 4:cout << "请输入你要查找第几个元素" << endl;
cin >> i;
p= GetElem(L, i);
if(p!=NULL){
cout << "你要查找的元素为" << p->data << endl<<"查找结束"<<endl;
}else{
cout << "未找到" << endl;
}
break;
case 5: cout<< "请输入你要查找的数字" << endl;
cin >> i;
p = LocateElem(L, i);
if (p != NULL) {
cout << "你要查找的数字已找到 " << p->data << endl << "查找结束" << endl;
}
else {
cout << "未找到" << endl;
}
break;
case 6:cout << "请输入你要插入的位置" << endl;
cin >> i;
cout << "请输入你要插入的数字" << endl;
p = (LNode*)malloc(sizeof(LNode));
cin >> p->data;
Insert(L, i, p);
break;
case 7:cout << "请输入你要删除的位置" << endl;
cin >> i;
Delect(L, i);
break;
case 8: return;
default:
cout << "输入错误" << endl;
break;
}
} while (true);
}
int main() {
LinkList L;//创建单链表表
menu(L);
cout << "程序已结束运行...";
return 0;
}