Using arrays and lists to implement stacks (C language Edition)

Source: Internet
Author: User

Arrays and lists are the two basic data structures and the basis of many complex data structures. This program uses arrays and linked lists to implement the stack.

/********************************************************

STACK. H

********************************************************/


#ifndef _stack_h
#define _stack_h


#ifndef BOOL
#define BOOL INT
#endif

typedef char Item;

struct _stack;
typedef struct _STACK Stack;
typedef stack* Pstack;


Pstack stack_init (int nmax);
void Stack_destroy (Pstack pSt);

void Stack_push (Pstack pst,item elem);

Item Stack_pop (Pstack pSt);

int stack_size (Pstack pSt);

BOOL stack_isempty (Pstack pSt);
BOOL stack_isfull (Pstack pSt);

#endif

/********************************************

Based on Array stack.c

********************************************/

#include <stdio.h>
#include <stdlib.h>
#include "STACK.h"


Static Error (char *msg)
{
printf ("error:%s/n", msg);
}

struct _stack{
item* Elem;
int top;
int Nmax;
};


Pstack stack_init (int nmax)
{
Pstack pst=malloc (sizeof (*PST));
Pst->elem=malloc (nmax*sizeof (*pst->elem));
pst->top=-1;
pst->nmax=nmax-1;
return pSt;
}

void Stack_destroy (Pstack pSt)
{
Free (Pst->elem);
pst->elem=null;
Free (pSt);
Pst=null;
}

void Stack_push (Pstack pst,item elem)
{
if (Stack_isfull (pSt))
{
Error ("Stack is full");
Exit (-1);
}
pst->elem[++pst->top]=elem;
}


Item Stack_pop (Pstack pSt)
{
if (Stack_isempty (pSt))
{
Error ("Stack is Empty");
Exit (-1);
}
Return pst->elem[pst->top--];
}

int stack_size (Pstack pstk)
{
Return pstk->top+1;
}

BOOL stack_isempty (Pstack pSt)
{
Return pst->top==-1;
}

BOOL stack_isfull (Pstack pSt)
{
return Pst->top==pst->nmax;
}


/***************************************************

Based on linked list

****************************************************/

typedef struct _STACKELEM
{
Item Elem;
struct _stackelem *next;
}stackelem;

typedef stackelem* Link;

typedef struct _STACK
{
Link head;
int Nmax;

}stack;


Pstack stack_init (int nmax)
{
Pstack pstk=malloc (sizeof (*PSTK));
pstk->head=null;
pstk->nmax=nmax;
return PSTK;
}

void Stack_destroy (Pstack pstk)
{
while (! Stack_isempty (PSTK))
{
Stack_pop (PSTK);
}
Free (PSTK);
}

void Stack_push (Pstack pstk,item elem)
{
Link ln;
if (Stack_isfull (PSTK))
{
Error ("Stack is full");
Exit (-1);
}
Ln=malloc (sizeof (*LN));
ln->elem=elem;
ln->next=pstk->head;
pstk->head=ln;
}

Item Stack_pop (Pstack pstk)
{
Link ln;
Item Elem;
if (Stack_isempty (PSTK))
{
Error ("Stack is Empty");
Exit (-1);
}
ln=pstk->head;
elem=ln->elem;
pstk->head=pstk->head->next;
Free (LN);
return elem;
}

int stack_size (Pstack pstk)
{
int cnt;
Link ln;
for (cnt=0,ln=pstk->head;ln!=null;ln=ln->next,cnt++);

return CNT;
}

BOOL stack_isempty (Pstack pstk)
{
Return pstk->head==null;
}

BOOL stack_isfull (Pstack pstk)
{
Return Stack_size (PSTK) ==pstk->nmax;
}

/**********************************************

Test program MAIN.C

*********************************************/


#include <stdio.h>
#include <string.h>
#include "STACK.h"


int main ()
{
Pstack Pstk=stack_init (20);

Char i= ' z ';
while (! Stack_isfull (PSTK))
{
Stack_push (pstk,i--);
}


printf ("Stack size=%d/n", Stack_size (PSTK));

while (! Stack_isempty (PSTK))
{
printf ("%c/n", Stack_pop (PSTK));
}

Stack_destroy (PSTK);

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.