mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-10-31 12:24:51 +08:00
栈与队列更新
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
@@ -143,8 +143,10 @@
|
||||
<ClCompile Include="main.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="double_link_list.h" />
|
||||
<ClInclude Include="link_list.h" />
|
||||
<ClInclude Include="sequence_list.h" />
|
||||
<ClInclude Include="static_link_list.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
#include "head.h"
|
||||
|
||||
// 单链表结点
|
||||
typedef struct LinkNode {
|
||||
typedef struct LinkListNode {
|
||||
element_type data;
|
||||
struct LinkNode* next;
|
||||
} LinkNode, *LinkList;
|
||||
struct LinkListNode* next;
|
||||
} LinkListNode, *LinkList;
|
||||
|
||||
// 初始化有头节点单链表
|
||||
int InitLinkListWithHead(LinkList list) {
|
||||
list = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
list = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (list == NULL) {
|
||||
printf("InitLinkListWithHead:初始化分配内存失败!");
|
||||
return 1;
|
||||
@@ -52,7 +52,7 @@ int InsertLinkListWithHead(LinkList list, int index, element_type elem) {
|
||||
return 1;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkNode* p;
|
||||
LinkListNode* p;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 0;
|
||||
// 将链表头结点指向p,为第0个结点
|
||||
@@ -69,7 +69,7 @@ int InsertLinkListWithHead(LinkList list, int index, element_type elem) {
|
||||
return 1;
|
||||
}
|
||||
// 此时i==index-1
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (s == NULL) {
|
||||
printf("InsertLinkListWithHead:分配内存失败!\n");
|
||||
return 1;
|
||||
@@ -88,7 +88,7 @@ int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) {
|
||||
return 1;
|
||||
}
|
||||
if (index == 0) {
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (s == NULL) {
|
||||
printf("InsertLinkListWithoutHead:分配内存失败!\n");
|
||||
return 1;
|
||||
@@ -101,7 +101,7 @@ int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) {
|
||||
return 0;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkNode* p;
|
||||
LinkListNode* p;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 0;
|
||||
// 将链表头结点指向p,为第0个结点
|
||||
@@ -118,7 +118,7 @@ int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) {
|
||||
return 1;
|
||||
}
|
||||
// 此时i==index-1
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
s->data = elem;
|
||||
// 将p原来的后继给新的结点
|
||||
s->next = p->next;
|
||||
@@ -127,15 +127,15 @@ int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) {
|
||||
}
|
||||
|
||||
// 后插入单链表元素
|
||||
int InsertNextLinkNode(LinkNode* node, element_type elem) {
|
||||
int InsertNextLinkListNode(LinkListNode* node, element_type elem) {
|
||||
if (node == NULL) {
|
||||
printf("InsertNextLinkNode:插入结点为空!");
|
||||
printf("InsertNextLinkListNode:插入结点为空!");
|
||||
return 1;
|
||||
}
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
// 如果分配空间失败
|
||||
if (s == NULL) {
|
||||
printf("InsertNextLinkNode:分配内存失败!\n");
|
||||
printf("InsertNextLinkListNode:分配内存失败!\n");
|
||||
return 1;
|
||||
}
|
||||
s->data = elem;
|
||||
@@ -145,15 +145,15 @@ int InsertNextLinkNode(LinkNode* node, element_type elem) {
|
||||
}
|
||||
|
||||
// 前插入单链表元素
|
||||
int InsertPriorLinkNode(LinkNode* node, element_type elem) {
|
||||
int InsertPriorLinkListNode(LinkListNode* node, element_type elem) {
|
||||
if (node == NULL) {
|
||||
printf("InsertPriorLinkNode:插入结点为空!");
|
||||
printf("InsertPriorLinkListNode:插入结点为空!");
|
||||
return 1;
|
||||
}
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
// 如果分配空间失败
|
||||
if (s == NULL) {
|
||||
printf("InsertPriorLinkNode:分配内存失败!\n");
|
||||
printf("InsertPriorLinkListNode:分配内存失败!\n");
|
||||
return 1;
|
||||
}
|
||||
s->next = node->next;
|
||||
@@ -170,7 +170,7 @@ int DeleteLinkListWithHead(LinkList list, int index, element_type *elem) {
|
||||
return 1;
|
||||
}
|
||||
// p指向当前扫描的结点
|
||||
LinkNode* p;
|
||||
LinkListNode* p;
|
||||
// i表示当前指向的是第几个结点
|
||||
int i = 0;
|
||||
// 指向头结点
|
||||
@@ -184,7 +184,7 @@ int DeleteLinkListWithHead(LinkList list, int index, element_type *elem) {
|
||||
return 1;
|
||||
}
|
||||
// q指向被删除的结点
|
||||
LinkNode* q = p->next;
|
||||
LinkListNode* q = p->next;
|
||||
// 获取删除的元素数据
|
||||
*elem = q->data;
|
||||
// 将q结点从链表中断开
|
||||
@@ -200,7 +200,7 @@ int DeleteLinkListWithHead(LinkList list, int index, element_type* elem) {
|
||||
return 1;
|
||||
}
|
||||
// p指向当前扫描的结点
|
||||
LinkNode* p;
|
||||
LinkListNode* p;
|
||||
// i表示当前指向的是第几个结点
|
||||
int i = 0;
|
||||
// 指向头结点
|
||||
@@ -220,7 +220,7 @@ int DeleteLinkListWithHead(LinkList list, int index, element_type* elem) {
|
||||
return 1;
|
||||
}
|
||||
// q指向被删除的结点
|
||||
LinkNode* q = p->next;
|
||||
LinkListNode* q = p->next;
|
||||
// 获取删除的元素数据
|
||||
*elem = q->data;
|
||||
// 将q结点从链表中断开
|
||||
@@ -230,18 +230,18 @@ int DeleteLinkListWithHead(LinkList list, int index, element_type* elem) {
|
||||
}
|
||||
|
||||
// 删除单链表元素
|
||||
int DeleteLinkNode(LinkNode* node) {
|
||||
int DeleteLinkListNode(LinkListNode* node) {
|
||||
if (node == NULL) {
|
||||
printf("DeleteLinkNode:本结点是空结点无法删除!");
|
||||
printf("DeleteLinkListNode:本结点是空结点无法删除!");
|
||||
return 1;
|
||||
}
|
||||
// 如果该结点为最后一个结点,无法找到前驱结点,无法操作
|
||||
if (node->next = NULL) {
|
||||
printf("DeleteLinkNode:后继结点为空无法操作!");
|
||||
printf("DeleteLinkListNode:后继结点为空无法操作!");
|
||||
return 1;
|
||||
}
|
||||
// 指向后继结点
|
||||
LinkNode* p = node->next;
|
||||
LinkListNode* p = node->next;
|
||||
// 交换数据
|
||||
node->data = p->data;
|
||||
// 断开结点
|
||||
@@ -257,7 +257,7 @@ element_type GetLinkListElement(LinkList list, int index) {
|
||||
return NULL;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkNode* p;
|
||||
LinkListNode* p;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 0;
|
||||
// 将链表头结点指向p,为第0个结点
|
||||
@@ -277,13 +277,13 @@ element_type GetLinkListElement(LinkList list, int index) {
|
||||
}
|
||||
|
||||
// 按位查找单链表结点
|
||||
LinkNode* GetLinkListNode(LinkList list, int index) {
|
||||
LinkListNode* GetLinkListNode(LinkList list, int index) {
|
||||
if (index < 0) {
|
||||
printf("GetLinkListNode:查找索引值过小!\n");
|
||||
return NULL;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkNode* p;
|
||||
LinkListNode* p;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 0;
|
||||
// 将链表头结点指向p,为第0个结点
|
||||
@@ -304,8 +304,8 @@ LinkNode* GetLinkListNode(LinkList list, int index) {
|
||||
|
||||
|
||||
// 按值查找单链表结点
|
||||
LinkNode* LocateLinkListNode(LinkList list, element_type elem) {
|
||||
LinkNode* p = list;
|
||||
LinkListNode* LocateLinkListNode(LinkList list, element_type elem) {
|
||||
LinkListNode* p = list;
|
||||
while (p != NULL && p->data != elem) {
|
||||
p = p->next;
|
||||
}
|
||||
@@ -315,7 +315,7 @@ LinkNode* LocateLinkListNode(LinkList list, element_type elem) {
|
||||
// 求链表长度
|
||||
int GetLength(LinkList list) {
|
||||
int len = 0;
|
||||
LinkNode* p = list;
|
||||
LinkListNode* p = list;
|
||||
while (p->next != NULL) {
|
||||
p = p->next;
|
||||
len++;
|
||||
@@ -326,9 +326,9 @@ int GetLength(LinkList list) {
|
||||
// 后插建立带头节点单链表
|
||||
LinkList TailBuildLinkListWithHead(LinkList list, int length) {
|
||||
element_type elem;
|
||||
list = (LinkList)malloc(sizeof(LinkNode));
|
||||
list = (LinkList)malloc(sizeof(LinkListNode));
|
||||
// s指针为一个中间变量指针,r指针为尾指针(next指向最后一个元素)
|
||||
LinkNode* s, * r = list;
|
||||
LinkListNode* s, * r = list;
|
||||
int i = 0;
|
||||
element_type x;
|
||||
if (length < 1) {
|
||||
@@ -337,7 +337,7 @@ LinkList TailBuildLinkListWithHead(LinkList list, int length) {
|
||||
}
|
||||
while (i < length) {
|
||||
scanf("%d", &x);
|
||||
s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
s->data = x;
|
||||
r->next = s;
|
||||
r = s;
|
||||
@@ -350,11 +350,11 @@ LinkList TailBuildLinkListWithHead(LinkList list, int length) {
|
||||
// 前插建立带头节点单链表
|
||||
LinkList HeadBuildLinkListWithHead(LinkList list, int length) {
|
||||
element_type elem;
|
||||
list = (LinkList)malloc(sizeof(LinkNode));
|
||||
list = (LinkList)malloc(sizeof(LinkListNode));
|
||||
// 将单链表尾部设置为NULL
|
||||
list->next = NULL;
|
||||
// s指针为一个中间变量指针
|
||||
LinkNode* s;
|
||||
LinkListNode* s;
|
||||
int i = 0;
|
||||
element_type x;
|
||||
if (length < 1) {
|
||||
@@ -363,7 +363,7 @@ LinkList HeadBuildLinkListWithHead(LinkList list, int length) {
|
||||
}
|
||||
while (i < length) {
|
||||
scanf("%d", &x);
|
||||
s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
s->data = x;
|
||||
s->next = list->next;
|
||||
list->next = s;
|
||||
@@ -374,7 +374,7 @@ LinkList HeadBuildLinkListWithHead(LinkList list, int length) {
|
||||
|
||||
// 初始化有头节点循环单链表
|
||||
int InitCircularLinkListWithHead(LinkList list) {
|
||||
list = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
list = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (list == NULL) {
|
||||
printf("InitCircularLinkListWithHead:初始化分配内存失败!");
|
||||
return 1;
|
||||
@@ -394,7 +394,7 @@ int IsCircularLinkListEmptyWithHead(LinkList list) {
|
||||
}
|
||||
|
||||
// 判断结点是否尾有头节点循环单链表的尾结点
|
||||
int IsCircularLinkListEndWithHead(LinkList list, LinkNode* node) {
|
||||
int IsCircularLinkListEndWithHead(LinkList list, LinkListNode* node) {
|
||||
if (node->next == list) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
9
Code/link_stack.h
Normal file
9
Code/link_stack.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 链栈结点
|
||||
typedef struct LinkStackNode {
|
||||
element_type data;
|
||||
struct LinkStackNode* next;
|
||||
} LinkStackNode, * LinkStack;
|
||||
57
Code/sequence_queue.h
Normal file
57
Code/sequence_queue.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 顺序队列
|
||||
typedef struct {
|
||||
// 数组存放队列元素
|
||||
element_type data[MAXSIZE];
|
||||
// 声明队头指针与队尾指针,其指的是队开始索引与结束索引
|
||||
int front, rear;
|
||||
} SequenceQueue;
|
||||
|
||||
// 初始化顺序队列
|
||||
int InitSequenceQueue(SequenceQueue* queue) {
|
||||
// 队首队尾都指向0
|
||||
queue->rear = queue->front = 0;
|
||||
}
|
||||
|
||||
// 顺序队列元素入队
|
||||
int EnterSequenceQueue(SequenceQueue* queue, element_type elem) {
|
||||
// 如果队尾指针后一个就是队头指针那就是队满
|
||||
if ((queue->rear + 1) % MAXSIZE == queue->front) {
|
||||
printf("EnterSequenceQueue:队列已满无法继续进队!");
|
||||
return 1;
|
||||
}
|
||||
// 根据队尾指针插入元素
|
||||
queue->data[queue->rear] = elem;
|
||||
// 对队尾指针进行取余,从而让队尾指针指向队列前面的空闲空间
|
||||
queue->rear = (queue->rear + 1) % MAXSIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 顺序队列元素出队
|
||||
int ExitSequenceQueue(SequenceQueue* queue, element_type* elem) {
|
||||
// 如果队尾指针就是队头指针那就是队空
|
||||
if (queue->rear == queue->front) {
|
||||
printf("ExitSequenceQueue:队列已空无法继续出队!");
|
||||
return 1;
|
||||
}
|
||||
// 根据队头指针删除元素
|
||||
*elem = queue->data[queue->front];
|
||||
// 对队头指针进行取余,从而让队头指针指向队列后面的空闲空间
|
||||
queue->front = (queue->front + 1) % MAXSIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 获取顺序队列队头元素
|
||||
int GetSequenceQueueHead(SequenceQueue* queue, element_type* elem) {
|
||||
// 如果队尾指针就是队头指针那就是队空
|
||||
if (queue->rear == queue->front) {
|
||||
printf("GetSequenceQueueHead:队列已空无法获取元素!");
|
||||
return 1;
|
||||
}
|
||||
// 根据队头指针赋值元素
|
||||
*elem = queue->data[queue->front];
|
||||
return 0;
|
||||
}
|
||||
67
Code/sequence_stack.h
Normal file
67
Code/sequence_stack.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 顺序栈
|
||||
typedef struct {
|
||||
// 静态数组存储栈元素
|
||||
element_type data[MAXSIZE];
|
||||
// 栈顶指针
|
||||
int top;
|
||||
} SequenceStack;
|
||||
|
||||
// 初始化顺序栈
|
||||
int InitSequenceStack(SequenceStack* stack) {
|
||||
stack->top = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 判断顺序栈是否为空
|
||||
int IsSequenceStackEmpty(SequenceStack stack) {
|
||||
if (stack.top == -1) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 将元素推入顺序栈
|
||||
int PushSequenceStack(SequenceStack* stack, element_type elem) {
|
||||
// 栈满无法添加元素
|
||||
if (stack->top == MAXSIZE - 1) {
|
||||
printf("PushSequenceStack:栈满无法继续推入元素!");
|
||||
return 1;
|
||||
}
|
||||
//// 指针加一
|
||||
//stack->top += 1;
|
||||
//// 新元素入栈
|
||||
//stack->data[stack->top] = elem;
|
||||
// 加一并入栈
|
||||
stack->data[++stack->top] = elem;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 将元素弹出顺序栈
|
||||
int PopSequenceStack(SequenceStack* stack, element_type* elem) {
|
||||
// 栈空无法删除元素
|
||||
if (stack->top == -1) {
|
||||
printf("PopSequenceStack:栈空无法继续弹出元素!");
|
||||
return 1;
|
||||
}
|
||||
// 弹出后再出栈
|
||||
*elem = stack->data[stack->top--];
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 读取顺序栈栈顶元素
|
||||
int GetSequenceStackTop(SequenceStack* stack, element_type* elem) {
|
||||
// 栈空无法获取元素
|
||||
if (stack->top == -1) {
|
||||
printf("PopSequenceStack:栈空无法读取元素!");
|
||||
return 1;
|
||||
}
|
||||
// 弹出后再出栈
|
||||
*elem = stack->data[stack->top];
|
||||
return 0;
|
||||
}
|
||||
@@ -7,4 +7,6 @@ typedef struct StaticLinkNode {
|
||||
element_type data;
|
||||
// 下一个元素的数组下标
|
||||
int next;
|
||||
} DoubleLinkNode, * DoubleLinkList;
|
||||
} StaticLinkNode;
|
||||
// 静态链表数组
|
||||
typedef StaticLinkNode StaticLinkList[MAXSIZE];
|
||||
Reference in New Issue
Block a user