From 07ce7a3949bb11e3a6d6f806262e3dc7d5977add Mon Sep 17 00:00:00 2001 From: Moustafa Attia Date: Thu, 7 Dec 2017 11:42:58 +0200 Subject: [PATCH 1/4] Add files via upload --- Stack.cpp | 80 ++++++++++++++++++++++++++++ Stack.h | 25 +++++++++ Test.cpp | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 261 insertions(+) create mode 100644 Stack.cpp create mode 100644 Stack.h create mode 100644 Test.cpp diff --git a/Stack.cpp b/Stack.cpp new file mode 100644 index 0000000..b3c739c --- /dev/null +++ b/Stack.cpp @@ -0,0 +1,80 @@ +#include"Stack.h" +#include + +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; itop; i++) + sp->entry[i] = 0; +} + +StackEntry FirstElement(Stack *ps) +{ + return ps->entry[0]; +} + +void CopyStack(Stack *src,Stack *dest) +{ + for(int i=0; itop; 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; ientry[i-1] = ps->entry[i]; + } + ps->top--; + return temp; +} \ No newline at end of file diff --git a/Stack.h b/Stack.h new file mode 100644 index 0000000..d27b6c2 --- /dev/null +++ b/Stack.h @@ -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); \ No newline at end of file diff --git a/Test.cpp b/Test.cpp new file mode 100644 index 0000000..f289000 --- /dev/null +++ b/Test.cpp @@ -0,0 +1,156 @@ +#include +#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 onlyyyyyyyy \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; +} From a0f31fd4e432f35948761b76da55ee87167fa733 Mon Sep 17 00:00:00 2001 From: Moustafa Attia Date: Thu, 7 Dec 2017 11:44:22 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index ae24b26..730cfa8 100644 --- a/README.md +++ b/README.md @@ -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 From 16b9a574545f77b2b4447eba9a168b4171f08f9e Mon Sep 17 00:00:00 2001 From: Moustafa Attia Date: Thu, 7 Dec 2017 11:44:32 +0200 Subject: [PATCH 3/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 730cfa8..b95eb25 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ ## DataStructures -# Stack using array based +### Stack using array based From 4290950728e550b9d1c0e83423593b99797262e3 Mon Sep 17 00:00:00 2001 From: Moustafa Attia Date: Thu, 7 Dec 2017 11:45:26 +0200 Subject: [PATCH 4/4] Update and rename Test.cpp to Main.cpp --- Test.cpp => Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Test.cpp => Main.cpp (92%) diff --git a/Test.cpp b/Main.cpp similarity index 92% rename from Test.cpp rename to Main.cpp index f289000..586f6a5 100644 --- a/Test.cpp +++ b/Main.cpp @@ -106,7 +106,7 @@ StackEntry RemoveFirstElement2(Stack *ps) int main() { printf("********************************************\n" - "This Stack is just for CHAR onlyyyyyyyy \n" + "This Stack is just for CHAR only \n" "********************************************\n"); Stack s; StackEntry e;