C language and basic operation of the link stack

Link stack , i.e. using linked list implemented stack storage structure. Link stack implemented with the idea of a similar order of stacks, sequentially stack is the number of sequence table (array) of one end of a bottom of the stack, and the other end of the stack; link stack is also the case, we would normally head of the list as a stack, as the tail bottom of the stack, as shown below:
Here Insert Picture Description

The end of the stack as a linked list of the head, to avoid time-consuming operation to make a large number of traversing the list operation for data in the "stack" and "pop."

Head of the list as the top of the stack, which means:

  • "In implementing data stack " operation, data needs to be inserted from the head of the list;
  • In implementing data " pop " operation, the first element nodes need to remove the head of the list;

Thus, link stack is actually a list head only use interpolation data is inserted or deleted.

Drawing a link stack elements

For example, the stack elements sequentially 1,2,3,4, equivalent to the head of each element using interpolation sequentially added to the list, for each data element of the addition process as shown below:

C language code:

//链表中的节点结构
typedef struct lineStack{
    int data;
    struct lineStack * next;
}lineStack;
//stack为当前的链栈,a表示入栈元素
lineStack* push(lineStack * stack,int a){
    //创建存储新元素的节点
    lineStack * line=(lineStack*)malloc(sizeof(lineStack));
    line->data=a;
    //新节点与头节点建立逻辑关系
    line->next=stack;
    //更新头指针的指向
    stack=line;
    return stack;
}

Link stack pop element

For example, the link stack shown in FIG, 3 to the element stack, according to the principle "last out", the first element of the stack 4, i.e. removed from the list, and then element 3 to the stack, the entire operation as shown below:
Here Insert Picture Description
Thus, to achieve a top element C language link stack implementation code is:

//栈顶元素出链栈的实现函数
lineStack * pop(lineStack * stack){
    if (stack) {
        //声明一个新指针指向栈顶节点
        lineStack * p=stack;
        //更新头指针
        stack=stack->next;
        printf("出栈元素:%d ",p->data);
        if (stack) {
            printf("新栈顶元素:%d\n",stack->data);
        }else{
            printf("栈已空\n");
        }
        free(p);
    }else{
        printf("栈内没有元素");
        return stack;
    }
    return stack;
}

Code by using the if statement, to avoid the user performs a "stack is empty but also the stack is" erroneous operation.

to sum up

#include <stdio.h>
#include <stdlib.h>
typedef struct lineStack{
    int data;
    struct lineStack * next;
}lineStack;
lineStack* push(lineStack * stack,int a){
    lineStack * line=(lineStack*)malloc(sizeof(lineStack));
    line->data=a;
    line->next=stack;
    stack=line;
    return stack;
}
lineStack * pop(lineStack * stack){
    if (stack) {
        lineStack * p=stack;
        stack=stack->next;
        printf("弹栈元素:%d ",p->data);
        if (stack) {
            printf("栈顶元素:%d\n",stack->data);
        }else{
            printf("栈已空\n");
        }
        free(p);
    }else{
        printf("栈内没有元素");
        return stack;
    }
    return stack;
}
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;
}

As a result of the program:

Popping elements: the top element 4: 3
popping elements: the top element 3: 2
popping elements: the top element 2: 1
popping elements: 1 Stack is empty
no elements within the stack

Those are the times for everyone to share the link stack using the C language, the first post in February, hoping to gain the support of duck! Also, if you want information about additional C language data structures, algorithms, and welcome to my personal blog , we discuss, if anything badly written, there are better algorithms, welcome message in the comment section below! Our common progress together!

Published 19 original articles · won praise 8 · views 604

Guess you like

Origin blog.csdn.net/qq_43336390/article/details/104137347
Recommended