Data structure Learning notes-stacks of chained storage (C language Implementation)

Source: Internet
Author: User

Stack is a special linear table, LIFO , since it is a linear table, will be divided into sequential storage and chain storage, the following is the stack of the chain storage part, also known as chain stack. Single linked list is a head pointer node, usually the stack top of the stack is equivalent to the head pointer, because the initial chain stack is empty, that is, Top=null, because the top of the stack, the first node of a single linked table has no meaning, it does not need. For a chain stack, there is almost no stack full, unless the computer is full of memory. Chain stack operation and the operation of a single linked list is very similar, just insert, delete, get the difference is large, the implementation code is as follows:

#include <stdio.h> #include <stdlib.h> typedef int LSELETYPE;//Defines the data type of a chain stack store typedef struct STACKNODE{//
    The Structure Body (node) Lseletype data that defines chain stack storage;
struct Stacknode *next;
}stacknode;
    The typedef stacknode *stacklinkp;//defines the stack top of the pointer type (equivalent to the head pointer of the linked list) typedef struct STACKLINK{//defines the attributes STACKLINKP top of the chain stack;
int count;
}stacklink;
    Stacklink init () {Stacklink sl;
    Sl.top=null;
    sl.count=0;
return SL;
    int Stackempty (Stacklink s) {int result;
    if (s.count==0) {result=0;
    }else{result=1;
return result;
    } void push (Stacklink *s,lseletype e) {//defines a pointer to a node (equivalent to a linked-head pointer) STACKLINKP p;
    Create nodes through pointers and assign values to p= (STACKLINKP) malloc (sizeof (Stacknode));
    p->data=e;
    p->next=s->top;//the top of the stack to him and let the top of the stack point to him s->top=p; s->count++;//chain stack length increased} void Pop (Stacklink *s,lseletype *e) {int r=stackempty (*s);//To determine whether the stack is empty if (r==1) {St
        ACKLINKP p=s->top;//Get the node to delete (*e) =p->data;
  s->top=p->next;//let the top of the stack point to the next node      Free (p);
    s->count--;
    }else{return;
    } void Getele (Stacklink s,lseletype *e) {STACKLINKP p=s.top;
*e=p->data;
    int main () {Stacklink s=init ();
    Lseletype G;
    Lseletype e=666,f=888;
    printf ("Whether the chain stack is empty: \n%d\n", Stackempty (s));
    Push (&AMP;S,E);
    Push (&AMP;S,F);
    printf ("Whether the chain stack is empty: \n%d\n", Stackempty (s));
    Getele (S,&AMP;G);
    printf ("Current stack top data is: \n%d\n", g);
    Pop (&AMP;S,&AMP;G);
    Getele (S,&AMP;G);
    printf ("Delete a data, the current stack top data is: \n%d\n", g);
    Pop (&AMP;S,&AMP;G);
    printf ("Whether the chain stack is empty: \n%d\n", Stackempty (s));
return 0;
 }


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.