Thread: Could Someone help me? (Linked list)

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

    Could Someone help me? (Linked list)

    Hey guys, With this linked list, instead a number, I want to insert a name in the green line highlighted (CHAR name[30]) but it comes a lot of bug, may someone help me? This is the original code. Please tell me where and ehat to insert in witch lines. Sorry about my english and C knowledge.
    Thank you everyone!!
    (Im sorry if Im not using some law from this board).
    Code below:

    Code:
     
    #include<stdio.h>
    Code:
    #include<stdlib.h>
    
    struct node
    {
    int priority;
    int info;
    struct node *link;
    }*front=NULL;
    
    void insert(int item, int item_priority);
    int del();
    void display();
    int isEmpty();
    
    main()
    {
    int choice,item,item_priority;
    while(1)
    {
    printf(“1.Insert\n”);
    printf(“2.Delete\n”);
    printf(“3.Display\n”);
    printf(“4.Quit\n”);
    printf(“Enter your choice : “);
    scanf(“%d”, &choice);
    
    switch(choice)
    {
    case 1:
    printf(“Input the item to be added in the queue : “);
    scanf(“%d”,&item);
    printf(“Enter its priority : “);
    scanf(“%d”,&item_priority);
    insert(item, item_priority);
    break;
    case 2:
    printf(“Deleted item is %d\n”,del());
    break;
    case 3:
    display();
    break;
    case 4:
    exit(1);
    default :
    printf(“Wrong choice\n”);
    }/*End of switch*/
    }/*End of while*/
    }/*End of main()*/
    
    void insert(int item,int item_priority)
    {
    struct node *tmp,*p;
    
    tmp=(struct node *)malloc(sizeof(struct node));
    if(tmp==NULL)
    {
    printf(“Memory not available\n”);
    return;
    }
    tmp->info=item;
    tmp->priority=item_priority;
    /*Queue is empty or item to be added has priority more than first element*/
    if( isEmpty() || item_priority < front->priority )
    {
    tmp->link=front;
    front=tmp;
    }
    else
    {
    p = front;
    while( p->link!=NULL && p->link->priority<=item_priority )
    p=p->link;
    tmp->link=p->link;
    p->link=tmp;
    }
    }/*End of insert()*/
    
    int del()
    {
    struct node *tmp;
    int item;
    if( isEmpty() )
    {
    printf(“Queue Underflow\n”);
    exit(1);
    }
    else
    {
    tmp=front;
    item=tmp->info;
    front=front->link;
    free(tmp);
    }
    return item;
    }/*End of del()*/
    
    int isEmpty()
    {
    if( front == NULL )
    return 1;
    else
    return 0;
    
    }/*End of isEmpty()*/
    
    void display()
    {
    struct node *ptr;
    ptr=front;
    if( isEmpty() )
    printf(“Queue is empty\n”);
    else
    { printf(“Queue is :\n”);
    printf(“Priority Item\n”);
    while(ptr!=NULL)
    {
    printf(“%5d %5d\n”,ptr->priority,ptr->info);
    ptr=ptr->link;
    }
    }
    }/*End of display() */
    
    

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You need to indent your code properly. Then probably more people here are willing to help you.

    You should also always copy the warnings/errors from your compiler (don't retype them!).

    Basically you need to add a new member to your node structure if you want to store a character array and then adapt all functions accordingly.

    Bye, Andreas

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Paste your code as plain text and make sure it preserves formatting.
    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"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread