CList.cpp
#include<stdio.h>
#include<stdlib.h>
#include"cList.h"
#include<assert.h>
//初始化
void InitCList(CList pList) {
assert(pList != NULL);
if (pList == NULL)
return;
pList->next = pList;
}
//头插
bool Insert_head(CList pList, int val) {
assert(pList != NULL);
if (pList == NULL)
return false;
CList p = (CList)malloc(sizeof(Cnode));
p->data = val;
p->next = pList->next;
pList->next = p;
return true;
}
//尾插
bool Insert_tail(CList pList, int val) {
assert(pList != NULL);
if (pList == NULL)
return false;
CList p = (CList)malloc(sizeof(Cnode));
assert(p != NULL);
p->data = val;
CList q;
for (q = pList; q->next != pList; q = q->next) {
;
}
p->next = q->next;
q->next = p;
return true;
}
//插入数据,在pList链表的pos位置插入val;
bool Insert(CList pList, int pos, int val) {
assert(pList != NULL);
if (pList == NULL)
return false;
if (pos<0 || pos>Getlength(pList))
return false;
CList p = (CList)malloc(sizeof(Cnode));
p->data = val;
int i = 0;
CList q;
for (q = pList; i < pos;q = q->next ,i++ ) {
}
p->next = q->next;
q->next = p;
return true;
}
//判空
bool IsEmpyt(CList pList) {
if (pList->next == pList) {
return true;
}
else {
return false;
}
}
//获取有效数据节点的个数
int Getlength(CList pList) {
CList p;
int i = 0;
for (p = pList->next; p != pList; p = p->next) {
i++;
}
return i;
}
//在pList中查找第一个key值,找到返回节点地址,没有找到返回NULL
Cnode* Search(CList pList, int key) {
assert(pList != NULL);
if (pList == NULL)
return 0;
CList p;
for (p = pList->next; p != pList; p = p->next) {
if (p->data == key)
return p;
}
}
//删除pos位置的值
bool DelPos(CList pList, int pos) {
assert(pList != NULL);
if (pList == NULL)
return false;
CList p;
int i = 0;
for (p = pList; i < pos; i++, p = p->next) {
;
}
CList q = (CList)malloc(sizeof(Cnode));
q = p->next;
p->next = q->next;
free(q);
return true;
}
//删除第一个val的值
bool DelVal(CList pList, int val) {
assert(pList != NULL);
if (pList == NULL)
return false;
CList p = GetPrio(pList, val);
CList q = p->next;
p->next = q->next;
free(q);
return true;
}
//返回key的前驱地址,如果不存在返回NULL
Cnode* GetPrio(CList pList, int key) {
assert(pList != NULL);
if (pList == NULL)
return NULL;
CList p;
for (p = pList->next; p != pList; p = p->next) {
if (p->next->data == key) {
return p;
}
}
}
//返回key的后继地址,如果不存在返回NULL
Cnode* GetNext(CList pList, int key) {
assert(pList != NULL);
if (pList == NULL)
return NULL;
CList p = Search(pList, key);
return p->next;
}
//输出
void Show(CList pList) {
assert(pList != NULL);
if (pList == NULL)
return;
for (Cnode* p = pList->next; p !=pList; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
//清空
void clear(CList plist) {
Destroy(plist);
}
//销毁
void Destroy(CList pList) {
CList p;
p = pList->next;
while (p != pList) {
p = p->next;
free(p);
}
}
CList.h
#pragma once
typedef struct Cnode {
int data;
struct Cnode* next;
}Cnode,*CList;
//初始化
void InitCList(CList pList);
//头插
bool Insert_head(CList pList, int val);
//尾插
bool Insert_tail(CList pList, int val);
//插入数据,在pList链表的pos位置插入val;
bool Insert(CList pList, int pos, int val);
//判空
bool IsEmpyt(CList pList);
//获取数据节点的个数
int Getlength(CList pList);
//在pList中查找第一个key值,找到返回节点地址,没有找到返回NULL
Cnode* Search(CList pList, int key);
//删除pos位置的值
bool DelPos(CList pList, int pos);
//删除第一个val的值
bool DelVal(CList pList, int val);
//返回key的前驱地址,如果不存在返回NULL
Cnode* GetPrio(CList pList, int key);
//返回key的后继地址,如果不存在返回NULL
Cnode* GetNext(CList pList, int key);
//输出
void Show(CList pList);
//清空
void clear(CList plist);
//销毁
void Destroy(CList pList);
test.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"cList.h"
int main() {
Cnode head;
InitCList(&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("此时链表有效节点个数是:%d\n", Getlength(&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);
Cnode* x = GetPrio(&head, key);
Cnode* 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");
//clear(&head);
//Destroy(&head);
}
循环单链表
最新推荐文章于 2024-10-08 23:13:16 发布