Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#include<stdio.h>
#include"Stack.h"


/* This is user level */

void PrintStack(StackEntry pe)
{
printf("%c <--",pe);
}

StackEntry FirstElement2(Stack *ps)
{
Stack temp;
CreateStack(&temp);
StackEntry item , result;
while(!StackEmpty(ps)){
pop(&item , ps);
push(item , &temp);
}
result = item;
while(!StackEmpty(&temp)){
pop(&item , &temp);
push(item , ps);
}

return result;
}

StackEntry LastElement2(Stack *ps)
{
StackEntry result;
pop(&result , ps);
push(result , ps);

return result;
}

void DestroyStack(Stack *ps)
{
StackEntry item;
while(!StackEmpty(ps)){
pop(&item , ps);
}
}

void CopyStack2(Stack *src,Stack *dest){
Stack temp;
StackEntry item;

CreateStack(&temp);
while(!StackEmpty(src)){
pop(&item , src);
push(item , &temp);
}
while(!StackEmpty(&temp)){
pop(&item , &temp);
push(item , dest);
push(item , src);
}

}

int StackSize2(Stack *s)
{
int counter = 0;
Stack temp;
StackEntry item;
CreateStack(&temp);

while(!StackEmpty(s))
{
pop(&item , s);
push(item , &temp);
counter++;
}

while(!StackEmpty(&temp)){
pop(&item , &temp);
push(item , s);
}

return counter;
}

StackEntry RemoveFirstElement2(Stack *ps)
{
Stack temp;
CreateStack(&temp);
StackEntry item , result;

while(!StackEmpty(ps)){
pop(&item , ps);
push(item , &temp);
}
result = item;
pop(&item , &temp);
while(!StackEmpty(&temp)){
pop(&item , &temp);
push(item , ps);
}

return result;
}

int main()
{
printf("********************************************\n"
"This Stack is just for CHAR only \n"
"********************************************\n");
Stack s;
StackEntry e;
char arr[10] = {'A','B','C','D','E','F','G','H','I','J'};
CreateStack(&s);

for(int i=0; i<10; i++)
{
e = arr[i];
push(e,&s);
TraverseStack(&s , &PrintStack);
putchar('\n');
}

printf("_______________________________________________________\n");
printf("The orignal Stack is like that : \n");
TraverseStack(&s , &PrintStack);
putchar('\n');
printf("_______________________________________________________\n");


for(int i=0; i<10; i++)
{
pop(&e , &s);
printf("After we pop the elemnt number %d\n",(10-i));
TraverseStack(&s , &PrintStack);
putchar('\n');
printf("The element e = '%c'\n",e);
printf("_______________________________________________________\n");
}

StackEntry item;
Stack stack;
CreateStack(&stack);

for(int i=0;i<5;i++)
{
scanf("%c",&item);
push(item , &stack);
}
printf("__________________________________________________\n");
TraverseStack(&stack , &PrintStack);

putchar('\n');

return 0;
}
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
## DataStructures
This repository for my implemented data structure using C
I write this code when I was in college second grade at 2010 in data structure course

+ [Stack data structure pointer based using C](https://github.com/MoustafaAttia/DataStructures/tree/StackPointerBased)
### Stack using array based
80 changes: 80 additions & 0 deletions Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include"Stack.h"
#include<stdio.h>

void CreateStack(Stack *ps){
ps->top = 0;
}

void push(StackEntry e, Stack *ps){
ps->entry[ps->top] = e;
ps->top++;
}

int StackFull(Stack *ps)
{
if(ps->top == MAXSTACK)
return 1;
else
return 0;
}

void pop(StackEntry *pe, Stack *ps){
ps->top--;
*pe = ps->entry[ps->top];
}

int StackEmpty(Stack *ps){
if(ps->top == 0)
return 1;
else
return 0;
}

void StackTop(StackEntry *pe, Stack *ps){
*pe = ps->entry[ps->top];
}

int StackSize(Stack *ps){
return ps->top;
}

void ClearStack(Stack *ps){
ps->top = 0;
}

void TraverseStack(Stack *ps,void (*pf)(StackEntry)){
for(int i=ps->top; i>0; i--)
{
(*pf)(ps->entry[i-1]);
}
}

void MakeStack0(Stack *sp)
{
for(int i = 0; i<sp->top; i++)
sp->entry[i] = 0;
}

StackEntry FirstElement(Stack *ps)
{
return ps->entry[0];
}

void CopyStack(Stack *src,Stack *dest)
{
for(int i=0; i<src->top; i++){
dest->entry[i] = src->entry[i];
}
dest->top = src->top;
}

StackEntry RemoveFirstElement(Stack *ps)
{
StackEntry temp = ps->entry[0];
int i;
for(i = 1; i<MAXSTACK; i++){
ps->entry[i-1] = ps->entry[i];
}
ps->top--;
return temp;
}
25 changes: 25 additions & 0 deletions Stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define MAXSTACK 10

typedef char StackEntry;

typedef struct stack{
int top;
StackEntry entry[MAXSTACK];
}Stack;



void push(StackEntry, Stack *); // done
void pop(StackEntry *, Stack *); // done
int StackEmpty(Stack *); // done
int StackFull(Stack *); // done
void CreateStack(Stack *); // done
void StackTop(StackEntry *,Stack *); // done
int StackSize(Stack *); // done
void ClearStack(Stack *); // done
void TraverseStack(Stack *,void (*)(StackEntry)); // done
void PrintStack(Stack *);
void MakeStack0(Stack *);
StackEntry FirstElement(Stack *ps);
void CopyStack(Stack *src,Stack *dest);
StackEntry RemoveFirstElement(Stack *ps);