数据结构栈的操作

#define MAXSIZE 10
#include <stdio.h>
顺序栈
typedef struct stack{
    int data[MAXSIZE];
    int top;
}Stack;
Stack* initStack();
void push(Stack *s,int x);
void pop(Stack *s,int *x);
int isEmpty(Stack *s);
int isFull(Stack *s);
int getTopData(Stack *s);
Stack* initStack(){
    Stack* s
    s->top = -1;
    return s;
}
int isEmpty(Stack *s){
    if (s->top!=-1) {
        return 0;
    }
    return 1;
}
int isFull(Stack *s){
    if (s->top == MAXSIZE-1) {
        return 1;
    }
    return 0;
}
void push(Stack *s,int x){
    if (isFull(s)) {
        printf("栈已满\n");
    }
    else{
        s->top++;
        s->data[s->top] = x;
    }
}
void pop(Stack *s,int *x){
    if (isEmpty(s)) {
        printf("栈为空\n");
    }
    else{
        *x = s->data[s->top];
        s->top--;
    }
}
int getTopData(Stack *s){
    if (isEmpty(s)) {
        printf("当前栈为空\n");
        exit(1);
    }
    else{
        return  s->data[s->top];
    }
}

int main(int argc, const char * argv[]) {
    int x;
    Stack *s = initStack();
    push(s, 3);
    push(s, 4);
    push(s, 5);
    push(s, 4);
    push(s, 4);
    printf("\n要删除的 %d\n",x);
    for (int i =0; i<=s->top; i++) {
        printf(" %d ",s->data[i]);
    }
    printf("当前栈顶元素 :%d\n",getTopData(s));

   return 0;
}
共享栈
typedef struct shareSatck{
    int data[MAXSIZE];
    int top1;
    int top2;
}Stack;
Stack *initStack(){
    Stack *s;
    if (!s) {
        printf("分配空间失败\n");
        exit(0);
    }
    s->top1 = -1;
    s->top2 = MAXSIZE;
    return s;
}
int isFull(Stack *s){
    if ((s->top1)+1 == s->top2) {
        return 1;
    }
    return 0;
}
int isEmpty(Stack *s){
    if (s->top1 == -1||s->top2 == MAXSIZE) {
        return 1;
    }
    return 0;
}
void Push(Stack *s,int x,int stackType){
    if (isFull(s)) {
        printf("栈已满\n");
        exit(0);
    }
    if (stackType == 1) {
        s->top1++;
        s->data[s->top1] = x;
    }
    else if(stackType == 2){
        s->top2--;
        s->data[s->top2] = x;
    }
}
void pop(Stack *s,int *x,int stackType){
    if (isEmpty(s)) {
        printf("栈为空\n");
        exit(0);
    }
    if (stackType == 1) {
        *x = s->data[s->top1];
        s->top1--;
    }
    else if(stackType == 2){
        *x = s->data[s->top2];
        s->top2++;
    }
}
*/
int main(int argc, const char * argv[]) {
     //共享栈
    int x;
    Stack *s = initStack();
    Push(s, 1, 1);
    Push(s, 2, 2);
    Push(s, 3, 1);
    Push(s, 4, 2);
    printStack(s);
    printf("\n");
    pop(s, &x, 2);
    printf("%d",x);
    return 0;
}
链栈
typedef struct node{
    int data;
    struct node *next;
}StackNode ,*ListStack;
void initStack(ListStack s){
    s->next = NULL;
}
int isEmpty(ListStack s){
    if (s->next == NULL) {
        return 1;
    }
    return 0;
}
void push(ListStack s,int x){

    StackNode *new = (ListStack)malloc(sizeof(StackNode));
    new->data= x;
    new ->next = s->next;
    s->next = new;
}
void pop(ListStack s,int  *x){
    if (isEmpty(s)) {
        printf("栈为空\n");
        exit(0);
    }
    *x = s->next->data;
    s->next =s->next->next;
}
int main(int argc, const char * argv[]) {
    //链栈没有头结点,s->top ===NULL
    ListStack s;
    s = (ListStack)malloc(sizeof(StackNode));
    initStack(s);
    push(s, 1);
    push(s, 2);
    push(s, 3);
    push(s, 4);
    push(s, 5);
    push(s, 6);
    ListStack p=s->next;
    while (p) {
        printf(" %d ",p->data);
        p=p->next;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li15809284891/article/details/72809575