建立单链表
main.cpp
#include<iostream>
#include"linklist.h"
using namespace std;
int main()
{
ElemType a[5] = { 1, 3, 5, 7, 9 };
LinkNode* L1,*L2;
ElemType n = 0;
CreateListH(L1, a, 5);
CreateListB(L2, a, 5);
DispList(L1); DispList(L2);
n = GetElem(L1, 3);
cout <<"L1第3个元素值:"<< n << endl;
n = LocateElem(L1, 3);
cout << "L1元素值为3的元素是第"<< n <<"个节点"<< endl;
insertElem(L1, 4, 6);
cout << "插入后L1的值:";
DispList(L1);
DelList(L1, 5);
DispList(L1);
DestroyList(L2);
DestroyList(L1);
return 0;
}
Linklist.cpp
#include"linklist.h"
#include<iostream>
using namespace std;
void CreateListH(LinkNode*& L, ElemType *a, int n) { //头插法创建
LinkNode* s;
L = new LinkNode;
L->next = NULL;
for (int i = 0; i < n; i++) {
s = new LinkNode;
s->next = L->next;
s->data = a[i];
L->next = s;
}
}
void CreateListB(LinkNode*& L, ElemType *a, int n) { //尾插法创建
LinkNode* s,*r;
L = new LinkNode;
L->next = NULL;
r = L;
for (int i = 0; i < n; i++)
{
s = new LinkNode;
s->data = a[i];
r->next = s;
r=s;
}
r->next = NULL;
}
void DestroyList(LinkNode*& L) { //销毁链表
LinkNode* p = L->next,*pre=L;
while (p!=NULL)
{
free(pre);
pre = p;
p = pre->next;
}
free(pre);
}
bool Empty(LinkNode* L) {
return(L->next == NULL);
}
int ListLength(LinkNode* L)
{
int n = 0;
LinkNode* p = L;
while (p->next != NULL)
{
n++; p = p->next;
}
return n;
}
void DispList(LinkNode* L) {
LinkNode* p = L->next;
while (p!=NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
ElemType GetElem(LinkNode* L, int n) {//查找第n个元素并返回值
LinkNode* p = L;
for (int i = 0; i < n; i++){
if (Empty(p)) {
cout << "未找到元素" << endl;
return -1;
}
p = p->next;
}
return p->data;
}
int LocateElem(LinkNode* L, ElemType n) {
LinkNode* p = L->next; int i = 1;
while (p != NULL && p->data != n)
{
p = p->next;
i++;
}
if (p == NULL)
return 0;
else
return i;
}
bool insertElem(LinkNode*& L, int i, ElemType e) {
int j = 0;
LinkNode* p = L, * s;
if (i <= 0)return false;
while (j < i - 1 && p != NULL) {
j++; p = p->next;
}
if (p == NULL)return false;
else
{
s = new LinkNode;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
}
bool DelList(LinkNode*& L, int i)//删除第i个节点
{
int j = 0;
LinkNode* p = L, * s;
if (i <= 0)return false;
while (j < i - 1 && p != NULL)
{
j++; p = p->next;
}
if (p == NULL)return false;
else
{
s = p->next;
if (s == NULL)return false;
p->next = s->next;
free(s);
return true;
}
}
linklist.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode * next;
}LinkNode;
void CreateListH(LinkNode*& L, ElemType *a, int n); //头插法创建
void CreateListB(LinkNode*& L, ElemType *a, int n); //尾插法创建
void DestroyList(LinkNode*& L);//销毁
bool Empty(LinkNode* L);//判空
int ListLength(LinkNode* L);//返回长度
void DispList(LinkNode* L);//输出链表
ElemType GetElem(LinkNode* L, int n);//求第n个节点的值
int LocateElem(LinkNode* L, ElemType n);//找到值为n的节点位置
bool insertElem(LinkNode*& L, int i, ElemType e);//在第i位前面插入元素
bool DelList(LinkNode*& L, int i);//删除第i位前的元素
#endif
#pragma once