Stack in C

Thread Solved

Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Stack in C

 
0
  #1
Apr 1st, 2007
Hello guyz i'm trying to write a stack program in C...

Push one character onto the stack. Your function should have the prototype:
void push(POINTER *Top,stackitem a);
2. Push a string of characters into a stack. Your function should have the prototype:
void push_string(POINTER *Top,char *string);
Your implementation should make use of the push() function.
3. Print the contents the stack. Your function should have the prototype:
void print_stack(POINTER Top);
It should not modify the stack in any way, only display its contents.
4. Pop the top character from the stack. Your function should have the prototype:
stackitem pop(POINTER *Top);.
5. Delete the stack, i.e. remove all items from the stack. Your function should have the
prototype:
void delete_stack(POINTER *Top);
Your implementation should make use of the pop() function.


this how i went so far but the problem i dont know how to push a string of character using the function push and i'm not sure ifthe print_stack function is right...
i'll appreciate any help !
#include<stdio.h>
#include<stdlib.h>
 
typedefchar stackitem;
struct stack {
stackitem d;
struct stack *next;
};
typedefstruct stack ELEMENT;
typedef ELEMENT *POINTER;
 
 
 
void push(POINTER *Top, stackitem a)
/* put a into the top of the stack */
{
POINTER temp;
temp = malloc(sizeof(ELEMENT));
temp->d = a;
temp->next = *Top;
*Top = temp;
}
void push_string(POINTER *Top,char *string)
{
}
void pop(POINTER *Top)
{
POINTER Top1 = *Top;
if (*Top != NULL)
{
*Top = Top1->next;
free(Top1);
}
else
{
printf("Empty stack.\n");
}
}
 
void print_stack(POINTER Top){
POINTER copy= Top;
while(copy!=NULL)
{ 
printf("%c",copy->d);
copy=copy->next;
}
}
int main()
{
int i;
char buff[20];
POINTER start=NULL;
fgets(buff,20,stdin);

for(i=0;buff[i]!='\0';i++)
{
push(&start,buff[i]);
}
 
print_stack(start);
 

}
Thanks guyz
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,672
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stack in C

 
0
  #2
Apr 1st, 2007
push_string() could use a for loop like you have in main() in push_string().

You are basing your stack on a list and adding each new node to the front of the list, which is fine. However, the very first node in the list has to be somewhat different than any other node in the list in that temp->next must be NULL in order for copy to be NULL at some point when you print the list.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: Stack in C

 
0
  #3
Apr 1st, 2007
you mean the same as push but i'll add a for loop for example:
for(i=0;buff[i]!='\0';i++)
{
void push(POINTER *Top, stackitem a)
}
so it keeps taking characters untill eof right ?!
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Stack in C

 
0
  #4
Apr 1st, 2007
I tried running it and after a few small touchups (no logical changes though) it seems that you're off to a good start. Here's how it went:
  1. $ ./a.out
  2. this is a line
  3.  
  4. enil a si siht
As you can see, it's working for single items and print_stack is fine

for push_string, you can just use a loop over the char*. Since it's really basic, I'll just show you:
  1. void push_string(POINTER *Top,char *string)
  2. {
  3. int i = 0; // for indexing string
  4. while(string[i]) // while not end of string
  5. {
  6. push(Top, string[i]); // push item
  7. i++; // increment
  8. }
  9. }
This could also be done pretty easily in a for loop.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: Stack in C

 
0
  #5
Apr 1st, 2007
Originally Posted by Infarction View Post
I tried running it and after a few small touchups (no logical changes though) it seems that you're off to a good start. Here's how it went:
  1. $ ./a.out
  2. this is a line
  3.  
  4. enil a si siht
As you can see, it's working for single items and print_stack is fine

for push_string, you can just use a loop over the char*. Since it's really basic, I'll just show you:
  1. void push_string(POINTER *Top,char *string)
  2. {
  3. int i = 0; // for indexing string
  4. while(string[i]) // while not end of string
  5. {
  6. push(Top, string[i]); // push item
  7. i++; // increment
  8. }
  9. }
This could also be done pretty easily in a for loop.
aw thats beautifull mate !
but why it keeps giving me a Segmentation fault when i try to add the push_string function inside the main ?!
am i using the wrong syntax ?!

for example
int main()
{
int i;
char buff[20];
POINTER start=NULL;
fgets(buff,20,stdin);

for(i=0;buff[i]!='\0';i++)
{
push_string(&start,buff);
//push(&start,buff[i]);
}
 
print_stack(start);
 

}
sorry champ i'm newbie to C and thank you !
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,672
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stack in C

 
0
  #6
Apr 1st, 2007
Uck! The loop you had in main() works fine for pushing one char at a time on to the stack from within main(). push_string is completely different than that in that the pushing needs to be done from within push_string(), not form main()
  1. int main()
  2. {
  3. int i;
  4. char buff[20];
  5. POINTER start=NULL;
  6. fgets(buff,20,stdin);
  7.  
  8. //add buff to stack from within main()
  9. for(i=0;buff[i]!='\0';i++)
  10. {
  11. push(&start,buff[i]);
  12. }
  13.  
  14. //print stack
  15. print_stack(start);
  16.  
  17. //delete stack
  18. delete_stack(&start);
  19.  
  20. //add buff to stack from within push_string()
  21. push_string(&start, buff);
  22.  
  23. //print stack
  24. print_stack(start);
  25.  
  26. //delete stack
  27. delete_stack(&start);
  28. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: Stack in C

 
0
  #7
Apr 1st, 2007
i'm trying to get the delete function but do i have to use loop as wel to delete the stack contents ?!
this is what i got without using the pop function.
  1. delete_stack(POINTER *TOP)
  2. {
  3. POINTER TEMP2;
  4. while (Top !=NULL)
  5. {
  6. TEMP2= Top;
  7. Top=Top->next;
  8. delete TEMP2;
  9. }
  10. }

thanks
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,672
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Stack in C

 
0
  #8
Apr 1st, 2007
According to the instructions you posted you should have a pop() function. To delete the stack you will probably use a loop to repeated call the pop() function until the stack is empty.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 63
Reputation: rowly is an unknown quantity at this point 
Solved Threads: 3
rowly's Avatar
rowly rowly is offline Offline
Junior Poster in Training

Re: Stack in C

 
0
  #9
Apr 1st, 2007
Originally Posted by Lerner View Post
According to the instructions you posted you should have a pop() function. To delete the stack you will probably use a loop to repeated call the pop() function until the stack is empty.
umm ehehe :lol: thats pretty easy i couldnt see that :cheesy:
so
delete_stack(POINTER *TOP)
{
POINTER TOP3;
while (Top !=NULL)
{
  pop(Top);
*Top = Top3->next;
  }
}
is that it ?!
DarkCoder+
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Stack in C

 
0
  #10
Apr 1st, 2007
I don't think you even need Top3 (and you're using it uninitialized, which is bad). Top will be updated as you call pop, so you could probably just do:
  1. while(Top)
  2. pop(Top);
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC