对应源码:
/* *********
date:2022 - 10 - 04
author : kyh
version : 1.0
Description : 实现顺序栈,链栈和循环队列
* ***********/
//①****************************************
#include<stdlib.h>
#include<stdio.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;
int size;//记录数组有效数据的多少
int capacity; //记录动态开辟的空间大小
}SeqList;
void InitList(SeqList *ps)
{
ps->a = NULL;
ps->capacity = ps->size = 0;
}
void CheckSeqList(SeqList* ps)//检查顺序表的容量大小,容量不够进行扩容
{
if (ps->size == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDataType * tmp = (SLDataType *)realloc(ps->a, sizeof(SLDataType) * newcapacity);
if (NULL == tmp)
{
perror("realloc fail\n");
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
}
void PutSeqList(SeqList* ps, SLDataType x)
{
CheckSeqList(ps);
ps->a[ps->size] = x;
ps->size++;
}
void OutPutSeqList(SeqList* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
int LengthList(SeqList* ps)
{
return ps->size;
}
int PositionList(SeqList* ps, SLDataType x)
{
int pos = 0;
while (ps->a[pos] < x)
{
pos++;
}
return pos;
}
void InsertList(SeqList* ps, int pos, SLDataType x)
{
CheckSeqList(ps);
for (int i = ps->size; i > pos; i--)
{
ps->a[i] = ps->a[i - 1];
}
ps->a[pos] = x;
ps->size++;
}
int main()
{
int length = 0;
int n = 0;
int input = 0;
SeqList s;
InitList(&s);
printf("Please input the length of list:");
scanf("%d", &length);
printf("Please input the list:");
while (length)
{
scanf("%d", &n);
PutSeqList(&s, n);
length--;
}
printf("Output:");
OutPutSeqList(&s);
//④验证求长度函数
printf("此时数组的长度为:%d\n\n", LengthList(&s));
//⑤验证查找和插入函数
printf("请输入你要插入的数据:");
if (~scanf("%d", &input))
{
printf("此时插入的下标为:%d\n", PositionList(&s,input));
InsertList(&s, PositionList(&s, input), input);
printf("Output:");
OutPutSeqList(&s);
printf("此时数组的长度为:%d\n\n", LengthList(&s));
}
return 0;
}
//②****************************************
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node* next;
}*linkstack;
void Push(linkstack* top, datatype x)
{
linkstack s = (linkstack)malloc(sizeof(struct node));
if (s == NULL)
exit(-1);
s->data = x;
s->next = (*top);
(*top) = s;
}
int Empty(linkstack top)
{
return top == NULL;
}
void Pop(linkstack* top, datatype* x)
{
if (!Empty(*top))
{
linkstack del = (*top);
*x = del->data;
(*top) = (*top)->next;
free(del);
}
else
exit(-1);
}
void Convert(int num, int mode)
{
int h;
linkstack top = NULL;
printf("转化结果为:");
if (num > 0)
{
while (num != 0)
{
h = num % mode;
Push(&top, h);
num = num / mode;
}
while (!Empty(top))
{
Pop(&top, &h);
printf("%d ", h);
}
puts("");
}
else if (num < 0)
{
printf("-");
num = num * (-1);
while (num != 0)
{
h = num % mode;
Push(&top, h);
num = num / mode;
}
while (!Empty(top))
{
Pop(&top, &h);
printf("%d ", h);
}
puts("");
}
else
printf("%d\n",0);
}
void main()
{
int num, mode;
printf("输入要转化的数:");
scanf("%d", &num);
printf("输入要转化的进制:");
scanf("%d", &mode);
Convert(num, mode);
}
//③****************************************
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#define maxsize 20
typedef int datatype;
typedef struct Queue
{
datatype data[maxsize];
int front, rear;
int num;
}*SeQueue;
void InitSeQueue(SeQueue* Q)
{
(*Q) = (SeQueue)malloc(sizeof(struct Queue));
if (NULL == (*Q))
{
exit(-1);
}
(*Q)->front = -1;
(*Q)->rear = -1;
(*Q)->num = 0;
}
int IsEmpty(SeQueue Q)
{
return Q->num == 0;
}
int IsFull(SeQueue Q)
{
return Q->num == maxsize - 1;
}
void In_SeQueue(SeQueue* Q, datatype x)
{
if (!IsFull(*Q))
{
(*Q)->rear = ((*Q)->rear + 1) % maxsize;
(*Q)->data[(*Q)->rear] = x;
(*Q)->num++;
}
else
{
exit(-1);
}
}
void Out_SeQueue(SeQueue* Q, datatype* x)
{
if (!IsEmpty(*Q))
{
(*Q)->front = ((*Q)->front + 1) % maxsize;
*x = (*Q)->data[(*Q)->front];
(*Q)->num--;
}
else
{
exit(-1);
}
}
void main()
{
datatype x; int i;
SeQueue q;
InitSeQueue(&q);
printf("请输入10个整数元素入队列:");
for (int i = 0; i < 10; i++)
{
scanf("%d", &x);
In_SeQueue(&q, x);
}
printf("出队列并输出:");
for (i = 0; i < 10; i++)
{
Out_SeQueue(&q, &x);
printf(" %d ", x);
}
puts("");
}
对应结果: