regarding linked list

This is a discussion on regarding linked list within the C Programming forums, part of the General Programming Boards category; iam storing the values in linked list in a sorted form how can i do this i have to sorted ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    regarding linked list

    iam storing the values in linked list in a sorted form
    how can i do this i have to sorted out with item_num key
    my code is
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct dllist {
    
     int item_num;
     char description[256];
     int quantity;
     float price;
     struct dllist *next;
     struct dllist *prev;
    };
    struct dllist *head, *tail;
    void main()
    {
    struct dllist *record;
    int item_no,quantity;
    float price;
    char description[256];
    int i;
    for(i=0;i<=4;i++)
    {
    printf("enter records \n");
    record = (struct dllist *)malloc(sizeof(struct dllist));
    scanf("%d",&item_no);
    scanf("%s", &description);
    scanf("%d", &quantity);
    scanf("%f", &price);
    record->item_num=item_no;
    strcpy(record->description,description);
    record->quantity=quantity;
    record->price=price;
    append_node(record);
    }
    for(record = head; record != NULL; record = record->next)
    printf("%d \n",record->item_num);
    }
    
    void append_node(struct dllist *lnode) {
     if(head == NULL) {
      head = lnode;
      lnode->prev = NULL;
     } else {
      tail->next = lnode;
      lnode->prev = tail;
     }
    
     tail = lnode;
     lnode->next = NULL;
    }
    thank u ,inadvance
    sree

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The simplest thing would be to use an "insertion sort" method inside your append_node() which should in this case be renamed to insert_node().

    Insertion sort is based on finding the node that is greater (or lesser - depending on your sort order) and inserting the current item before the found node.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    Haven't you learnt anything in your 77 posts to date?

    - void main - wrong
    - casting malloc - wrong
    - non-existent indentation
    - incorrect use of pointers in scanf with &#37;s

    To answer your other question, you need an insert() function, which walks down the list comparing each item_num with the one you want to add. When you get to the point where the 'prev' node is less and the 'next' node is greater-equal, that is where you insert the new node. Plus detecting the various edge conditions such as insert into an empty list, insert at head of list etc.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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