单链表(建表、查找、插入、删除、清空、判空)
建表、查找、插入、删除都使用了两种方法;
1.list.h
#pragma once
#ifndef _LIST_H
#define _LIST_H
#include<iostream>
using namespace std;
//#define OK1
//#define ERROR 0
//#define TRUE 1
//#define FALSE 0
//定义单链表节点类型
struct ListNode {
int data;
ListNode* next;
};
//初始化
bool InitList(ListNode* L); //初始化
//建表
ListNode* CreateList_haed(ListNode* L, int n);//头插法建表
ListNode* CreateList_tail(ListNode* L, int n);//尾插法建表
//查找
ListNode* GetElement_i(ListNode* L, int i);//按序号查找,返回结点
int GetElement_e(ListNode* L, int e);//按值查找,返回序号
//插入
ListNode* ListInsert(ListNode* L, int i, int e);//按序号插入,在第i个位置值为e的新节点
ListNode* InsertNextNode(ListNode* L, ListNode* p, int e);//按结点插入,结点p之后插入元素e
//删除
ListNode* ListDelete(ListNode* L, int i);//删除指定位置元素
ListNode* DeleteNextNode(ListNode* L, ListNode* p);//删除结点p之后元素e
//长度
int Length(ListNode* L);//链表长度
//打印
bool printList(ListNode* L);//打印链表
//清空
int ClearList(ListNode* L);
//判空
bool isEmpty(ListNode* L);
#endif // !_LIST_H
2 list.cpp
#include "list.h"
//初始化
bool InitList(ListNode* L) {
//构建空的单链表,作为单链表的表头,数据域为空
L = new ListNode;
L->next = NULL; //将头节点的指针域置空
if (L == NULL) //内存不够
return false;
else
return true;
}
//头插法建表
ListNode* CreateList_haed(ListNode* L,int n) {
if (InitList(L)) {
for (int i = 0; i < n; i++) {
ListNode* p = new ListNode; //创建新节点
cin >> p->data;
p->next = L->next; //将新节点插入到表中,L为头指针
L->next = p;
}
return L;
}
}
//尾插法建表
ListNode* CreateList_tail(ListNode* L, int n) {
if (InitList(L)) {
ListNode* r = L;
for (int i = 0; i < n; i++) {
ListNode* p = new ListNode;
cin >> p->data;
r->next = p;
r = p;
}
r->next = NULL;
return L;
}
}
//按序号查找,返回结点
ListNode* GetElement_i(ListNode* L, int i) {
int j = 1;
ListNode* p = L->next; //p指向头结点
if (i == 0)
return L;
if (i < 1)
return NULL;
while (p && j < i) {//从第一个结点开始查找,找到第i个结点
p = p->next;
j++;
}
return p; //返回i结点
}
//按值查找,返回序号
int GetElement_e(ListNode* L, int e) {
ListNode* p = L->next;
cout << "L->next->data =" << p->data <<" "<<L->next->data<< endl;
int i =1;
while (p && p->data != e) {
i++;
p = p->next;
}
return i;
}
//按序号插入,在第i个位置值为e的新节点
ListNode* ListInsert(ListNode* L, int i, int e) {
ListNode* p=new ListNode;
p = GetElement_i(L, i - 1);//找到待插入前驱
ListNode* s = new ListNode;
s->data = e;
s->next = p->next;
p->next = s;
return L;
}
//按结点插入,结点p之后插入元素e
ListNode* InsertNextNode(ListNode* L,ListNode* p, int e) {
ListNode* s=new ListNode;//创建新结点
s->data = e;
s->next = p->next;
p->next = s;
return L;
}
//删除指定位置元素
ListNode* ListDelete(ListNode* L, int i) {
ListNode* p=new ListNode;
p= GetElement_i(L, i - 1);//找到待删除前驱
ListNode* q = p->next;
p->next = p->next->next;
delete q;
return L;
}
//删除结点p之后元素e
ListNode* DeleteNextNode(ListNode* L, ListNode* p) {
ListNode* q=p->next;
p->next = p->next->next;
delete q;
return L;
}
//求链表的长度
int Length(ListNode* L) {
int len = 0;
ListNode* p = L->next;
while (p) {
len++;
p = p->next;
}
return len;
}
//打印链表
bool printList(ListNode* L) {
if (L->next == NULL)
return false;
ListNode* p = L->next;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
return true;
}
//清空
int ClearList(ListNode* L) {
ListNode* p = L->next;
while (p) {
ListNode* q=p;
p = p->next;
delete q;
}
L ->next= p;
return 0;
}
//判空
bool isEmpty(ListNode* L) {
if (L->next == NULL)
return true;
else
return false;
}
3 main()
分别调用两种方法测试效果
#include "list.h"
int main()
{
ListNode* L = new ListNode;//创建头结点
L->next = NULL; //将头节点的指针域置空
//头插法建表
CreateList_haed(L,5);
int len=Length(L);
cout << "len = " << len << endl;
printList(L);
//按序号查找,返回结点
ListNode* p=new ListNode;
p = GetElement_i(L,1);
cout << "查找结果:第1个位置,元素是:"<< p->data << endl;
//按序号插入,在第i个位置值为e的新节点
ListInsert( L, 3, 0);
len = Length(L);
cout << "在第3个位置值为0后 len = " << len << endl;
printList(L);
//删除指定位置元素
ListDelete(L, 2);
len = Length(L);
cout << "删除第2元素后len = " << len << endl;
printList(L);
//清空
ClearList(L);
len = Length(L);
printList(L);
cout << "清空 len = " << len << endl;
//判空
cout << "判空"<<isEmpty(L) << endl << endl;
/*-------我是一条可爱的分界线-------*/
cout << "*-------我是一条可爱的分界线-------*" << endl;
//尾插法建表
CreateList_tail(L, 5);
len = Length(L);
cout << "len = " << len << endl;
printList(L);
//按值查找,返回序号
cout << "查找结果:元素3,序号是:" << GetElement_e(L, 3) << endl;
//按结点插入,结点q之后插入元素e
ListNode* q = L->next->next;
InsertNextNode(L, q, 0);
len = Length(L);
cout << "在结点q之后插入元素0后 len = " << len << endl;
printList(L);
//删除结点q之后插入元素e
DeleteNextNode(L, q);
len = Length(L);
cout << "删除结点q之后元素后 len = " << len << endl;
printList(L);
//清空
ClearList(L);
len = Length(L);
printList(L);
cout << "清空 len = " << len << endl;
//判空
cout << "判空" << isEmpty(L) << endl << endl;
}