problem with stack and queue

Discussion in 'C' started by shania, Aug 28, 2010.

  1. shania

    shania New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hello guys!

    my goal is to test if the string inputted by the user is palindrome or not using stack and queue..but now i am having a trouble in my queue program...can you please check my code and tell me what is the problem...thank you..

    here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 100
    
    typedef struct stack {
            char value;
            struct stacknode *next;
    }STACK_NODE;
    
    typedef struct ListNode {
            char element;
            struct ListNode *next;
    }ListNode;
    
    typedef struct Q {
            ListNode *frontOfQueue;
            ListNode *rearOfQueue;
            int numElements;
    } Queue;
    
    
    void initStack(STACK_NODE** head);
    void push(STACK_NODE** head, char input);
    void stackfunction(char* input);
    int isEmptyStack(STACK_NODE** head);
    
    void initQueue(Queue *q);
    void enqueue(Queue *q, char input);
    int IsEmptyQ(Queue* q);
    void queuefunction(char *input);
    
    main()
    {
        char* string = (char*)malloc(MAX+1* sizeof(char));
        char* stringq = (char*)malloc(MAX+1* sizeof(char));
    
        char option;
        do
        {
            printf("\n\tInput string: ");
            fflush(stdin);
            gets(string);
            stackfunction(string);
            queuefunction(stringq);
            printf("\n\tstack:  %s",string);
            printf("\n\tqueue:  %s",stringq);
    
            printf("\n\n\tDo you want to continue? [Y/N]: ");
            option=getchar();
        }while(toupper(option)=='y');
        printf("\n\n\tProgram Terminated...\n");
    	return 0;
    }
    
    void initStack(STACK_NODE** head){
        *head = NULL;
    }
    
    void push(STACK_NODE** head, char input){
        STACK_NODE* temp = (STACK_NODE*)malloc(sizeof(STACK_NODE));
        temp->value =input;
        temp->next = *head;
        *head = temp;
    }
    int isEmpty(STACK_NODE** head){
    
        if(*head==NULL){
            printf("Stack is empty!");
            return 1;
        }
    }
    
    void stackfunction(char* input){
    
        STACK_NODE* head = (STACK_NODE*)malloc(sizeof(STACK_NODE));
        initStack(&head);
    
        while(*input != '\0') {
            push(&head,*input);
                  input++;
       }
    }
    
    void queuefunction(char *input){
    
        Queue* head = (Queue*)malloc(sizeof(Queue));
        initQueue(&head);
    
        while(*input != '\0') {
            enqueue(&head,*input);
                  input++;
       }
    }
    
    void initQueue(Queue *q){
         q->frontOfQueue = NULL;
         q->rearOfQueue = NULL;
         q->numElements=0;
    }
    
    void enqueue(Queue *q, char input) {
         ListNode *temp= (ListNode*)malloc(sizeof(ListNode));
    
         //out of memory
         if (temp == NULL){
                  printf("error");
                  return;
         }
    
         //add element to listnode
         temp->element =input;
         temp->next=NULL;
    
         if(IsEmptyQ(&(*q))){
                  q->frontOfQueue = temp;
                  q->rearOfQueue = temp;
         }
         else {
                  q->rearOfQueue->next = temp;
                  q->rearOfQueue = temp;
         }
         q->numElements++;
    }
    
    int IsEmptyQ(Queue* q)
    {
      return(q->frontOfQueue==NULL);
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice