Skip to content

Instantly share code, notes, and snippets.

@kimkidong
Created October 11, 2012 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimkidong/3872291 to your computer and use it in GitHub Desktop.
Save kimkidong/3872291 to your computer and use it in GitHub Desktop.
Realization Stack with Using LinkedList
#include <stdio.h>
#include <stdlib.h>
typedef struct _Stack
{
int data;
struct _Stack* pNext;
}Stack;
typedef Stack* Stack_Top;
bool push(Stack data);
Stack pop();
bool isFull();
bool isEmpty();
void PrintStack();
Stack_Top top = NULL;
bool bFull = false;
void main()
{
Stack s;
s.data =1,push(s);
s.data =2,push(s);
s.data =3,push(s);
s.data =4,push(s);
PrintStack();
pop(),pop();
PrintStack();
}
bool push(Stack data)
{
Stack* node = (Stack*)malloc(sizeof(Stack));
if ( node == NULL)
{
printf("Stack is full\n");
bFull = true;
return !bFull;
}
node->data = data.data;
node->pNext = NULL;
if ( top == NULL)
{
top = node;
}
else
{
node->pNext = top;
top = node;
}
return true;
}
Stack pop()
{
Stack temp = *top;
Stack* temp_next = top->pNext;
free(top);
top = temp_next;
bFull = false;
return temp;
};
void PrintStack()
{
Stack_Top temp = (Stack_Top)malloc(sizeof(Stack));
temp = top;
while ( temp != NULL)
{
printf("%2d",temp->data);
temp = temp->pNext;
}
printf("\n");
}
bool isFull()
{
return bFull;
}
bool isEmpty()
{
return top == NULL ? true:false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment