Like Tree1Likes
  • 1 Post By iMalc

Linked List with CHAR

This is a discussion on Linked List with CHAR within the C Programming forums, part of the General Programming Boards category; Im sorry about the original post, that was my first. So, this is a program to be used in a ...

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

    Linked List with CHAR

    Im sorry about the original post, that was my first.
    So, this is a program to be used in a bank as a line organizer.
    Everytime i add a person, at the time to show the queue, it displays eveyone with the last person added name. how to fix it?
    And on deleting person, how to show the name (like: "the person XXX was attended").


    The code is working but I need to fix those:
    Set the right name and show it when deleted.
    Thank you everyone!!


    Code below:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct node{
    int priority;
    int info;
    struct node *link;
    }*front=NULL;
    
    
    void insert(char person[20], int person_priority);
    int del();
    void display();
    int isEmpty();
    
    
    main(){
    int choice,person_priority;
    char person[20];
    
    
    while(1){
    printf("1.Insert Person\n");
    printf("2.Attend Client\n");
    printf("3.Show Queue\n");
    printf("4.Exit\n");
    printf("Type choice : ");
    scanf("%d", &choice);
    
    
    switch(choice){
                   
    case 1:
    printf("Type persons name:");
    scanf("%s", &person);
    printf("Its Priority:\n1 Pregnant\n2 Older\n3 Standard: ");
    scanf("%d",&person_priority);
    insert(person, person_priority);
    system("cls");
    break;
    case 2:
         system("cls");
    printf("Person was attended",del());
    break;
    case 3:
         system("cls");
    display();
    
    
    break;
    case 4:
    exit(1);
    default :
    printf("Invalid Choice\n");
    }/*end of switch*/
    }/*end of while*/
    }/*end of main()*/
    
    
    void insert(char person[20],int person_priority){
    struct node *tmp,*p;
    
    
    tmp=(struct node *)malloc(sizeof(struct node));
    
    
    if(tmp==NULL){
    printf("No Memory available\n");
    return;
    }
    
    
    tmp->info=person;
    tmp->priority=person_priority;
    /*Starting list*/
    if( isEmpty() || person_priority < front->priority ){
    tmp->link=front;
    front=tmp;
    }
    
    
    else{
    p = front;
    while( p->link!=NULL && p->link->priority<=person_priority )
    p=p->link;
    tmp->link=p->link;
    p->link=tmp;
    }
    }/*end of insere()*/
    
    
    int del(){
    struct node *tmp;
    int person;
    if( isEmpty() )
    {
        
    printf("Empty Queue\n");
    exit(1);
    }
    
    
    else{
    tmp=front;
    person=tmp->info;
    front=front->link;
    free(tmp);
    }
    return person;
    }/*end of del()*/
    
    
    int isEmpty(){
    if( front == NULL )
    return 1;
    else
    return 0;
    
    
    }/*end of emtpy verification (isEmpty())*/
    
    
    void display(){
    struct node *ptr;
    ptr=front;
    if( isEmpty() )
    printf("Empty Queu\n");
    else{    printf("Line :\n");
    printf("Priority       Name\n");
    while(ptr!=NULL){
    printf("%5d        %5s\n",ptr->priority,ptr->info);
    ptr=ptr->link;
    }
    printf("\n\n\n");
    }
    }/*end of display() */

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    And one forum
    Leaving this one open, as the code is marginally more readable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Code:
    }/*end of display() */
    No no no, that's not how you make the end of a function obvious.
    What you are supposed to do is to use correct indentation.
    I'm not helping you out with such horribly formatted code.
    laserlight likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Here, this is what code is supposed to look like with indentation.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct node {
      int priority;
      int info;
      struct node *link;
    } *front = NULL;
    
    void insert(char person[20], int person_priority);
    int del();
    void display();
    int isEmpty();
    
    int main() //!! fixed
    {
      int choice, person_priority;
      char person[20];
    
      while (1) {
        printf("1.Insert Person\n");
        printf("2.Attend Client\n");
        printf("3.Show Queue\n");
        printf("4.Exit\n");
        printf("Type choice : ");
        scanf("%d", &choice);
    
        switch (choice) {
        case 1:
          printf("Type persons name:");
          scanf("%s", &person);
          printf("Its Priority:\n1 Pregnant\n2 Older\n3 Standard: ");
          scanf("%d", &person_priority);
          insert(person, person_priority);
          system("cls");
          break;
        case 2:
          system("cls");
          printf("Person was attended", del());
          break;
        case 3:
          system("cls");
          display();
          break;
        case 4:
          exit(1);
        default:
          printf("Invalid Choice\n");
        }                           /*end of switch */
      }                             /*end of while */
      return 0;   //!! fixed
    }                               /*end of main() */
    
    
    void insert(char person[20], int person_priority)
    {
      struct node *tmp, *p;
    
      tmp = (struct node *) malloc(sizeof(struct node));
    
      if (tmp == NULL) {
        printf("No Memory available\n");
        return;
      }
    
      tmp->info = person;
      tmp->priority = person_priority;
    /*Starting list*/
      if (isEmpty() || person_priority < front->priority) {
        tmp->link = front;
        front = tmp;
      }
      else {
        p = front;
        while (p->link != NULL && p->link->priority <= person_priority)
          p = p->link;
        tmp->link = p->link;
        p->link = tmp;
      }
    }                               /*end of insere() */
    
    int del()
    {
      struct node *tmp;
      int person;
    
      if (isEmpty()) {
        printf("Empty Queue\n");
        exit(1);
      }
      else {
        tmp = front;
        person = tmp->info;
        front = front->link;
        free(tmp);
      }
    
      return person;
    }                               /*end of del() */
    
    int isEmpty()
    {
      if (front == NULL)
        return 1;
      else
        return 0;
    }                               /*end of emtpy verification (isEmpty()) */
    
    void display()
    {
      struct node *ptr;
      ptr = front;
      if (isEmpty())
        printf("Empty Queu\n");
      else {
        printf("Line :\n");
        printf("Priority       Name\n");
        while (ptr != NULL) {
          printf("%5d        %5s\n", ptr->priority, ptr->info);
          ptr = ptr->link;
        }
        printf("\n\n\n");
      }
    }                               /*end of display() */
    Next, get a better compiler, turn up the warning level, and pay attention to what it says.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:31:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
    bar.c:39:7: warning: too many arguments for format [-Wformat-extra-args]
    bar.c: In function ‘insert’:
    bar.c:66:13: warning: assignment makes integer from pointer without a cast [enabled by default]
    bar.c: In function ‘display’:
    bar.c:119:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
    One problem seems to be that you only have "int person" in your struct, yet everywhere else in your code, person is a char array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,340
    You need to fix the info field so it's an array of characters. I also prefer to keep the link pointers at the start of structures or classes, since you can use generic code for handling linked list by casting to a generic struct or class that only has the link pointers (or use inhertiance from a class with only the link pointer(s)).

    Code:
    struct node {
        struct node *link; 
        int priority; 
        char info[20]; 
    } *front = NULL;
    


    or perhaps use typedefs:

    Code:
    typedef struct node {
        struct node *link; 
        int priority; 
        char info[20]; 
    }node_t;
    node_t *front = NULL;
    


    Last edited by rcgldr; 04-23-2013 at 06:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C char string help in linked list and queues program
    By kdj1881 in forum C Programming
    Replies: 3
    Last Post: 04-13-2011, 06:32 PM
  2. Fill a linked list char by char
    By w2look in forum C Programming
    Replies: 6
    Last Post: 03-26-2009, 04:07 PM
  3. linked list with char array HELP!
    By littlepiggy in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2005, 03:45 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 12:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21