-
Stack Program Here
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct stack {
int sdata;
struct stack *next;
} ;
struct stack *Push (struct stack *llist, int data);
struct stack Pop (struct stack ** llist);
void ReleaseStack(struct stack * llist);
int main () {
struct stack *llist = NULL;
int data, choice;
do
{
printf("\nEnter choice (1: Push) (2: Pop) (3: end) : ");
scanf("%d",&choice);
while(getchar() != '\n') continue;
switch (choice)
{
case 1:
printf ("Integer to Push onto the stack: ");
scanf ("%d", &data);
//clear memory buffer
while(getchar() != '\n') continue;
llist = Push(llist,data);
break;
case 2:
if (llist == NULL)
{
printf ("stack empty!\n");
}else
{
struct stack temp = Pop(&llist);
printf("%d Popped.\n", temp.sdata);
}
case 3:
break;
default:
printf ("Invalid menu choice - try again\n");
break;
}
} while (choice != 3);
ReleaseStack(llist);
return 0;
}
struct stack * Push(struct stack * llist, int data)
{
struct stack *newp = NULL;
if(llist == NULL)
{
newp = (struct stack *) malloc (sizeof (struct stack));
newp->next = NULL;
newp->sdata = data;
llist = newp;
return llist;
}else
{
newp = (struct stack *) malloc (sizeof (struct stack));
newp->sdata = data;
newp->next = llist;
return newp;
}
}
struct stack Pop (struct stack ** llist)
{
//holds the return value (data)
struct stack temp;
//use to free the head node
struct stack *freep = *llist;
//reposition the head of the stack for post Pop
if((*llist)->next == NULL)
{
*llist = NULL;
temp = *freep;
free(freep);
}else
{
*llist = (*llist)->next;
//get data value
temp = *freep;
//free head node
free(freep);
}
return temp;
}
void ReleaseStack(struct stack * llist)
{
while(llist != NULL)
{
printf("releasing %d\n", Pop(&llist));
}
}
Here is a stack program. It seems to be the topic of the day. This is the stack program that I wrote this morning.
-
Ohh goody - here's my attempt.
Code:
#include <stdio.h>
#include <stdlib.h>
struct stack {
int data;
struct stack *next;
};
// push to front
// way simpler than push to rear
void push ( struct stack **head, int val ) {
struct stack *node = malloc( sizeof(struct stack) );
node->data = val;
node->next = *head;
*head = node;
}
int pop ( struct stack **head ) {
struct stack *node = *head;
int result = node->data;
*head = (*head)->next;
free( node );
return result;
}
int main ( ) {
struct stack *s = NULL; // its empty
int i;
for ( i = 0 ; i < 10 ; i++ ) {
push( &s, i );
}
for ( i = 0 ; i < 10 ; i++ ) {
printf( "%d\n", pop(&s) );
}
printf( "The stack is %sempty\n", s == NULL ? "" : "not " );
return 0;
}
-
If you push and it is empty than it crashes. I should have also used a pointer to a pointer to update main for my push function. I did that for my pop function. Why didn't I do it for the push? Also you add the node to the head of the stack, ofcourse, this is true for a stack.
-
> If you push and it is empty than it crashes.
> *head = node;
this updates the variable s in main, not what s points to.
I put this line of code in the push loop, and it al looks good.
printf( "%p %p\n", s, s->next );
-
Actually your code is very clear. I understand it perfectly. This is similar to my code, for the pop function. I was just saying that if you pop too many times than the program crashes. There is no error checking. I had a hard time this morning making mine bulletproof. I have it now but I could revise the push function so that it is automatically updated. I wasn't used to using pointers to pointers. When you pop the last node in the stack, I didn't know where to put the head pointer until I realized I should set it to NULL. I was also having difficulties freeing the poped node but I finally figured it out.
-
> I was just saying that if you pop too many times than the program crashes.
Well I was confused when you said push in the previous message.
> There is no error checking.
As they say in books "left as an easy exercise for the reader" . :p
-
For some reason this was not a simple exercise for me this morning. It took me a few hours to figure it out.
-
I'll post mine again. It does use pointers, but rather differently to what has been done here. It is using a simple contiguos stack.