栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 单链表的初始化,插入,查找,删除操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 单链表的初始化,插入,查找,删除操作

#include
using namespace std;
typedef struct LNode {
int data;
LNode* next;
}LNode,linklist;
//初始化一个空表
//bool InitList(linklist &L) {
// L = NULL;
// return true;
//}
//判断单链表是否为空
//bool Empty(linklist L) {
// return(L == NULL);
//}
//初始化一个带头结点的单链表
bool InitList(linklist& L) {
L = new LNode;
if (L == NULL)
return false;
L->next = NULL;
return true;
}
//判断带头结点的单链表是否为空
bool Empty(linklist L) {
return(L->next == NULL);
}
//带头结点的单链表的插入操作(视头节点为第0个节点)
bool ListInsert(linklist& L, int i, int e) {
if (i < 1) {
return false;
}
linklist p = L; //作为移动指针
int j = 0; //当前p指向的是哪个节点
while (p != NULL && j < i - 1) { //循环找到第i-1个结点
p = p->next; //指针向后移动
j++; //j随着p移动
}
if (p == NULL) { //i值不合法,超过单链表的长度
return false;
}
LNode
s = new LNode;
s->data = e; //插入结点
s->next = p->next;
p->next = s;
return true;
}
//不带头结点的单链表的插入操作
//bool ListInsert(linklist& L, int i, int e) {
// if (i < 1)
// return false;
// if (i == 1) { //插入第1个结点需要特殊处理
// LNode* s = new LNode;
// s->data = e;
// s->next = L;
// L = s;
// }
// linklist p = L; //作为移动指针
// int j = 1; //当前p指向的是哪个节点
// while (p != NULL && j < i - 1) { //循环找到第i-1个结点
// p = p->next; //指针向后移动
// j++; //j随着p移动
// }
// if (p == NULL) { //i值不合法,超过单链表的长度
// return false;
// }
// LNode* s = new LNode;
// s->data = e; //插入结点
// s->next = p->next;
// p->next = s;
// return true;
//}
//单链表的p结点后插操作(上面插入函数插入部分可直接替换成此函数)
bool InsertNextNode(LNode* p, int e){
if (p == NULL)
return false;
LNode* s = new LNode;
if (s == NULL) //判断内存是否满了
return false;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
//在p结点前插操作
//第一种传入一个头指针
bool InsertPeriorNode(linklist &L,LNodep,int e){}
//第二种数据交换
bool InsertPeriorNode(LNode
p, int e) {
if (p == NULL)
return false;
LNode* s = new LNode;
if (s == NULL)
return false;
s->next = p->next; //交换数据
p->next = s;
s->data = p->data;
p->data = e;
}
//按位序删除带头结点的第i个结点并返回值
bool ListDelete(linklist &L, int i, int& e) {
if (i < 1)
return false;
LNode* p = L;
int j = 0;
while (p != NULL && j < i - 1) {
p = p->next;
j++;
}
if (p == NULL)
return false;
//e = p->next->data;
//p->next = p->next->next;
LNode q = p->next;
e = q->data;
p->next = q->next;
delete(q);
return true;
}
//删除定结点p
bool DeleteNode(LNode
p) {
if (p == NULL)
return false;
LNode* s = p->next;
p->data = s->data;
p->next = s->next;
delete(s);
}
//按位查找
LNode* GetElem(linklist L, int i) {
if (i < 0)
return NULL;
LNode* p = L;
int j = 0;
while (p != NULL && j < i) {
p = p->next;
j++;
}
return p;
}
//按值查找
LNode* locateElem(linklist L, int e) {
LNode* p = L->next;
while (p != NULL && p->data = e) {
p = p->next;
}
return p;
}
//求表的长度
int Length(linklist L) {
int len = 0;
LNode* p = L;
while (p->next != NULL) {
p = p->next;
len++;
}
return len;
}
//单链表的建立
//尾插法
linklist List_Tailsert(linklist& L) {
int e = 0;
linklist s,p = L;
p = new LNode;
while (e != 9999) {
cout << “请输入您要添加的数据:”;
cin >> e;
p->data = e;
p->next = s->next;
s->next = p;
}
return L;
}
//头插法
linklist List_Forstsert(linklist& L) {
int e = 0;
while (e != 9999) {
cout << “请输入您要添加的数据:”;
cin >> e;
LNode* p = new LNode;
p->data = e;
p->next = L->next;
L->next = p;
}
return L;
}

int main() {
//LNode* L; //声明一个指向单链表第一个结点的指针
linklist L; //声明一个指向单链表第一个结点的指针
InitList(L);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346953.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号