1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/* * Stack.c * * Program to demonstrate the stack concept * * by Ahmed M. Sha'alan, disturbed.619@gmail.com, www.thedb.com/m */ #include <stdio.h> #include <stdlib.h> //for the system() and exit() functions #define STACKSIZE 100 //the stack size struct STACK { //stack structure short int top; int items[STACKSIZE]; }; typedef struct STACK stk; //a short-hand for the stack int empty(stk *ptr){ //checks if the stack is empty return(ptr->top<0?1:0); //shorter and makes more sense than the IF statment } int full(stk *ptr) { //checks if the stack is full return(ptr->top>=STACKSIZE?1:0); //read above } void push(stk *ptr, int *x) { //pushs a value to the top of the stack ptr->items[++ptr->top]=*x; } int pop(stk *ptr) { //displays the top value of the stack return (ptr->items[ptr->top--]); } void print_pop(stk *ptr){ //the pop() function but with empty() check and messages if(empty(ptr)) puts(">>The stack is empty!"); else printf(">>The last value in the stack is: %d.\n", pop(ptr)); } void print_push(stk *ptr){ //the push() function with the checking and messages int x; if(full(ptr)) puts(">>The stack is full!"); else{ printf(">>Enter the value you want to push: "); fflush(stdin); /*to clear the standard input and prevent a weird behavior, although i been told it's not THE way 2 do it*/ scanf("%d", &x); push(ptr, &x); puts(">>done!"); } } void print_all(stk *ptr) { //prints all the elements in the stack unsigned short int i=0; system("cls"); puts("********************"); if (empty(ptr)) puts(">>The stack is empty!"); else{ for (;i<=ptr->top;i++) printf("|%d- %d\n",i+1, ptr->items[i]); } puts("********************"); } void interface(stk *ptr) { //the "interface" lol, i KNOW it's a console aplication :D unsigned short int choice; //we only need numbers from 1 to 4 system("cls"); puts("\n====================================="); puts("Stack Implementation in C..."); puts("====================================="); BEGINING: puts("\n======================================================="); puts("1- Press 1 for putting a value in the stack."); puts("2- Press 2 for displaying the last value in the stack."); puts("3- Press 3 for displaying ALL the values in the stack."); puts("4- Press 4 for exiting the program."); puts("======================================================="); printf("What do you want to do? "); fflush(stdin); //read above scanf("%d", &choice); puts(""); //new line switch (choice) { case 1: print_push(ptr); break; case 2: print_pop(ptr); break; case 3: print_all(ptr); break; case 4: exit(1); default: puts(">>Please make sure you've entered a valid option.."); break; } goto BEGINING; //repeat again and again til the user exits } int main() { //finaly the main :D stk stack; stack.top=-1; //sets the top to -1 for the stack to act right interface(&stack); return 0; }
Refactorings
No refactoring yet !
jaredgrubb
November 3, 2007, November 03, 2007 04:48, permalink
A few comments: your push/pop functions don't check for overflow or underflow. So, for example, you could pop 1000 items off your stk. Also, you don't need to pass x as a pointer in your push function. Otherwise, it looks good. (You should next do the same thing in C++!)
X
November 8, 2007, November 08, 2007 22:50, permalink
another comment´s:
i prefer to do a: return(ptr->top<0);
instead of return(ptr->top<0?1:0);
and use a char for the menu in the case that the user press a char instead of a number..
(sorry for my bad english :P)
St0rM
November 11, 2007, November 11, 2007 08:41, permalink
good code BUT ,
1- you are using fflush(stdin) that's bad
standard says :
The function fflush() forces a write of all user-space buffered data for the given output
output : like stderr , stdout
not like stdin , behavior is not defined in this case
2- well you are using labels rather than the BEAUTIFUL loops :)
3- complicated statements like
ptr->items[++ptr->items]
you are accessing structure twice , and it's complicated for the reader
4- your printall functions isn't right it starts from 0 rather than top
then it prints the first element not the last
stack says : LAST IN FIRST OUT
any way :D really good job man , go on
take a look at your modified code and tell me what you think
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#include <stdio.h> #include <stdlib.h> //for the system() and exit() functions #define STACKSIZE 100 //the stack size struct STACK { //stack structure short int top; int items[STACKSIZE]; }; typedef struct STACK stk; //a short-hand for the stack int empty(stk *ptr){ //checks if the stack is empty return(ptr->top<0?1:0); //shorter and makes more sense than the IF statment } int full(stk *ptr) { //checks if the stack is full return(ptr->top>=STACKSIZE?1:0); //read above } void push(stk *ptr, int *x , short int *top) { //pushs a value to the top of the stack ptr->items[++(*top)]=*x; /*Doing ptr->items[++ptr->times]; takes * time and make it so complicated for the reader * Just refernce the top element in the stack * rather than accesing structure twice */ printf(">>Stack Layer %d\n",(*top)+1); } int pop(stk *ptr , short int *top ) { //displays the top value of the stack printf(">>Stack Layer %d\n",((*top))+1); /* The User Will Never Like The Idea of * StacK Layer 0 :D */ return (ptr->items[(*top)--]); /* Same as before */ } void print_pop(stk *ptr){ //the pop() function but with empty() check and messages if(empty(ptr)) puts(">>The stack is empty!"); else printf(">>The last value in the stack is: %d.\n", pop(ptr , &ptr->top )); } void print_push(stk *ptr){ //the push() function with the checking and messages int x; if(full(ptr)) puts(">>The stack is full!"); else{ printf(">>Enter the value you want to push: "); fflush(stdin); /*to clear the standard input and prevent a weird behavior, although i been told it's not THE way 2 do it*/ scanf("%d", &x); push(ptr, &x , &ptr->top); puts(">>done!"); } } void print_all(stk *ptr) { //prints all the elements in the stack short int i; /* if you say i=0 it will start from the beggining of the stack not the top of the stack remember First in last out */ system("cls");/* What if someone have a linux box like me :S */ puts("********************"); if (empty(ptr)) puts(">>The stack is empty!"); else{ /* You soppuse to start from the top */ for ( i = ptr->top ; i >=0 ; i--) printf("|%d- %d\n",i+1, ptr->items[i]); } puts("********************"); } void interface(stk *ptr) { //the "interface" lol, i KNOW it's a console aplication :D unsigned short int choice; //we only need numbers from 1 to 4 system("cls"); puts("\n====================================="); puts("Stack Implementation in C..."); puts("====================================="); /*BEGINING:*/ /* Why there is loops ?? * Using GOTO stetement is danger in debuging * you will have no idea where in the program is flowing to this * lable */ while(1) { puts("\n======================================================="); puts("1- Press 1 for putting a value in the stack."); puts("2- Press 2 for displaying the last value in the stack."); puts("3- Press 3 for displaying ALL the values in the stack."); puts("4- Press 4 for exiting the program."); puts("======================================================="); printf("What do you want to do? "); fflush(stdin); //read above /* Flushing Stdin Is Wrong * Read man fflush * use a functin like getchar(); */ scanf("%d", &choice); puts(""); //new line switch (choice) { case 1: print_push(ptr); break; case 2: print_pop(ptr); break; case 3: print_all(ptr); break; case 4: exit(1); /* You Are Missing A Break Here man :D*/ break; default: puts(">>Please make sure you've entered a valid option..(1-4))"); break; } } /*goto BEGINING; //repeat again and again til the user exits*/ } int main() { //finaly the main :D stk stack; stack.top=-1; //sets the top to -1 for the stack to act right interface(&stack); return 0; }
mangiucugna
December 4, 2007, December 04, 2007 13:27, permalink
Some comments on the pop and push functions, if you want to implement a stack structure the pop function needs only the stack, and the push function only stack and value. The top value is derived from ptr, you do not need to pass it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//////////// void push(stk *ptr, int *x) { //pushs a value to the top of the stack ptr->items[++(ptr->top)]=*x; printf(">>Stack Layer %d\n",(ptr->top)+1); } int pop(stk *ptr) { //displays the top value of the stack printf(">>Stack Layer %d\n",((ptr->top))+1); return (ptr->items[(ptr->top)--]); } ///////////////////
My first attempt to data structures using c..