Thread: regarding linked list

  1. #1
    Registered User
    Join Date
    Aug 2006

    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
    #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;
    printf("enter records \n");
    record = (struct dllist *)malloc(sizeof(struct dllist));
    scanf("%s", &description);
    scanf("%d", &quantity);
    scanf("%f", &price);
    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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    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.

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

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, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM