1. 栈
1.1 定义
- 基本操作
1.2 栈的顺序存储
- 栈为空
- 栈满
1.2.1 元素入栈
bool Push(SqStack& S, ElemType x)
{
if (S.top == MaxSize - 1)
{
return false;
}
S.data[++S.top] = x;
return true;
}
1.2.2 元素出栈
bool Pop(SqStack& S, ElemType& x)
{
if (StackEmpty(S))
{
return false;
}
x = S.data[S.top];
S.top--;
return true;
}
1.3 栈的链表存储
- 元素入栈
- 元素出栈,Lhead后面的元素就是栈顶元素(top)
- 栈满
1.4 总程序
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S)
{
S.top = -1;
}
bool StackEmpty(SqStack S)
{
if (S.top == -1)
{
return true;
}
return false;
}
bool Push(SqStack& S, ElemType x)
{
if (S.top == MaxSize - 1)
{
return false;
}
S.data[++S.top] = x;
return true;
}
bool GetTop(SqStack S, ElemType &x)
{
if (StackEmpty(S))
{
return false;
}
x = S.data[S.top];
return true;
}
bool Pop(SqStack& S, ElemType& x)
{
if (StackEmpty(S))
{
return false;
}
x = S.data[S.top];
S.top--;
return true;
}
int main()
{
SqStack S;
bool flag;
ElemType m;
InitStack(S);
flag = StackEmpty(S);
if (flag)
{
printf("栈是空的\n");
}
Push(S, 3);
Push(S, 4);
Push(S, 5);
flag = GetTop(S, m);
if (flag)
{
printf("获取栈顶元素为%d\n", m);
}
flag = Pop(S, m);
if (flag)
{
printf("弹出元素为%d\n", m);
}
return 0;
}
2. 循环队列
2.1 定义
- 初始化
2.2 元素入队
bool EnQueue(SqQueue& Q, ElemType x)
{
if ((Q.rear + 1) % MaxSize == Q.front)
{
return false;
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
2.3 元素出队
bool DeQueue(SqQueue &Q, ElemType &x)
{
if (Q.front == Q.rear)
{
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
2.4 总程序
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 5
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int front, rear;
}SqQueue;
void InitQueue(SqQueue& Q)
{
Q.front = 0;
Q.rear = 0;
}
bool isEmpty(SqQueue Q)
{
if (Q.front == Q.rear)
{
return true;
}
return false;
}
bool EnQueue(SqQueue& Q, ElemType x)
{
if ((Q.rear + 1) % MaxSize == Q.front)
{
return false;
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x)
{
if (Q.front == Q.rear)
{
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
int main()
{
SqQueue Q;
InitQueue(Q);
bool ret;
ret = isEmpty(Q);
if (ret)
{
printf("队列为空\n");
}
else
{
printf("队列不为空\n");
}
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
ret = EnQueue(Q, 6);
if (ret)
{
printf("入队成功\n");
}
else
{
printf("入队失败\n");
}
ElemType element;
ret = DeQueue(Q, element);
if (ret)
{
printf("出队成功,元素值为%d\n", element);
}
else
{
printf("出队失败\n");
}
ret = DeQueue(Q, element);
if (ret)
{
printf("出队成功,元素值为%d\n", element);
}
else
{
printf("出队失败\n");
}
return 0;
}
2.5 队列的链式存储
- 添加尾指针
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode
{
ElemType data;
struct LinkNode* next;
}LinkNode;
typedef struct
{
LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q)
{
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
bool IsEmpty(LinkQueue Q)
{
if (Q.front == Q.rear)
{
return true;
}
else
{
return false;
}
}
void EnQueue(LinkQueue& Q, ElemType x)
{
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x;
s->next = NULL;
Q.rear->next = s;
Q.rear = s;
}
bool DeQueue(LinkQueue& Q, ElemType &x)
{
if (Q.front == Q.rear)
return false;
LinkNode* p = Q.front->next;
x = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return true;
}
int main()
{
LinkQueue Q;
bool ret;
ElemType element;
InitQueue(Q);
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
EnQueue(Q, 6);
EnQueue(Q, 7);
ret = DeQueue(Q, element);
if (ret)
{
printf("出队成功,元素值为%d\n", element);
}
else
{
printf("出队失败\n");
}
}
3. 中级Day4作业
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode* next;
}LNode, * LinkList;
LinkList CreateList1(LinkList& L)
{
LNode* s; int x;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
scanf("%d", &x);
while (x != 9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d", &x);
}
return L;
}
LinkList CreateList2(LinkList& L)
{
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;
scanf("%d", &x);
while (x != 9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return L;
}
void PrintList(LinkList L)
{
L = L->next;
while (L != NULL)
{
printf("%d", L->data);
L = L->next;
if (L != NULL)
{
printf(" ");
}
}
printf("\n");
}
int main()
{
LinkList L = NULL;
LinkList search;
CreateList1(L);
PrintList(L);
CreateList2(L);
PrintList(L);
return 0;
}
4. 中级Day5作业
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode* next;
}LNode, * LinkList;
LinkList CreateList2(LinkList& L)
{
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;
scanf("%d", &x);
while (x != 9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return L;
}
LNode* GetElem(LinkList L, int i)
{
int j = 1;
LNode* p = L->next;
if (i == 0)
{
return L;
}
if (i < 1)
{
return NULL;
}
while (p && j < i)
{
p = p->next;
j++;
}
return p;
}
bool ListFrontInsert(LinkList L, int i, ElemType e)
{
LinkList p = GetElem(L, i - 1);
if (p == NULL)
{
return false;
}
LinkList s = (LNode*)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool ListDelete(LinkList L, int i)
{
LinkList p = GetElem(L, i - 1);
if (p == NULL)
{
return false;
}
LinkList q = p->next;
if (q == NULL)
{
return false;
}
p->next = q->next;
free(q);
q = NULL;
return true;
}
void PrintList(LinkList L)
{
L = L->next;
while (L != NULL)
{
printf("%3d", L->data);
L = L->next;
}
printf("\n");
}
int main()
{
LinkList L;
LinkList search;
CreateList2(L);
search = GetElem(L, 2);
if (search != NULL)
{
printf("%d\n", search->data);
}
ListFrontInsert(L, 2, 99);
PrintList(L);
ListDelete(L, 4);
PrintList(L);
return 0;
}