Thread: Linear list

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    67

    Linear list

    I must create a program which sorts linear list. The list must be implemented as a pointer list, which holds the strings. Sorting must be ascending (in alphabetic order).

    Code:
     #include<stdio.h>
    struct list{
    char string[100]; //the value part of the list
    struct list *p_next; //a pointer to the next element of the list
    };
    int main() {
    struct list *p_start; //a pointer which points at the start of the list
    p_start=add(p_start,"SomeString1");
    p_start=add(p_start,"Blahhhh");
    p_start=add(p_start,"Life sucks!");
    p_start=add(p_start,"Programming can be hard!");
    p_start=add(p_start,"Nothing to say here");
    p_start=add(p_start,"SomeStsd");
    p_start=add(p_start,"sdsdsdsd");
    return 0;
    }
    
    void print_list(struct list *p_start){ //a method to printout the list
    
     while(p_start!=NULL){
      printf("%s\n",p_start->string);
      p_start=p_start->p_next;
        }
    }
    
    
    
    struct list *add(struct list *p_start,char added_value[])
     {
      int *q;
       q=(struct list*)malloc(sizeof(struct list));//must create a pointer on that new element of the list
        q->value=added_value;
        q->p_next=p_start;
        p_start=q;
         return(q);
      }
    Now in the above code I tried to just make some list of strings without sorting them, and each time I add a string I add it at the beginning. I get a few errors. Any help would be great. And suggestions how to sort the list too.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    52
    Hey there, try taking a look here..

    http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    - Daniel Wallace

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM