Thread: Linked List Mayhem

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

    Linked List Mayhem

    Hello I have been working on a linked list program for homework.
    I have to make a list of 25 random numbers in ORDERED LINKED LIST here is my code
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    void inn(int);
    struct node {
      int val;
      struct node * next;
    };
    
    typedef struct node item;
    
    void main() {
      item * cptr, * hptr;
      int i,num;
    
      hptr = NULL;
      srand(time(NULL));
        for(i=0;i<25;i++) {
        num= rand() % 99 + 1;
        cptr = (item *)malloc(sizeof(item));
        cptr->val = num;
        cptr->next  = hptr;
        hptr = cptr;
    
        cptr = hptr;
        }
    
    
     while(cptr) {
        printf("%d\n", cptr->val);
        cptr = cptr->next ;
      }
    }
    This produces a linked list but not in order. This topic seems very difficult or am I learning impaired I cant Imagine doing trees my branches would fall off please help!!!! P.S. Proffessor
    says linked lists are fun.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    int main ( void ). void main is wrong.

    >make a list of 25 random numbers in ORDERED LINKED LIST
    To insert into an ordered linked list you need to find the correct position to insert at. This involves three things:

    1) Check to see if the list is empty.
    2) Check to see if the new value is less than the head. If so, do head insertion like in your code.
    3) Otherwise, walk from the head until you reach the end or find a value greater than the new value. Insert there.
    Code:
    insert_ordered ( data )
    begin
      if list == NULL then
        list = new node ( data )
      else if data < list.data then
        new_node = new node ( data )
        new_node.next = list
        list = new_node
      else
        walk = list
        while walk.next != NULL && data > walk.next.data
          walk = walk.next
        new_node = new node ( data )
        new_node.next = walk.next;
        walk.next = new_node;
      endif
    end
    >Proffessor says linked lists are fun
    They have some interesting properties, and linked lists are a springboard into more powerful data structures.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    I can't follow your code maybe I'm Impaired after all.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I can't follow your code
    Then you need more help than I can give you here. You need someone face to face in real-time to answer your questions and draw you some diagrams. Might I suggest your instructor?
    My best code is written with the delete key.

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. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  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