![]() |
| | #1 |
| Banned Join Date: Oct 2001
Posts: 1,784
| 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));
}
}
|
| Troll_King is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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 dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Banned Join Date: Oct 2001
Posts: 1,784
| 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. |
| Troll_King is offline | |
| | #4 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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 );
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #5 |
| Banned Join Date: Oct 2001
Posts: 1,784
| 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. |
| Troll_King is offline | |
| | #6 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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" .
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #7 |
| Banned Join Date: Oct 2001
Posts: 1,784
| For some reason this was not a simple exercise for me this morning. It took me a few hours to figure it out. |
| Troll_King is offline | |
| | #8 |
| Registered User Join Date: Oct 2001
Posts: 422
| 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.
__________________ Ramble on... |
| EvenFlow is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Finished Stack | the pooper | C Programming | 11 | 02-02-2005 10:52 AM |
| Stacks | Cmuppet | C Programming | 19 | 10-13-2004 02:32 PM |
| linked list stack question | Drew | C++ Programming | 2 | 09-11-2003 05:05 AM |
| stack implementation problem-help needed | sanju | C Programming | 1 | 12-10-2002 07:29 AM |
| Accessing data in the program stack | dajur | C Programming | 1 | 09-23-2001 10:15 PM |