1. 程式人生 > >C語言——棧的實現(順序棧,兩個順序棧共享空間,鏈式棧)

C語言——棧的實現(順序棧,兩個順序棧共享空間,鏈式棧)

1.什麼是棧

棧是一種只能在一端進行插入或者刪除操作的線性表)。其中允許進行插入或者刪除操作的一端稱為棧頂。棧的插入和刪除一般叫入棧和出棧。棧的順序儲存結構叫做==順序棧==,棧的鏈式儲存結構叫做==鏈棧==。

2.棧的特點

棧的特點是==後進先出==

3.順序棧

標頭檔案

#ifndef Stack
#define Stack
#define SIZE 10
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef
struct data { int num[SIZE]; int top; }stack; bool init(stack * st); bool push(stack * s, int n); bool pop(stack * s); void print(stack * s); #endif

資原始檔

#include"stack.h"
bool init(stack * st)
{
    st->top = -1;
    return true;
}
bool push(stack * s, int n)
{
    if (s->top == SIZE - 1
) { return false; } s->top++; s->num[s->top] = n; printf("%d已入棧\n", n); return true; } bool pop(stack * s) { if (s->top == -1) { return false; } printf("%d已被彈出\n", s->num[s->top]); s->top--; return true; } void print(stack * s) { while
(s->top != -1) { printf("%d\n", s->num[s->top--]); } return; }

主函式

#include"stack.h"
int main(void)
{
    stack * a;
    a = (stack *)malloc(sizeof(stack));
    init(a);
    puts("請輸入您需要入棧的個數");
    int n;
    scanf("%d", &n);
    int t;//臨時變數
    for (int i = 0; i < n; i++) {
        printf("請輸入值:");
        scanf("%d", &t);
        if (!push(a, t)) {
            printf("棧已滿!\n");
        }
    }
    if (!pop(a)) {
        printf("棧是空的!\n");
    }
    print(a);
    return 0;
}

4.兩棧共享空間

思想:在順序棧的兩個頭都進行進棧出棧操作。

注意:棧是否滿,以及棧是否空的判斷條件是什麼?


棧空:對棧底而言,top == -1即為空,對棧頂而言,top2 == SIZE即為棧空。

棧滿:top + 1 == top2即為棧滿


程式碼如下


stack.h

#ifndef STACK
#define STACK
#define SIZE 10
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct stack {
    int top;
    int top2;
    int data[SIZE];
}stack;
void init(stack * s);
bool Pop(stack * s, int flag);
bool Push(stack * s, int n, int flag);
#endif

stack.c

#include"stack.h"
void init(stack * s)
{
    s->top = -1;
    s->top2 = SIZE;
    return;
}
bool Push(stack * s,int n, int flag)
{
    if (s->top2 + 1 == s->top2) {
        printf("棧已滿!\n");
        return false;
    }
    if (flag == 1) {
        s->data[s->top] = n;
        s->top++;
        printf("%d已入棧!\n", n);
    }
    else if (flag == 2) {
        s->data[s->top2] = n;
        s->top2--;
        printf("%d已入棧!\n", n);
    }

    return true;
}
bool Pop(stack * s, int flag)
{
    if (flag == 1) {
        if (s->top == -1) {
            printf("空棧\n");
            return false;
        }
        printf("%d已出棧!\n", s->data[s->top--]);
    }
    if (flag == 2) {
        if (s->top2 == SIZE) {
            printf("空棧\n");
            return false;
        }
        printf("%d已出棧!\n", s->data[s->top2++]);
    }
    return true;
}

c.c

#include"stack.h"
int main(int argc, char * argv[])
{
    stack * s;
    s = (stack*)malloc(sizeof(stack));
    init(s);
    int flag;
    int val;//臨時變數
    int n;
    printf("請輸入您要操作的棧的序號(1 normal), (2 reverse):");
    scanf("%d", &flag);
    printf("請輸入您需要入棧的個數:");
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &val);
        Push(s, val, flag);
    }
    Pop(s, 1);
    return 0;
}

5.鏈式棧

結構與單鏈表相同,不同的是,需要將一個==top==指標,指向該連結串列的頭部,需要注意的是,每次的操作,都是通過top指標來完成的,程式碼如下:

標頭檔案

#ifndef STACK
#define STACK
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define SIZE 1000
typedef struct node {
    int data;
    struct node * next;
}stacknode;
typedef struct node2 {
    stacknode * top;
    int count;
}stack;
void init(stack * s);
bool push(stack * s, int n);
bool pop(stack * s);
void print(stack * s);
//void print(stack * s);
#endif

stack.c檔案

#include"stack.h"
void init(stack *s)
{
    s->top = NULL;
    s->count = 0;
}
bool push(stack * s, int n)
{
    stacknode *p = (stacknode *)malloc(sizeof(stacknode));
    p->data = n;
    p->next = s->top;
    s->top = p;
    s->count++;
    printf("%d已入棧\n", p->data);
    return true;
}
bool pop(stack * s)
{
    if (s->top == NULL) {
        return false;
    }
    stacknode * p;
    p = s->top;
    s->top = s->top->next;
    s->count--;
    printf("%d已出棧\n", p->data);
    free(p);
    return true;
}
void print(stack * s)
{
    stacknode * p;
    p = s->top;
    if (p == NULL) {
        printf("No data\n");
        return;
    }
    while (p) {
        printf("%d ", p->data);
        p = p->next;
    }
    putchar('\n');
    return;
}

c.c

#include"stack.h"
int main(void)
{
    stack * s = (stack *)malloc(sizeof(stack));
    init(s);
    printf("請輸入您需要入棧的個數:");
    int n;
    scanf("%d", &n);
    int t;
    for (int i = 0; i < n; i++) {
        scanf("%d", &t);
        push(s, t);
    }
    printf("該棧中一共有%d項\n",s->count);
    pop(s);
    print(s);
    return 0;
}