最近打算复习巩固一下数据结构,用c++实现了链表的一些基本功能,可能有bug,有问题的话,希望大佬留下评论指证。
#include <iostream>
#include <malloc.h>
#include <ctime>
#include <cmath>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
using namespace std;
typedef int ElmeType;
typedef int status;
//链表的结构体
typedef struct Node
{
ElmeType data;
struct Node* next;
} Node;
typedef struct Node* LinkList;
//初始化链表
status InitList(LinkList *L)
{
*L = (LinkList)malloc(sizeof(Node));
if((*L))
return ERROR;
(*L)->next = NULL;
return OK;
}
//头插法创建链表
status CreateListHead(LinkList *L, int len)
{
LinkList p;
srand(time(0));//初始化随机数种子
*L = (LinkList)malloc(sizeof(Node));//创建一个头节点
(*L)->next = NULL;
for(int i = 0; i < len; i++)
{
p = (LinkList)malloc(sizeof(Node));//创建新的节点
p->data = rand() % 100 + 1;
p->next = (*L)->next;
(*L)->next = p;
}
return OK;
}
//尾插法创建链表
status CreateListTail(LinkList *L, int len)
{
LinkList p, q;
srand(time(0));//初始化随机数种子
*L = (LinkList)malloc(sizeof(Node));
//p = (LinkList)malloc(sizeof(Node));
//p->data = rand()%100 + 1;
//(*L)->next = p;
p = *L;
for( int i = 0; i < len; i++)
{
q = (LinkList)malloc(sizeof(Node));
q->data = rand() % 100 + 1;
p ->next = q;
p = p->next;
p->next = NULL;
}
return OK;
}
//删除链表
void DeleteList(LinkList *L)
{
LinkList p, q;
p = (*L)->next;
while(p)
{
q = p->next;
delete(p);
p = q;
}
(*L)->next = NULL;
}
//打印出链表内容
void PrintLinkList(LinkList L)
{
LinkList p;
p = L->next;
while(p)
{
cout << p->data << "\t";
p = p->next;
}
cout << endl;
}
//返回链表的长度(不包括头节点)
int LinkListLength(LinkList L)
{
int count = 0;
LinkList p;
p = L->next;
while(p)
{
p = p->next;
count++;
}
return count;
}
//判断链表是否为空
status isEmpty(LinkList L)
{
if(L->next)
return FALSE;
else
return TRUE;
}
//返回链表中第一个值为e的位置
int FindElme(LinkList L, ElmeType e)
{
LinkList p;
int count = 0;
p = L->next;
while(p)
{
if(p->data != e)
{
count++;
p = p->next;
}
else
break;
}
if(count < LinkListLength(L))
return count + 1;
else
return -1;
}
//获得链表中第i个元素的值
int GetElme(LinkList L, int i)
{
LinkList p;
p = L->next;
int j = 0;
if(i < 1 || i > LinkListLength(L))
return -1;
while(j < i - 1)
{
p = p->next;
j++;
}
return p->data;
}
//在第i个位置插入元素e
status InsertLinklist(LinkList *L, int i, ElmeType e)
{
LinkList p, q;
int j = 0;
p = (*L)->next;
if(i < 1 || i > LinkListLength((*L)))
return -1;
if(i == 1)
{
q = (LinkList)malloc(sizeof(Node));
(*L)->next = q;
q->data = e;
q->next = p;
return OK;
}
while(j < i - 2)
{
p = p->next;
j++;
}
q = (LinkList)malloc(sizeof(Node));
q->data = e;
q->next = p->next;
p->next = q;
return OK;
}
//删除第i个位置的元素
int DeleteElme(LinkList *L, int i)
{
LinkList p, q;
int j = 0;
ElmeType e;
p = (*L)->next;
if(i < 1 || i > LinkListLength((*L)))
return -1;
if(i == 1)
{
e = p->data;
(*L)->next = (*L)->next->next;
delete(p);
return e;
}
while(j < i - 2)
{
j++;
p = p->next;
}
q = p->next;
e = q->data;
p->next = p->next->next;
delete(q);
return e;
}
int main()
{
status i;
int j, k;
LinkList L;
InitList(&L);
cout << "头插法创建链表的长度:";
cin >> j;
CreateListHead(&L, j);
PrintLinkList(L);
cout << "链表长度为:" << LinkListLength(L) << endl;
//DeleteList(&L);
cout << "请输入你要查找的数值:";
cin >> j;
i = FindElme(L, j);
if(i != -1)
cout << "你查找的数值在链表中第" << i << "个位置!\n";
else
cout << "你要查找的数值不存在该链表中!\n";
cout << "请输入你想要第几个元素:";
cin >> j;
i = GetElme(L, j);
if(i != -1)
cout << "第" << j << "个元素的值为:" << i << endl;
else
cout << "请输入正确的索引\n";
cout << "请输入要插入的位置和插入的元素:";
cin >> j >> k;
i = InsertLinklist(&L, j, k);
if(i != -1)
PrintLinkList(L);
else
cout << "请输入正确的元素或正确的位置!\n";
cout << "请输入要删除位置的索引:";
cin >> j;
i = DeleteElme(&L, j);
if(i != -1)
{
cout << "删除的元素为:" << i << "删除后的链表为:\n";
PrintLinkList(L);
}
else
cout << "请输入正确的索引!";
DeleteList(&L);
i = isEmpty(L);
if(i == 0)
cout << "链表不为空\n";
else
cout << "链表为空\n";
cout << "尾插法创建链表的长度:";
cin >> j;
CreateListTail(&L, j);
PrintLinkList(L);
return 0;
}