0

// hi guys actually i need some suggestion or help .. i want to add STRING capability in this following Queue program. currently it just eccepts the chars . so if somone can help me out in getting the String output. thanks

#include "stdafx.h"
/*****  Structure template  *****/
struct list{
                char data;
                struct list *next;
};
/*****  Redefining struct list as node  *****/
    typedef struct list node;
    void qinsert(node**,node**); /** Inserting character function in               queue **/
 void qdelete(node**,node**); /** Deleting character function in queue **/
void disp(node*);            /** Output displaying function **/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|*"front" and "rear" pointer needed to be changed when inserting and *|
|*  deleting so addresses of them is passed in qinsert and qdelete   *|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

void main()
{
    int opt;        /* Option inputing variable */    
    char ch;        /* choice inputing variable */    
    node *front;    /* front pointer in queue*/    
    node *rear;     /* rear pointer in queue */
    rear=front=NULL;
    do
    {
        printf("\nEnter your option to perform in a queue\n");
        printf("\n1. Insert\n");
        printf("\n2. delete\n");
        printf("\n3. Display the list\n");
        scanf("%d",&opt);
        switch(opt)
        {
        case 1:
            qinsert(&front,&rear);
            break;
        case 2:
            qdelete(&front,&rear);
            break;
        case 3:
            disp(front);
            break;
        }
        printf("\nDo you wish to continue[y/n]\n");
        ch=(char)getche();
    }while(ch=='Y' || ch=='y');
     printf("\nPress any key to exit\n");
    getch();
}


void qinsert(node **front,node **rear)
{
    node *newnode;      /* New node to be inserted */
    newnode=(node*)malloc(sizeof(node));
    newnode->next=NULL;
    printf("\nEnter the character to push\n");
    fflush(stdin);
    scanf("%c",&(newnode->data));
    if(*front==NULL && *rear==NULL)
    {
        *front=newnode;
        *rear=newnode;
    }
    else
    {
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
    |*  "rear" points to last node whose next field must be pointed to *|
    |*  newnode and then rear points to the latest node i.e. new node  *|
    \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        (*rear)->next=newnode;
        *rear=newnode;
    }
}

void qdelete(node **front,node **rear)
{
    node *delnode;      /* Node to be deleted */
    if((*front)==NULL && (*rear)==NULL)
        printf("\nQueue is empty to delete any element\n");
    else
    {
        delnode=*front;
        (*front)=(*front)->next;
        free(delnode);
    }
}
void disp(node *f)
{
    while(f!=NULL)
    {
        printf(" %c ",f->data);
        f=f->next;
    }
}

Edited by WaltP: Added CODE Tags

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

change the structure to use std::string instead of the char data. Then change the insert function to get a string from the keyboard instead of a single character. You will probably need to make other appropriate changes to your program as well.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.