数据结构教程 | 栈的基本操作和应用

本文详细介绍了栈这一线性存储结构的基本概念、两种实现方式(顺序栈与链栈),并提供了具体的C语言代码示例来演示如何进行元素的压栈与出栈操作。此外,还探讨了栈在十进制数转换、括号匹配等实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Data Structure: Basic operations and Applications of Stack


1 Basic knowledge

1.1 Introduction

Similar to sequential table or link list, stack is another kind of linear storage structure. Stack is special due to its unique features:

  • The stack can only access data from one end of the list, and the other end is closed.
  • The elements follows the rules ’ First in last out '.

1.2 Implement the stack

There are two ways to implement the stack, usually we call them Sequential Stack and Link Stack. It is easy to inform that the sequential stack uses array to store elements while the link stack uses link list. The only difference between two ways is the place where we put the elements in the physical space.

2 Operations of Sequential Stack

2.1 Push the element

// Push 'elem' into the stack
int push(int* a,int top,int elem){
    a[++top]=elem;
    return top;
}

2.2 Pop the element

// Pop 'elem' out of the stack
int pop(int * a,int top){
    if (top==-1) {
        printf("Empty stack");
        return -1;
    }
    printf("Pop element:%d\n",a[top]);
    top--;
    return top;
}

2.3 Check the code

int main() {
    int a[100];
    int top=-1;
    top=push(a, top, 1);
    top=push(a, top, 2);
    top=push(a, top, 3);
    top=push(a, top, 4);
    top=pop(a, top);
    top=pop(a, top);
    top=pop(a, top);
    top=pop(a, top);
    top=pop(a, top);
    return 0;
}

Output, we can see it works well:
在这里插入图片描述

3 Operations of Link Stack

3.1 Push the element

We can also use link list to implement the stack, the definition and construction is same to that of link list:

typedef struct lineStack{
    int data;
    struct lineStack * next;
}lineStack;

For each element, we insert it behind the head node:
在这里插入图片描述

lineStack* push(lineStack * stack,int a){
    lineStack * line=(lineStack*)malloc(sizeof(lineStack));	// Create new node
    line->data=a;
    line->next=stack;	// Connect the new node and the head node
    stack=line;			// Alter the head node's directivity 
    return stack;
}

3.2 Pop the element

The pop operation is just the same:
在这里插入图片描述

lineStack * pop(lineStack * stack){
    if (stack) {
        lineStack * p=stack;
        stack=stack->next;
        printf("Pop element:%d ",p->data);
        if (stack) {
            printf("The element on the top:%d\n",stack->data);
        }else{
            printf("The stack is empty\n");
        }
        free(p);
    }else{
        printf("No element in the stack");
        return stack;
    }
    return stack;
}

3.3 Check the code

int main() {
    lineStack * stack=NULL;
    stack=push(stack, 1);
    stack=push(stack, 2);
    stack=push(stack, 3);
    stack=push(stack, 4);
    stack=pop(stack);
    stack=pop(stack);
    stack=pop(stack);
    stack=pop(stack);
    stack=pop(stack);
    return 0;
}

Output:
在这里插入图片描述

4 Applications

4.1 Number system conversion

Request:The user provides the data to be converted and the base of the data, the base converter should provides the user with the final correct conversion result.
The converter can convert the number among 2(Binary system) to 36:
在这里插入图片描述
Thinking:

  • When the user input the number, we store it in a sequential structure(string array).
  • Convert the number to decimal system than convert to the number system they need. The final result will be stored in a stack.

Code implementation:

#include <stdio.h>
#include <string.h>
#include <math.h>
int top=-1;		// We use -1 to represent the top of the stack
void push(char *a, char elem){
	a[++top]=elem;
}
void pop(char *a){
	if(top==-1){
		return;
	}
	if(a[top]>=10){
		printf("%c",a[top]+55);
	}else{
		printf("%d",a[top]); 
	}
	top--; 
}
// Transform all types of number to decimal system
int scaleFun(char * data,int system){
	int k=(int)strlen(data)-1;
	int system_10_data=0;
	int i;
	for(i=k;i>=0;i--){
		int temp;
		if (data[i]>=48 && data[i]<=57) {
            temp=data[i]-48;
        }else{
            temp=data[i]-55;
        }
        system_10_data+=temp*pow(system, k-i);
	}
	return system_10_data;
} 
int main() {
    char data[100];
    printf("The number system input(2-36):");
    int system;
    scanf("%d",&system);
    getchar();
    printf("The number to be converted:");
    scanf("%s",data);
    getchar();
    int system_10_data=scaleFun(data, system);
    printf("The number system output:");
    int newSystem;
    scanf("%d",&newSystem);
    getchar();
    while (system_10_data/newSystem) {
        push(data,system_10_data%newSystem );
        system_10_data/=newSystem;
    }
    push(data,system_10_data%newSystem);
    printf("Result:\n");
    while (top!=-1) {
        pop(data);
    }
}

Example output:
在这里插入图片描述

4.2 Parentheses matching

Request: Give a series of the parentheses to determine whether they match, for example, {([])} match but {(}[ do not.

Thinking:

  • If we meet ( or { , push it
  • If we meet ) or } , match with the top element, if they match, pop them, else we would say the parentheses do not match.

Code implementation:

/* Parentheses matching */
#include <stdio.h>
#include <string.h>
int top=-1;
void push(char * a,int elem){
    a[++top]=elem;
}
void pop(char* a){
    if (top==-1) {
        return ;
    }
    top--;
}
char visit(char * a){
    // If the stack is empty, return NULL
    if (top!=-1) {
        return a[top];
    }else{
        return ' ';
    }
}
int main() {
    char a[30];
    char bracket[100];
    printf("Input the parentheses:");
    scanf("%s",bracket);
    getchar();
    int length=(int)strlen(bracket);
    for (int i=0; i<length; i++) {
        if (bracket[i]=='('||bracket[i]=='{') {	
            push(a, bracket[i]);
        }else{
            if (bracket[i]==')') {
                if (visit(a)=='(') {
                    pop(a);
                }else{
                    printf("Not match");
                    return 0;
                }
            }else{
                if (visit(a)=='{') {
                    pop(a);
                }else{
                    printf("Not match");
                    return 0;
                }
            }
        }
    }
    // If they all match, the stack will be empty in the end
    if (top!=-1) {
         printf("Not match");
    }else{
       printf("Match");
    }
}

Output:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值