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