三、栈和队列
(一)栈
1.栈:线性结构的一种特殊形式,元素满足先入后出
(1)元素依次入栈
(2)栈顶元素出栈
2.元素入栈
(1)注意栈满
3.元素出栈
(1)注意栈空
4.代码实现
(1)顺序表实现
#include <stdio.h>
#include <stdlib.h>
#define Elem int
typedef struct Stack {
Elem* data;
int capacity;
int top;
}stack;
int init(stack* s,int capacity)
{
int flag = 0;
s->data = (Elem*)malloc(sizeof(Elem)*capacity);
if(s->data == NULL)
flag = -1;
s->capacity = capacity;
s->top = -1;
return flag;
}
int IsEmpty(stack* s)
{
int flag = 0;
if(s->top == -1)
flag = -1;
return flag;
}
int IsFull(stack* s)
{
int flag = 0;
if(s->top == s->capacity - 1)
flag = -1;
return flag;
}
int push(stack* s,Elem data)
{
int flag = 0;
if(IsFull(s))
flag = -1;
else
s->data[++s->top] = data;
return flag;
}
int pop(stack* s)
{
int flag = 0;
if(IsEmpty(s))
flag = -1;
else
s->top--;
return flag;
}
void print(stack s)
{
for(int i = 0;i <= s.top;i++)
printf("%d ",s.data[i]);
}
int main()
{
/* Write C code in this online editor and run it. */
stack s;
init(&s,5);
for(int i = 0;i < 6;i++)
push(&s,i);
for(int i = 0;i < 4;i++)
pop(&s);
print(s);
return 0;
}
(2)链表实现
#include <stdio.h>
#include <stdlib.h>
#define Elem int
typedef struct Linklist {
Elem data;
struct Linklist* next;
}linklist;
linklist* createNode(Elem data)
{
linklist* node = (linklist*)malloc(sizeof(linklist));
node->data = data;
node->next = NULL;
return node;
}
void push(linklist* head,Elem data)
{
while(head->next)
head = head->next;
head->next = createNode(data);
}
void pop(linklist* *phead)
{
linklist* temp = *phead;
if(*phead)
{
*phead = (*phead)->next;
free(temp);
}
}
void print(linklist* head)
{
while(head)
{
printf("%d ",head->data);
head = head->next;
}
}
int main()
{
/* Write C code in this online editor and run it. */
linklist* head;
head = createNode(0);
push(head,1);
push(head,2);
pop(&head);
pop(&head);
print(head);
return 0;
}
(二)队列
1.队列:线性结构的一种特殊形式,元素满足先入先出
(1)元素依次入队列
(2)队首元素出队列
2.元素入队
(1)注意队满,对满时,rear + 1 = front
3.元素出队
(1)注意队空,队空时,front = rear
(2)循环队列入队,rear = (rear + 1)%MAXSIZE
4.代码实现
(1)顺序表实现队列(循环队列)
#include <stdio.h>
#include <stdlib.h>
#define Elem int
typedef struct Queue {
Elem* data;
int capacity;
int rear;
int front;
}queue;
int init(queue* q,int capacity)
{
int flag = 0;
q->data = (Elem*)malloc(sizeof(Elem)*(capacity+1));
if(q->data == NULL)
flag = -1;
q->capacity = capacity;
q->rear = 0;
q->front = 0;
}
int IsFull(const queue* q)
{
int flag = 0;
if((q->rear+1)%(q->capacity+1) == q->front)
flag = -1;
return flag;
}
int Enqueue(queue* q,Elem data)
{
int flag = 0;
if(IsFull(q))
flag = -1;
else
{
q->data[q->rear] = data;
q->rear = (q->rear + 1)%(q->capacity+1);
}
return flag;
}
int IsEmpty(const queue* q)
{
int flag = 0;
if(q->front == q->rear)
flag = -1;
return flag;
}
int Dequeue(queue* q)
{
int flag = 0;
if(IsEmpty(q))
flag = -1;
else
q->front = (q->front+1)%(q->capacity+1);
}
void print(queue q)
{
int i = 0;
for(i = q.front;i != q.rear;)
{
printf("%d ",q.data[i]);
i = (i+1)%(q.capacity+1);
}
}
int main()
{
/* Write C code in this online editor and run it. */
queue q;
init(&q,5);
for(int i = 0;i < 6;i++)
Enqueue(&q,i);
Dequeue(&q);
Enqueue(&q,5);
print(q);
return 0;
}
(2)链表实现队列
#include <stdio.h>
#include <stdlib.h>
#define Elem int
typedef struct Linklist {
Elem data;
struct Linklist* next;
}linklist;
typedef struct Queue {
linklist* front;
linklist* rear;
}queue;
linklist* createNode(Elem data)
{
linklist* node = (linklist*)malloc(sizeof(linklist));
node->data = data;
node->next = NULL;
return node;
}
void init(queue* q)
{
q->front = NULL;
q->rear = NULL;
}
int Enqueue(queue* q,Elem data)
{
int flag = 0;
if(q->front == NULL)
{
q->front = createNode(data);
if(q->front == NULL)
flag = -1;
q->rear = q->front;
}
else
{
q->rear->next = createNode(data);
if(q->rear->next == NULL)
flag = -1;
q->rear = q->rear->next;
}
return flag;
}
int IsEmpty(const queue* q)
{
int flag = 0;
if(q->front == NULL)
flag = -1;
return flag;
}
int Dequeue(queue* q)
{
int flag = 0;
if(IsEmpty(q))
flag = -1;
else
{
linklist* temp = q->front;
q->front = q->front->next;
free(temp);
}
return flag;
}
void print(queue q)
{
while(q.front)
{
printf("%d ",q.front->data);
q.front = q.front->next;
}
}
int main()
{
/* Write C code in this online editor and run it. */
queue q;
init(&q);
int i = 0;
for(i = 0;i < 5;i++)
Enqueue(&q,i);
for(i = 0;i < 3;i++)
Dequeue(&q);
Enqueue(&q,5);
print(q);
return 0;
}