Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Help me in dynamic stack program


    help me in Dynamic stack program..

    my problem is that i cant get proper out of pop, peep and update function..

    i dont know where i made mistakes.
    my english and pointer to structure concepts are weak :(
    so sorry for that :(

    Code:
    #indclude<stdio.h>
    #include<conio.h>
    
    struct stack
    {
    	int info;
    	struct stack *next;
    }*tos=NULL;
    
    void push();
    void pop();
    void peep();
    void update();
    void display ();
    
    void main()
    {
    	int ans;
    
    
    	do
    	{
    		clrscr();
    
    		printf("\n 1-push ");
    		printf("\n 2-pop ");
    		printf("\n 3-peep ");
    		printf("\n 4-update ");
    		printf("\n 5-display ");
    
    		printf("\n Enter chocie ");
    		scanf("%d",&ans);
    
    		switch (ans)
    		{
    			case 1:
    			push();
    			break;
    
    			case 2:
    			pop();
    			break;
    
    			case 3:
    			peep();
    			break;
    
    			case 4:
    			update();
    			break;
    
    			case 5:
    			display();
    			break;
    
    			case 6:
    			exit();
    
    			default:
    			printf("\n Invalid input" );
    		}
    
    	getch();
    	}while(ans!=6);
    }
    
    
    void push()
    {
    	struct stack *temp;
    
    	temp=(struct stack * ) malloc (sizeof(struct stack));
    
    	printf("Enter any value");
    	scanf("%d",&temp->info);
    
    	temp->next=tos;
    	tos=temp;
    }
    
    void pop()
    {
    	struct stack *temp;
    
    	if(tos==NULL)
    	{
    		printf("Stack is empty");
    	}
    	else
    	{
    		printf("%d value deleted",temp->info);
    
    		temp=tos;
    		tos=temp->next;
    		free(temp);
    	}
    }
    
    
    void display()
    {
    	struct stack *temp;
    
    	if(tos==NULL)
    	{
    		printf("\n Stack is empty ");
    	}
    	else
    	{
    		temp=tos;
    
    		while(temp!=NULL)
    		{
    			printf("\n values are = %d", temp->info);
    
    			temp=temp->next;
    		}
    	}
    }
    
    
    void peep()
    {
    	struct stack *temp;
    
    	int i=1,flag=0,n;
    
    	if(tos==NULL)
    	{
    		printf("\n Sorry stack is empty ");
    	}
    	else
    	{
    		printf("Enter location number to view: ");
    		scanf("%d",&n);
    
    		temp=tos;
    
    		while(temp!=NULL)
    		{
    			if(i==n)
    			{
    				printf("%d value found at %d position" );
    				flag++;
    			}
    
    			i++;
    			temp=temp->next;
    		}
    	}
    
    
    	if(flag==0)
    	{
    		printf("Wrong input");
    	}
    }
    
    
    void update()
    {
    	struct stack *temp;
    
    	int i=1,flag=0,n;
    
    	if(tos==NULL)
    	{
    		printf("\n Sorry stack is empty ");
    	}
    	else
    	{
    		printf("Enter location number to edit : ");
    		scanf("%d",&n);
    
    		temp=tos;
    
    		while(temp!=NULL)
    		{
    			if(i==n)
    			{
    				printf("Enter new value" );
    				scanf("%d",temp->info);
    				flag++;
    			}
    
    			i++;
    			temp=temp->next;
    		}
    	}
    
    
    	if(flag==0)
    	{
    		printf("Wrong input");
    	}
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    When you compile your program, what warnings do you get?

    When I compile it using MinGW gcc with all warnings turned on, I get this output (after correcting the misspelling of #include in the very first line):
    C:TEST>gcc -Wall stack.c conio.o
    stack.c:17: warning: return type of `main' is not `int'
    stack.c: In function `push':
    stack.c:72: warning: implicit declaration of function `malloc'
    stack.c: In function `pop':
    stack.c:95: warning: implicit declaration of function `free'
    stack.c: In function `peep':
    stack.c:143: warning: too few arguments for format
    stack.c: In function `update':
    stack.c:182: warning: format argument is not a pointer (arg 2)

    C:TEST>
    The warning about main is because main is supposed to return int, not void. Some older compilers (are you using Turbo C?) allow void, but it is not correct. main should return zero upon successful completion and non-zero to report that it terminated early because of an error (eg, file could not be opened, wrong number of command-line arguments).

    The warnings about malloc and free are because you did not #include the header file that provides those function prototypes. I #include malloc.h when I use those functions, though I understand that some other header files also provide those two (stdlib.h? -- I just tested that and it does provide them) and I suspect that that might vary from compiler to compiler.

    I added #including stdlib.h to test its providing malloc and free. However, it also provides exit, which takes an int to return to the system when it terminates the program, same as the zero that main is supposed to return. You call exit on line 58, but you do not define it anywhere. You need to correct that. And you should give it a different name so that it doesn't collide with the standard libary function name.

    But I think these are the main sources of what you see:
    stack.c: In function `peep':
    stack.c:143: warning: too few arguments for format
    stack.c: In function `update':
    stack.c:182: warning: format argument is not a pointer (arg 2)


    Line 143 is: printf("%d value found at %d position" );
    You told printf that you would give it two decimal values to display, but you didn't. You need to correct that. I think that what that will cause is for printf to look where those two values should be and use whatever garbage it finds there, so that your program will display two wildly wrong values which are probably very large.

    Line 182 is: scanf("%d",temp->info);
    temp->info is an int. scanf is expecting a pointer to an int. You need to provide that address with the & address operator. You know how that works (because you've done it correctly in other scanf calls), so this was just a typograpical error. But what happens is that whatever garbage is stored in temp->info gets used as an address, which could have disasterous results. At the very least, temp->info will not receive the value you entered, so when you display temp->info you will not see what you expect.

    This is a practical exercise in how important warnings are, because those warnings showed us exactly where you had made certain mistakes. That saved us both a lot of laborious reading through the entire program with no guarantee that we would spot a problem.

    Use warnings! Warnings are far more important than error messages are! Always tell your compiler to display all warnings! Never ignore a warning! Do not run a program until it has compiled cleanly; ie, do not run a program until you have corrected all warnings! Turning off warnings is never an option!

    If you are using Turbo C, there is an options setting somewhere for turning warnings on or off: always turn warnings on! I think there is also an options setting for the level of severity to display: always choose to display all warnings! If you are using Turbo C, I apologize that I cannot remember exactly where those settings are, because I would have to remember back about twenty years.

    PS

    BTW, good job of using code tags! And very good job of formatting your code! Unfortunately, either is a rarity among new members here, so for you to get both things right so soon is truly laudable.
    Last edited by dwise1_aol; February 22nd, 2013 at 11:04 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    One obvious problem is that your temp structure is declared locally in every function. Thus, the structure goes out of existence when you exit the function. One possible solution is to declare only ONE temp structure globally AND remove ALL the locally declared temp structures.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally Posted by BobS0327
    One obvious problem is that your temp structure is declared locally in every function. Thus, the structure goes out of existence when you exit the function. One possible solution is to declare only ONE temp structure globally AND remove ALL the locally declared temp structures.
    Code:
    void push()
    {
    	struct stack *temp;
    
    	temp=(struct stack * ) malloc (sizeof(struct stack));
    
    	printf("Enter any value");
    	scanf("%d",&temp->info);
    
    	temp->next=tos;
    	tos=temp;
    }
    temp is saved to tos, which is global.

    First he needs to correct the problems that the compiler is warning him about, then he can see what else might be wrong.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Code:
    void push()
    {
    	struct stack *temp;
    
    	temp=(struct stack * ) malloc (sizeof(struct stack));
    
    	printf("Enter any value");
    	scanf("%d",&temp->info);
    
    	temp->next=tos;
    	tos=temp;
    }
    temp is saved to tos, which is global.
    Although the tos variable is global, it is NOT being used for dynamic data storage. The stack is being implemented using pointers as a form of a linked list. In order to use a pointer based stack, you must have a (in data structures lingo) Top Of Stack (TOS) pointer which will ALWAYS point to the first record in a pointer based LL implementation of a stack. Thus, the reason the OP poster called the variable tos.

    Let me explain the above code.

    temp is structure that should be added to stack as the new first record. Assuming that there were already several records on the stack, tos which points to the previous first record is assigned to temp->next which now makes it the second record in the stack. Finally, since temp is the new first record, we must now assign it to tos.

    Bottom line tos is a pointer to properly implement a pointer based stack.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Yes, so what is your problem with that?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    No problem. Just correcting your misinformation about the OP code posting
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    What misinformation? I was correcting the misinformation you had posted. You insisted that temp has to be global, whereas there is no reason for that.

    At any rate, we have so far received absolutely no feedback from the original poster whether his program continues to have problems after his correction of the gross errors indicated by the warnings.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    The issue with original code was that the linked list data portion of the LIFO stack was not persistent. The data that was to be added, edited etc. within the various functions existed within the body of the respective functions. But once the function returned, the data no longer existed because it was local to the function. The simple solution was to make the stack linked list global as indicated by the statement in red.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include <stdlib.h>
    
    struct stack
    {
    	int info;
    	struct stack *next;
    }*tos;
    
    // This is the missing global variable
    // needed to make the original code function correctly
    typedef struct stack myStack;
    
    void push(void);
    int pop(void);
    void peep(void);
    void update(void);
    void display(void);
    
    int main(void)
    {
    	char chr;
    	int no;
    	int opt;
    do
       {
         //   clrscr();
    	    system("cls");
            printf ("\n 1: push ");
            printf ("\n 2: pop ");
    		printf ("\n 3 peep ");
    		printf ("\n 4 update ");
    		printf ("\n 5: display ");
            printf ("\n 6: exit");
            printf ("\n Enter your option: ");
            scanf("%d",&opt);
    
               switch(opt)
      {
         case 1: push (); break;
         case 2: no = pop ();
         printf ("\n The delete number was %d ", no);
         break;
    	 case 3: peep(); break;
    	 case 4: update(); break;
         case 5: display(); break;
    	 default: printf ("\n Good Bye ");
         exit(0);
      }
    
         printf ("\n Continue (y/n) ");
         fflush(stdin);
         chr=getche();
    
      }  while (chr=='Y' || chr=='y');
       return 0;
    }
    
    void push(void)
    {
    	//	struct stack *temp;
    	myStack *temp;
    	temp=(struct stack * ) malloc (sizeof(struct stack));
    
    	if(temp == NULL)
    	{
    		printf("Failed\n");
    		return;
    	}
    	printf("Enter any value ");
    	scanf("%d",&temp->info);
    	temp->next=tos;
    	tos=temp;
    }
    
    int pop(void)
    {
    	//	struct stack *temp;
    	myStack *temp;
    	temp = tos;
    	if(tos==NULL)
    	{
    		printf("Stack is empty");
    	}
    	else
    	{
    		//		printf("%d value deleted",temp->info);
    		//		temp=tos;
    		tos=tos->next;
    		free(temp);
    	}
    	return(temp->info);
    }
    
    
    void display(void)
    {
    	//	struct stack *temp;
    	myStack *temp;
    	temp = tos;
    	if(temp==NULL)
    	{
    		printf("\n Stack is empty ");
    	}
    	else
    	{
    		//	temp=tos;
    		while(temp!=NULL)
    		{
    			printf("\n values are = %d", temp->info);
    			temp=temp->next;
    		}
    	}
    }
    
    
    void peep(void)
    {
    	//	struct stack *temp;
    	myStack *temp;
    	int i=1,flag=0,n;
    	temp = tos;
    	if(temp==NULL)
    	{
    		printf("\n Sorry stack is empty ");
    	}
    	else
    	{
    		printf("Enter location number to view: ");
    		scanf("%d",&n);
    		//	temp=tos;
    		while(temp!=NULL)
    		{
    			if(i==n)
    			{
    				printf("value %d found at %d position", temp->info,i );
    				flag++;
    			}
    			i++;
    			temp=temp->next;
    		}
    	}
    	if(flag==0)
    	{
    		printf("Wrong input");
    	}
    }
    
    
    void update(void)
    {
    //	struct stack *temp;
    	myStack *temp;
    	temp = tos;
    	int i=1,flag=0,n;
    
    	if(temp==NULL)
    	{
    		printf("\n Sorry stack is empty ");
    	}
    	else
    	{
    		printf("Enter location number to edit : ");
    		scanf("%d",&n);
    	//	temp=tos;
    		while(temp!=NULL)
    		{
    			if(i==n)
    			{
    				printf("Enter new value " );
    				scanf("%d",&temp->info);
    				flag++;
    			}
    			i++;
    			temp=temp->next;
    		}
    	}
    	if(flag==0)
    	{
    		printf("Wrong input");
    	}
    }
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally Posted by BobS0327
    The issue with original code was that the linked list data portion of the LIFO stack was not persistent. The data that was to be added, edited etc. within the various functions existed within the body of the respective functions. But once the function returned, the data no longer existed because it was local to the function. The simple solution was to make the stack linked list global as indicated by the statement in red.
    What are you talking about? The struct declaration is indeed global, the linked list portion of the stack is indeed persistent (tos, remember? You yourself described how it works so how could you be blind to that?), and the nodes that are malloc'd in push are indeed persistent until they are free'd in pop. That typedef of yours is completely unnecessary, just simply more convenient than having to type struct stack all the time.

    The reason why we're not hearing any more from the OP is that once he corrected the problems pointed out by the warnings, the program works.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    The reason why we're not hearing any more from the OP is that once he corrected the problems pointed out by the warnings, the program works
    You just don't realize that Dev Shed isn't the "only game in town". There are a lot of other programming forums out there. I hang out on a lot of those forums and have seen instances of where users post on as many as four different forums simultaneously. I know this is not politically correct but it's just a fact of life.

    Without exception, all the users gravitate to the forums where they make the most progress in the least amount of time.

    Thus, it's the quality of the posts that separate the mediocre forums from the exceptional forums.

    Maybe Devil211272 found one such forum?
  22. #12
  23. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    And yet, after the corrections to the problems I pointed out, the program does work.

    Just own up to the fact that you were wrong and move along, sonny. Or don't own up to it and still move along.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    And yet, after the corrections to the problems I pointed out, the program does work
    OK. I'll throw down the gauntlet, SONNY!!! Make the changes that you have outlined above AND post the WORKING code.

    In other words, SONNY, put up or shut up!!!!!
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    What the hell is your problem? You submit misinformation and instead of learning from your error being pointed out, you insist upon your misinformation and add even more, insisting that you are right when you clearly are not.

    Wait a moment here. Aren't you that idiot who insisted that the standards of C99 are required for sockets programming to work? Now that was a laugh!

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include <malloc.h>
    
    struct stack
    {
    	int info;
    	struct stack *next;
    }*tos=NULL;
    
    void push();
    void pop();
    void peep();
    void update();
    void display ();
    
    int main()
    {
    	int ans;
    
    
    	do
    	{
    		clrscr();
    
    		printf("\n 1-push ");
    		printf("\n 2-pop ");
    		printf("\n 3-peep ");
    		printf("\n 4-update ");
    		printf("\n 5-display ");
    
    		printf("\n Enter chocie ");
    		scanf("%d",&ans);
    
    		switch (ans)
    		{
    			case 1:
    			push();
    			break;
    
    			case 2:
    			pop();
    			break;
    
    			case 3:
    			peep();
    			break;
    
    			case 4:
    			update();
    			break;
    
    			case 5:
    			display();
    			break;
    
    			case 6:
    			return 0;
    
    			default:
    			printf("\n Invalid input" );
    		}
    
    	getch();
    	}while(ans!=6);
    
        return 0;
    }
    
    
    void push()
    {
    	struct stack *temp;
    
    	temp=(struct stack * ) malloc (sizeof(struct stack));
    
    	printf("Enter any value");
    	scanf("%d",&temp->info);
    
    	temp->next=tos;
    	tos=temp;
    }
    
    void pop()
    {
    	struct stack *temp;
    
    	if(tos==NULL)
    	{
    		printf("Stack is empty");
    	}
    	else
    	{
    		printf("%d value deleted",temp->info);
    
    		temp=tos;
    		tos=temp->next;
    		free(temp);
    	}
    }
    
    
    void display()
    {
    	struct stack *temp;
    
    	if(tos==NULL)
    	{
    		printf("\n Stack is empty ");
    	}
    	else
    	{
    		temp=tos;
    
    		while(temp!=NULL)
    		{
    			printf("\n values are = %d", temp->info);
    
    			temp=temp->next;
    		}
    	}
    }
    
    
    void peep()
    {
    	struct stack *temp;
    
    	int i=1,flag=0,n;
    
    	if(tos==NULL)
    	{
    		printf("\n Sorry stack is empty ");
    	}
    	else
    	{
    		printf("Enter location number to view: ");
    		scanf("%d",&n);
    
    		temp=tos;
    
    		while(temp!=NULL)
    		{
    			if(i==n)
    			{
    				printf("%d value found at %d position", temp->info, n);
    				flag++;
    			}
    
    			i++;
    			temp=temp->next;
    		}
    	}
    
    
    	if(flag==0)
    	{
    		printf("Wrong input");
    	}
    }
    
    
    void update()
    {
    	struct stack *temp;
    
    	int i=1,flag=0,n;
    
    	if(tos==NULL)
    	{
    		printf("\n Sorry stack is empty ");
    	}
    	else
    	{
    		printf("Enter location number to edit : ");
    		scanf("%d",&n);
    
    		temp=tos;
    
    		while(temp!=NULL)
    		{
    			if(i==n)
    			{
    				printf("Enter new value" );
    				scanf("%d",&temp->info);
    				flag++;
    			}
    
    			i++;
    			temp=temp->next;
    		}
    	}
    
    
    	if(flag==0)
    	{
    		printf("Wrong input");
    	}
    }
    It's not great, but then all I did was to make the corrections indicated by the warnings. The stack operations work and the stack and its elements do persist outside of the individual functions. BTW, that getch(); at line 64 requires you to press enter one more time after each entry. And I replaced the exit() in line 58 with a return 0;.


    And you might want to work on your personality defects. If you're wrong about something, then accept it and learn from it. Or to put it in the words of an old humor piece: when you've reached rock-bottom, the last thing you want to do is to start digging.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Well, your app consistently crashes on executing the pop function and I can cause peep and display to crash if I go back between pop adn display/peep four or five times.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo