1.栈的数组实现
#include <stdio.h>
int s[1000010];//定义数组
int tt=0;//栈顶
void push(int x){//入栈
s[++tt]=x;
}
int isempty(){//判断栈是否为空
if(tt>0){
return 0;
}else{
return 1;
}
}
void pop(){//出栈
tt--;
}
void top(){//获取栈顶元素
printf("%d\n",s[tt]);
}
void traverse(){//遍历
for(int i=1;i<=tt;i++){
printf("%d->",s[i]);
}
printf("NULL");
}
int main(){
push(1);
push(2);
push(3);
top();
pop();
traverse();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
typedef struct STACK {
int* data;
int top;
int capacity;
}STACK,*PSTACK;
PSTACK init() {
PSTACK stack = (PSTACK)malloc(sizeof(STACK));
stack->data = (int*)malloc(sizeof(int) * MAX);
stack->capacity = MAX;
stack->top = -1;
return stack;
}
int isempty(PSTACK stack) {
if (stack->top == -1) {
return 1;
}
else
{
return 0;
}
}
int isfull(PSTACK stack) {
if (stack->top == stack->capacity-1) {
return 1;
}
else
{
return 0;
}
}
void push(PSTACK stack, int num) {
if (isfull(stack)) {
stack->capacity *= 2;
int* newarr = realloc(stack->data, sizeof(int) * stack->capacity);
stack->data = newarr;
}
stack->data[++stack->top] = num;
}
void pop(PSTACK stack) {
if (isempty(stack)) {
return;
}
printf("pop=%d\n", stack->data[stack->top--]);
}
void traverse(PSTACK stack) {
for (int i = 0; i <= stack->top; i++) {
printf("%d->", stack->data[i]);
}
printf("NULL");
}
int main() {
PSTACK stack = init();
push(stack, 1);
push(stack, 2);
push(stack, 3);
push(stack, 4);
push(stack, 5);
push(stack, 6);
push(stack, 7);
pop(stack);
traverse(stack);
return 0;
}
2.栈的链表实现
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
int data;
struct NODE* next;
}NODE,*PNODE;
typedef struct STACK {
struct NODE* top;
struct NODE* bottom;
}STACK,*PSTACK;
PSTACK init() {
PSTACK stack = (PSTACK)malloc(sizeof(STACK));
stack->top = stack->bottom = (PNODE)malloc(sizeof(NODE));
stack->bottom->data = 0;
stack->bottom->next = NULL;
return stack;
}
int isempty(PSTACK stack) {
if (stack->bottom->data == 0) {
return 1;
}
else
{
return 0;
}
}
void push(PSTACK stack, int num) {
PNODE NEWNODE = (PNODE)malloc(sizeof(NODE));
NEWNODE->data = num;
NEWNODE->next = stack->top;
stack->top = NEWNODE;
stack->bottom->data++;
}
void pop(PSTACK stack) {
PNODE temp = stack->top;
stack->top = stack->top->next;
printf("pop=%d\n", temp->data);
free(temp);
stack->bottom->data--;
}
void traverse(PSTACK stack) {
if (isempty(stack)) {
printf("kong\n");
return;
}
PNODE temp = stack->top;
while (temp!=stack->bottom) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
PSTACK stack = init();
push(stack, 1);
push(stack, 2);
push(stack, 3);
push(stack, 4);
pop(stack);
traverse(stack);
return 0;
}
3.队列的数组实现
#include <stdio.h>
int q[100010];
int hh=0,tt=-1;
void push(int x){
q[++tt]=x;
}
int isempty(){
if(hh<=tt){
return 0;
}else{
return 1;
}
}
void pop(){
if(isempty()){
printf("栈已空\n");
return;
}
hh++;
}
void gethh(){
printf("%d\n",q[hh]);
}
void traverse(){
for(int i=hh;i<=tt;i++){
printf("%d->",q[i]);
}
printf("NULL");
}
int main(){
push(1);
push(2);
push(3);
gethh();
pop();
pop();
pop();
pop();
traverse();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 6
typedef struct QUEUE {
int* data;
int front;
int rear;
}QUEUE,*PQUEUE;
PQUEUE init() {
PQUEUE Q = (PQUEUE)malloc(sizeof(QUEUE));
Q->data = (int*)malloc(sizeof(int) * MAX);
Q->front = Q->rear = 0;
return Q;
}
int isempty(PQUEUE Q) {
if (Q->front == Q->rear) {
return 1;
}
else
{
return 0;
}
}
int isfull(PQUEUE Q) {
if ((Q->rear + 1) % MAX == Q->front) {
return 1;
}
else
{
return 0;
}
}
void enqueue(PQUEUE Q, int num) {
if (isfull(Q)) {
return;
}
Q->data[Q->rear] = num;
Q->rear = (Q->rear + 1) % MAX;
}
void dequeue(PQUEUE Q) {
if (isempty(Q)) {
return;
}
printf("dequeue=%d\n", Q->data[Q->front]);
Q->front = (Q->front + 1) % MAX;
}
void traverse(PQUEUE Q) {
if (isempty(Q)) {
printf("此队列为空\n");
return;
}
for (int i = Q->front; i != Q->rear; i = (i + 1) % MAX) {
printf("%d->", Q->data[i]);
}
printf("NULL");
}
int main() {
PQUEUE Q = init();
enqueue(Q, 1);
enqueue(Q, 2);
enqueue(Q, 3);
enqueue(Q, 4);
dequeue(Q);
enqueue(Q, 5);
traverse(Q);
return 0;
}
4.队列的链表实现
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
int data;
struct NODE* next;
}NODE,*PNODE;
typedef struct QUEUE {
PNODE front;
PNODE rear;
}QUEUE,*PQUEUE;
PQUEUE init() {
PQUEUE Q = (PQUEUE)malloc(sizeof(QUEUE));
Q->front = Q->rear = (PNODE)malloc(sizeof(NODE));
Q->front->data = 0;
Q->front->next = NULL;
return Q;
}
int isempty(PQUEUE Q) {
if (Q->front->data==0) {
return 1;
}
else
{
return 0;
}
}
void enqueue(PQUEUE Q, int num) {
PNODE temp = Q->front;
while (temp->next) {
temp = temp->next;
}
PNODE NEWNODE = (PNODE)malloc(sizeof(NODE));
NEWNODE->data = num;
NEWNODE->next = temp->next;
temp->next = NEWNODE;
Q->rear = NEWNODE;
Q->front->data++;
}
void dequeue(PQUEUE Q) {
if (isempty(Q)) {
printf("队列为空,出队失败!\n");
return;
}
PNODE temp = Q->front->next;
printf("dequeue=%d\n", temp->data);
Q->front->next = temp->next;
Q->front->data--;
free(temp);
}
void traverse(PQUEUE Q) {
PNODE temp = Q->front->next;
while (temp) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL");
}
int main() {
PQUEUE Q = init();
enqueue(Q, 1);
enqueue(Q, 2);
enqueue(Q, 3);
enqueue(Q, 4);
enqueue(Q, 5);
dequeue(Q);
dequeue(Q);
traverse(Q);
return 0;
}