list.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;//数据域
struct Node* next;
}Node, * List;
//初始化
void InitList(List plist);
//头插
bool Insert_head(List plist, int val);
//尾插
bool Insert_tail(List plist, int val);
//插入数据,在plist链表的pos位置插入val;
bool Insert(List plist, int pos, int val);
//判空
bool IsEmpyt(List plist);
//获取数据节点的个数
int Getlength(List plist);
//在plist中查找第一个key值,找到返回节点地址,没有找到返回NULL
Node* Search(List plist, int key);
//删除pos位置的值
bool DelPos(List plist, int pos);
//删除第一个val的值
bool DelVal(List plist, int val);
//返回key的前驱地址,如果不存在返回NULL
Node* GetPrio(List plist, int key);
//返回key的后继地址,如果不存在返回NULL
Node* GetNext(List plist, int key);
//输出
void Show(List plist);
//销毁
void Destroy(List plist);
//逆置链表
void ReverseList(List plist);
list.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"list.h"
#include<assert.h>
//初始化
void InitList(List plist) {
assert(plist != NULL);
if (plist == NULL)
return ;
plist->next = NULL;
}
//头插
bool Insert_head(List plist, int val) {
assert(plist != NULL);
if (plist == NULL)
return false;
List p = (List)malloc(sizeof(Node));
p->data = val;
p->next = plist->next;
plist->next = p;
return true;
}
//尾插
bool Insert_tail(List plist, int val) {
assert(plist != NULL);
if (plist == NULL)
return false;
//申请节点
Node* p = (Node*)malloc(sizeof(Node));
assert(p != NULL);
if (p == NULL)
return false;
//将val插入新节点
p->data = val;
//找尾巴
Node* q;
for (q = plist; q->next != NULL; q = q->next)
{
;
}
//插入新节点
p->next = NULL;//或者p->next = NULL
q->next = p;
return true;
}
//插入数据,在plist链表的pos位置插入val;
bool Insert(List plist, int pos, int val) {
assert(plist != NULL);
if (plist == NULL)
return false;
if (pos<0 || pos>Getlength(plist))
return false;
List p = (List)malloc(sizeof(Node));
assert(p != NULL);
p->data = val;
List q;
int i = 0;
for (q = plist ; i < pos ; i++,q=q->next) {
}
p->next = q->next;
q->next = p;
return true;
}
//判空
bool IsEmpyt(List plist) {
assert(plist != NULL);
if (plist == NULL)
return false;
if (plist->next)
return true;
else
return false;
}
//获取数据节点的个数
int Getlength(List plist) {
List p;
p = plist->next;
int i = 0;
while (p) {
i++;
p = p->next;
}
return i;
}
//输出
void Show(List plist) {
assert(plist != NULL);
if (plist == NULL)
return;
List p;
p = plist->next;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
/*for (Node* p = plist->next; p != NULL; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");*/
}
//在plist中查找第一个key值,找到返回节点地址,没有找到返回NULL
Node* Search(List plist, int key)
{
assert(plist != NULL);
if (plist == NULL)
return NULL;
for (Node* p = plist->next; p != NULL; p = p->next)
{
if (p->data == key)
return p;
}
return NULL;
}
//删除pos位置的值
bool DelPos(List plist, int pos) {
assert(plist != NULL);
if (plist == NULL)
return NULL;
List p;
int i = 0;
for (p = plist; i < pos; i++, p = p->next) {
}
List q;
q = (List)malloc(sizeof(Node));
q = p->next;//临时保存被删除结点的地址以备释放
p->next = q->next;//p->next=p->next->next,改变了删除结点前的指针域,指到了下下结点
free(q);
return true;
}
//删除第一个val的值
bool DelVal(List plist, int val) {
assert(plist != NULL);
if (plist == NULL)
return NULL;
List p;
p = GetPrio(plist,val);
List q = p->next;
p->next = q->next;
free(q);
return true;
}
//返回key的前驱地址,如果不存在返回NULL
Node* GetPrio(List plist, int key) {
assert(plist != NULL);
if (plist == NULL)
return NULL;
for (Node* p = plist; p->next!= NULL; p = p->next)
{
if (p->next->data == key)
return p;
}
return NULL;
}
//返回key的后继地址,如果不存在返回NULL
Node* GetNext(List plist, int key) {
assert(plist != NULL);
if (plist == NULL)
return NULL;
List p = Search(plist, key);
return p->next;
}
//清空
void clear(List plist) {
Destroy(plist);
}
//销毁
void Destroy(List plist) {
List p;
p = plist;
while(p->next) {
p = p->next;
free(p);
}
}
//逆置链表
void ReverseList(List plist) {
List p, q;
p = plist->next;
plist->next = NULL;
while (p) {
q = p->next;
p->next = plist->next;
plist->next = p;
p = q;
}
}
test.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"list.h"
int main() {
Node head;
InitList(&head);
//printf("头插法插入:\n");
//for (int i = 0; i < 20; i++) {
// Insert_head(&head, i);
//}
//Show(&head);
printf("尾插法插入:\n");
for (int i = 0; i < 20; i++) {
Insert_tail(&head, i);
}
Show(&head);
printf("\n");
printf("插入数字后:\n");
Insert(&head, 3, 1);
Show(&head);
printf("\n");
printf("判断链表是否为空(1表示空,0表示不为空)\n");
printf("%d\n", IsEmpyt(&head));
printf("\n");
printf("请输入要查找的数:\n");
int key1;
scanf("%d", &key1);
if (key1 == Search(&head, key1)->data) {
printf("已找到,要查找的值为:%d\n", Search(&head, key1)->data);
}
else {
printf("未能查找到该数\n");
}
printf("\n");
printf("请输入需要返回前驱和后继的值:\n");
int key;
scanf("%d", &key);
Node* x = GetPrio(&head, key);
Node* y = GetNext(&head, key);
printf("%d的前驱是:%d\n", key, x->data);
printf("%d的后继是:%d\n", key, y->data);
printf("\n");
printf("请输入要删除的位置\n");
int val;
scanf("%d", &val);
DelPos(&head, val);
printf("删除后链表为:\n");
Show(&head);
printf("\n");
printf("请输入要删除的第一个数字\n");
int key2;
scanf("%d", &key2);
DelVal(&head, key2);
printf("删除后链表为:\n");
Show(&head);
printf("\n");
printf("链表逆置后为:\n");
ReverseList(&head);
Show(&head);
//printf("清空和销毁操作\n");
//clear(&head);
//Destroy(&head);
}
单链表的操作
最新推荐文章于 2024-10-08 23:13:16 发布