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>
 
typedef char stackitem;
struct stack {
stackitem d;
struct stack *next;
};
typedef struct 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

Recommended Answers

All 10 Replies

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 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 ?!

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:

$ ./a.out 
this is a line

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:

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
  }
}

This could also be done pretty easily in a for loop.

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:

$ ./a.out 
this is a line
 
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:

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
  }
}

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 !

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()

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.

delete_stack(POINTER *TOP)
{
POINTER TEMP2;
while (Top !=NULL)
                  {
TEMP2= Top;
Top=Top->next;
delete TEMP2;
                    }
}

thanks

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.

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 ?!

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:

while(Top)
  pop(Top);

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:

while(Top)
  pop(Top);

Thats it zn :)
thanks alote guyz i appreciate it !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.