Linux learning Summary (Data Structure-stack and queue)

Source: Internet
Author: User

Shortcuts to learning: write more code

STACK: A stack is a linear table that restricts the insert and delete operations on one end. The row number is also a linear table. That is to say, there are two storage methods: sequential stack and chained stack. One end that allows operations is called the "Stack top" and the other end is called the "Stack bottom ". Features: first-in-first-out.

The so-called stack and queue is actually a technology. Sometimes a special storage method is required, and then the element is restored when necessary, it will be used to stack or queue, for example, in some bare metal code operated by arm, the value in the Status Register needs to be kept, and the stack or queue can be used as needed for storage, which is convenient and secure to use, therefore, when it comes to operations such as data storage, consider these two technologies.

Basic operations:

Create an empty Stack: createstack (); judge whether an empty stack isempty (s); judge whether the full stack isfull (s); push (S, X ); pop (s) of the outbound stack );

Again, get familiar with sequential and chained storage.

Sequence Stack:

#include <stdio.h>#include <stdlib.h>#include <strings.h>#include <errno.h>#define N 10typedef int datatype;typedef struct {    datatype data[N];    int top;}sqstack;sqstack * create(){    sqstack * s;    bzero(&s,sizeof(s));    if((s = (sqstack *)malloc(sizeof(sqstack)))==NULL)    {        perror("malloc");        exit(-1);    }    s->top = -1;    return s;}int isempty(sqstack *s){    if(s->top == -1)    return 0;    return -1;}int isfull(sqstack *s){    if(s->top == N-1)        return 0;    return -1;}int push(sqstack *s,datatype data){    if(isfull(s)==0)    {        printf("stack is full\n");        return -1;    }    s->top++;    s->data[s->top] = data;    return 0;}datatype pop(sqstack *s){    datatype data;    if(isempty(s)==0)    {        printf("the sqstack is empty!");        return ;    }    data = s->data[s->top];    s->top--;    return data;}void show(sqstack *s){    int i;    i = s->top;    if(isempty(s)==0)    {        printf("empty!");        return ;    }    printf("data = %d\n",s->data[i]);    }int main(int argc,char * argv[]){    int i,ret;    sqstack * s;    s = create();    for(i = 1;i< 12;i++)    {        ret = push(s,i);        if(ret != 0 )            return -1;        show(s);    }     free(s);    return 0;}

 

Chain Stack:

#include <stdio.h>#include <stdlib.h>typedef int datatype;typedef struct node{    datatype data;    struct node * next;}linknode,*linkstack;linkstack create(){    linkstack h;    if((h = (linkstack)malloc(sizeof(linknode)))==NULL)    {        perror("malloc");        return NULL;    }    h->next = NULL;    return h;}int push(linkstack h,datatype data){    linkstack p;    if((p = (linkstack)malloc(sizeof(linknode)))==NULL)    {        perror("malloc");        return -1;    }    p->data = data;    p->next = h->next;    h->next = p;    return 0;}int isempty(linkstack h){    if(h->next==NULL)    {        printf("is empty\n");        return 1;    }    return 0;}datatype pop(linkstack h){    datatype data;    linkstack p = NULL;    if(isempty(h))    {        return 0;    }    p = h->next;    h->next = p->next;    data = p->data;    free(p);    return data;    }void show(linkstack h){    linkstack p = NULL;    if(isempty(h))    {        printf("is empty!\n");        return ;    }    p = h->next;    while(p != NULL)    {        printf("data = %d\n",p->data);        p = p->next;    }}int main(int argc,char * argv[]){    int i;    linkstack h;    h = create();    for(i = 1;i < 10;i++)    {        push(h,i);    }    show(h);    return 0;}

Simple operation. The next step is the queue, which is generally used as a cyclic queue. An interesting program is called a ball clock.

Next implementation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.