Thread: Queue linked list prob.. need help and suggestions

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Queue linked list prob.. need help and suggestions

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype.h>
    #include<malloc.h>
    #include<windows.h>

    void clear(){
    system("cls");
    }

    void gotoxy(int x,int y){
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE),coord);
    }

    void enqueue();
    void dequeue();
    void display();

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++

    struct node
    {
    int bookid; double bookprice;
    char booktitle[30]; char bookauthor[30];
    struct node *link;
    }*start=NULL, *end=NULL, *new,*temp,*e;
    typedef struct node N;

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++

    main()
    {

    int ch;
    do
    {

    clear();

    gotoxy(25,7);
    printf("***** ABC Booksellers *****");
    gotoxy(26,8);
    printf("Please select your choice:");
    gotoxy(29,10);
    printf("1.Enqueue / Append ");
    gotoxy(29,11);
    printf("2.Dequeue / Serve ");
    gotoxy(29,12);
    printf("3.Display");
    gotoxy(29,13);
    printf("4.Exit");
    gotoxy(28,15);
    printf("Enter your choice : ");
    scanf("%d",&ch);
    gotoxy(25,17);

    printf("\n");

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++

    switch(ch)
    {

    case 1:
    clear();
    enqueue();
    break;

    case 2:
    clear();
    break;

    case 3:
    clear();
    break;

    case 4:
    clear();
    gotoxy(32,10);
    printf("THANK YOU !!!");
    Sleep(3000);
    clear();
    printf("\n\t\t");
    exit(0);

    break;

    default:
    gotoxy(32,18);
    printf("Invalid choice\n\n");
    break;
    }

    }
    while(ch<=3);
    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++

    void enqueue()
    {
    new=(N*)malloc(sizeof(N));

    gotoxy(23,3);
    printf("Note: Please refrain from using spaces");
    gotoxy(19,4);
    printf("Instead of Spaces please use a underscore.. ^_^");

    gotoxy(24,7);
    printf("***** Enter Book Details *****");

    gotoxy(26,9);
    printf("Enter Book ID: ");
    scanf("%d",&new->bookid);

    gotoxy(26,10);
    printf("Enter Book TITLE: ");
    scanf("%s",&new->booktitle);

    gotoxy(26,11);
    printf("Enter Book AUTHOR: ");
    scanf("%s",&new->bookauthor);

    gotoxy(26,12);
    printf("Enter Book Price: P");
    scanf("%lf",&new->bookprice);

    gotoxy(14,14);
    printf("Thank you, Book has been successfully appended");

    Sleep(1500);

    new->link=NULL;
    if(end==NULL)
    start=new;
    else
    {
    e=end;
    while(e->link!=NULL)
    e=e->link;
    e->link=new;
    }
    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++

    void dequeue()
    {
    if(start==NULL)
    printf("\nStack is empty\n");
    else if(start->link==NULL)
    {

    gotoxy(28,7);
    printf("***** Book Deleted *****");

    gotoxy(24,9);
    printf("Book ID: %d\n",start->bookid);

    gotoxy(24,10);
    printf("Book Title: %s",start->booktitle);

    gotoxy(24,11);
    printf("Book Author: %s",start->bookauthor);

    gotoxy(24,12);
    printf("Book Price: P%.2lf",start->bookprice);

    gotoxy(24,13);
    printf("Book has been successfully served!");

    Sleep(1500);

    free(start);
    start=NULL;
    }

    else
    {
    e=end;
    while(e->link!=NULL)
    {
    temp=e;
    e=e->link;
    }
    gotoxy(28,7);
    printf("***** Book Deleted *****");

    gotoxy(24,9);
    printf("Book ID: %d\n",end->bookid);

    gotoxy(24,10);
    printf("Book Title: %s",end->booktitle);

    gotoxy(24,11);
    printf("Book Author: %s",end->bookauthor);

    gotoxy(24,12);
    printf("Book Price: P%.2lf",end->bookprice);

    gotoxy(24,13);
    printf("Book has been successfully served!");

    Sleep(1500);

    temp->link=NULL;
    free(e);
    }
    }


    //++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++


    void display()
    {
    if(start==NULL)
    printf("\nStack is empty\n");

    else
    {
    gotoxy(28,7);
    printf("***** Stored Books *****");
    e=start;
    while(e!=NULL)
    {

    printf("\n\t\t Book ID: %d",e->bookid);
    printf("\n\t\t Book Title: %s",e->booktitle);
    printf("\n\t\t Book Author: %s",e->bookauthor);
    printf("\n\t\t Book Price: P%.2lf\n\n",e->bookprice);

    Sleep(3000);

    e=e->link;
    }
    printf("\n");

    }

    }

    im really getting confused olready cant seem to serve data stored.. please help me.. really need it..
    Last edited by anthnzz; 04-11-2010 at 05:49 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    You mean something like this....

    Please note that this is incomplete....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct node
    {
      int bookid;
      double bookprice;
      char booktitle[30];
      char bookauthor[30];
      struct node *link;
    };
    
    void enqueue(struct node**);
    struct node* append();
    void display(struct node*);
    void menu();
    void cleanup(struct node*);
    
    struct node* append(void)
    {
      struct node* head = NULL;
      struct node* tail;
      int i;
      int n;
    
      (void)printf("How many books do you want to enter? ");
      fflush(stdout);
      scanf("%d", &n);
    
      enqueue(&head);
      tail = head;
    
      for (i = 2; i <= n; i++) {
        enqueue(&(tail->link));
        tail = tail->link;
      }
    
      return head;
    }
    
    void enqueue(struct node** head_ref)
    {
      int bookid;
      double bookprice;
      char booktitle[30];
      char bookauthor[30];
     
      struct node* new = malloc(sizeof(struct node));
    
      (void)printf("Note: Please refrain from using spaces\n");
      (void)printf("Instead of Spaces please use a underscore.. ^_^\n");
      (void)printf("***** Enter Book Details *****\n");
    
      printf("Enter Book ID: ");
      fflush(stdout);
      scanf("%d",&bookid);
      new->bookid = bookid;
    
      printf("Enter Book TITLE: ");
      fflush(stdout);
      scanf("%s", booktitle);
      (void)strcpy(new->booktitle, booktitle);
    
      printf("Enter Book AUTHOR: ");
      fflush(stdout);
      scanf("%s",bookauthor);
      (void)strcpy(new->bookauthor, bookauthor);
    
      printf("Enter Book Price: ");
      fflush(stdout);
      scanf("%lf", &bookprice);
      new->bookprice = bookprice;
    
      new->link = *head_ref;
      *head_ref = new;
    
      printf("Thank you, Book has been successfully appended\n");
    
    }
    
    void display(struct node* head)
    {
      struct node* current = head;
    
      while(current != NULL) {
        (void)printf("%d\n", current->bookid);
        (void)printf("%.2f\n", current->bookprice);
        (void)printf("%s\n", current->booktitle);
        (void)printf("%s\n", current->bookauthor);
        (void)printf("-------------------\n");
    
        current = current->link;
      }
    }
    void menu(void)
    {
      (void)printf("***** ABC Booksellers *****\n");
      (void)printf("Please select your choice:\n");
      (void)printf("1.Enqueue / Append \n");
      (void)printf("2.Dequeue / Serve \n");
      (void)printf("3.Display \n");
      (void)printf("4.Exit \n");
      (void)printf("Enter your choice : ");
      fflush(stdout);
    }
    
    void cleanup(struct node* head)
    {
      struct node *temp;
    
      while (head != NULL) {
        temp = head;
        head = head->link;
        free(temp);
      }
    }
    
    int main(void)
    {
      int ch;
      struct node* head = NULL;
    
      menu();
      
      if ((scanf("%d",&ch)) == 1) {
        switch(ch)
          {
          case 1:
            head = append();
            break;
    
          case 2:
            break;
    
          case 3:
            break;
    
          case 5:
            printf("THANK YOU !!!\n");
            exit(0);
            break;
    
          default:
            printf("Invalid choice\n\n");
            break;
          }
    
      } else {
        (void)fprintf(stderr, "Invalid input\n");
        exit(1);
      }
    
      display(head);
      cleanup(head);
    
      exit(0);
    }
    $ gcc -Wall -Wextra book.c -o book
    $ ./book
    ***** ABC Booksellers *****
    Please select your choice:
    1.Enqueue / Append
    2.Dequeue / Serve
    3.Display
    4.Exit
    Enter your choice : 1
    How many books do you want to enter? 3
    Note: Please refrain from using spaces
    Instead of Spaces please use a underscore.. ^_^
    ***** Enter Book Details *****
    Enter Book ID: 34
    Enter Book TITLE: C_Programming
    Enter Book AUTHOR: someone
    Enter Book Price: 19.99
    Thank you, Book has been successfully appended
    Note: Please refrain from using spaces
    Instead of Spaces please use a underscore.. ^_^
    ***** Enter Book Details *****
    Enter Book ID: 12
    Enter Book TITLE: APUE
    Enter Book AUTHOR: Stevens
    Enter Book Price: 79.99
    Thank you, Book has been successfully appended
    Note: Please refrain from using spaces
    Instead of Spaces please use a underscore.. ^_^
    ***** Enter Book Details *****
    Enter Book ID: 15
    Enter Book TITLE: Unix_Faq
    Enter Book AUTHOR: Andrew
    Enter Book Price: 10.00
    Thank you, Book has been successfully appended
    34
    19.99
    C_Programming
    someone
    -------------------
    12
    79.99
    APUE
    Stevens
    -------------------
    15
    10.00
    Unix_Faq
    Andrew
    -------------------
    $
    Last edited by Overworked_PhD; 04-11-2010 at 05:48 PM.

Popular pages Recent additions subscribe to a feed