Data structure-stacks (c description)

Source: Internet
Author: User
Tags empty include

1. Chain-type stack

Stackli.h

typedef int ElementType;
#ifndef stackli_h_included
#define Stackli_h_included
struct Node;
typedef struct NODE *ptrtonode;
typedef ptrtonode Stack;
int IsEmpty (Stack S);
Stack Createstack ();
void Disposestack (Stack S);
void Makeempty (Stack S);
void Push (ElementType X, Stack S);
ElementType Top (Stack S);
void Pop (Stack S);
#endif//stackli_h_included

Fatal.h
#ifndef fatal_h_included
#define Fatal_h_included
#include <stdio.h>
#include <stdlib.h>
#define ERROR (str) fatalerror (str)
#define FATALERROR (str) fprintf (stderr, "%s\n", Str), exit (1)
#endif//fatal_h_included

Stackli.c
#include "stackli.h"
#include "fatal.h"
struct Node
{
ElementType Element;
Ptrtonode Next;
};
int IsEmpty (Stack S)
{
return s->next = = NULL;
}
Stack Createstack ()
{
Stack S;
S = malloc (sizeof (struct Node));
if (S = = NULL)
FatalError ("Out of the space!!!");
S->next = NULL;
return S;
}
void Makeempty (Stack S)
{
if (S = = NULL)
Error ("must use Createstack");
Else
while (! IsEmpty (S))
Pop (S);
}
void Disposestack (Stack S)
{
Makeempty (S);
Free (S);
}
void Push (ElementType X, Stack S)
{
Ptrtonode Tmpcell;
Tmpcell = malloc (sizeof (struct Node));
if (Tmpcell = NULL)
FatalError ("Out of the space!!!");
Else
{
Tmpcell->element = X;
Tmpcell->next = s->next;
S->next = Tmpcell;
}
}
ElementType Top (Stack S)
{
if (! IsEmpty (S))
Return s->next->element;
Error ("Empty stack");
return 0; /* return value used to avoid warning * *
}
void Pop (Stack S)
{
Ptrtonode FirstCell;
if (IsEmpty (S))
Error ("Empty stack");
Else
{
FirstCell = s->next;
S->next = s->next->next;
Free (FirstCell);
}
}

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.