| | |
Stack in C
Thread Solved |
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 !
Thanks guyz
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); }
DarkCoder+
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
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.
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.
you mean the same as push but i'll add a for loop for example:
so it keeps taking characters untill eof right ?!
for(i=0;buff[i]!='\0';i++) { void push(POINTER *Top, stackitem a) }
DarkCoder+
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:
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:
This could also be done pretty easily in a for loop.
C Syntax (Toggle Plain Text)
$ ./a.out this is a line enil a si siht

for push_string, you can just use a loop over the char*. Since it's really basic, I'll just show you:
C Syntax (Toggle Plain Text)
void push_string(POINTER *Top,char *string) { int i = 0; // for indexing string while(string[i]) // while not end of string { push(Top, string[i]); // push item i++; // increment } }
•
•
•
•
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:
As you can see, it's working for single items and print_stack is fineC Syntax (Toggle Plain Text)
$ ./a.out this is a line enil a si siht
for push_string, you can just use a loop over the char*. Since it's really basic, I'll just show you:
This could also be done pretty easily in a for loop.C Syntax (Toggle Plain Text)
void push_string(POINTER *Top,char *string) { int i = 0; // for indexing string while(string[i]) // while not end of string { push(Top, string[i]); // push item i++; // increment } }
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); }
DarkCoder+
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
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()
C Syntax (Toggle Plain Text)
int main() { int i; char buff[20]; POINTER start=NULL; fgets(buff,20,stdin); //add buff to stack from within main() for(i=0;buff[i]!='\0';i++) { push(&start,buff[i]); } //print stack print_stack(start); //delete stack delete_stack(&start); //add buff to stack from within push_string() push_string(&start, buff); //print stack print_stack(start); //delete stack delete_stack(&start); }
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.
thanks
this is what i got without using the pop function.
C Syntax (Toggle Plain Text)
delete_stack(POINTER *TOP) { POINTER TEMP2; while (Top !=NULL) { TEMP2= Top; Top=Top->next; delete TEMP2; } }
thanks
DarkCoder+
•
•
•
•
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.
so
delete_stack(POINTER *TOP)
{
POINTER TOP3;
while (Top !=NULL)
{
pop(Top);
*Top = Top3->next;
}
} DarkCoder+
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:
C Syntax (Toggle Plain Text)
while(Top) pop(Top);
![]() |
Similar Threads
- Stack Queue Fstream (C++)
- Reverse Output (Stack) (C++)
- TCP/IP stack whacked by malware; no DNS resolution (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: new ISO `for' scoping?
- Next Thread: having problem with array of structures
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






