943,536 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 22280
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 1st, 2007
0

Stack in C

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Apr 1st, 2007
0

Re: Stack in C

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 1st, 2007
0

Re: Stack in C

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 ?!
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Apr 1st, 2007
0

Re: Stack in C

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.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 1st, 2007
0

Re: Stack in C

Click to Expand / Collapse  Quote originally posted by Infarction ...
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 !
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Apr 1st, 2007
0

Re: Stack in C

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. }
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 1st, 2007
0

Re: Stack in C

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
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Apr 1st, 2007
0

Re: Stack in C

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 1st, 2007
0

Re: Stack in C

Click to Expand / Collapse  Quote originally posted by Lerner ...
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 ?!
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
rowly is offline Offline
65 posts
since Sep 2006
Apr 1st, 2007
0

Re: Stack in C

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);
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: new ISO `for' scoping?
Next Thread in C Forum Timeline: having problem with array of structures





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC