Thread: Linked List

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    Linked List

    Hi, i'm having problems implementing a linked list . i want to create a list and then reverse the elements in a list, so for example i want to input Hello World. and the program output .dlroW olleH .here is my code so far:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    typedef struct linked_list {
    char d;
    struct linked_list *next;
    } Element;

    Element *insert(char h, Element **t){

    Element *y = calloc( 1, sizeof(Element));
    y->d = h;
    y->next = *t;
    *t = y;
    return y;
    }

    int main(void)
    {
    Element *list = NULL;
    char a;
    printf("please type in sentence\n");

    while((a = getchar()) != '.'){
    list=insert (a, list);

    }
    for(; list ;list=list->next);
    printf("%s\n", list);
    }


    apparently i am passing incopatible pointer types to function insert in the main function, however i cant see what i'm doing wrong. any help is greatly appreciated.

  2. #2

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    sorry i'm new to this board here's my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct linked_list {
    char d; 
    struct linked_list *next;
    } Element;
    
    Element *insert(char h, Element **t){ 
    
    Element *y = calloc( 1, sizeof(Element)); 
    y->d = h; 
    y->next = *t; 
    *t = y;
    return y; 
    } 
    
    int main(void)
    { 
    Element *list = NULL; 
    char a; 
    printf("please type in sentence\n"); 
    
    while((a = getchar()) != '.'){ 
    list=insert (a, list); 
    
    } 
    for(; list ;list=list->next); 
    printf("%s\n", list); 
    }

  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
    > Element **t){
    Remove a *

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, look at your insert function. It expects a pointer to a pointer, but you're just passing it a pointer. (Doesn't anyone pay attention to their compiler warnings?) Furthermore, there's no point in assigning the return value to the pointer, since you're updating (or are apparently trying to at least) the pointer via the argument passed.

    Fix the above and see how far you get.

    [edit] Curses, foiled again. [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    sorry i understand what your saying but however i try and fix it ,it still doesnt work. salem: it still doesnt work when i do what you say.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    sorry i'm a beginner programmer and i know that these questions have probably been asked a million times before but i really need help.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you're returning the new list, so there's no need to pass a pointer to your list in the first place
    Code:
    Element *insert(char h, Element *t ) { 
      Element *y = calloc( 1, sizeof(Element)); 
      y->d = h; 
      y->next = t;
      return y; 
    }
    Oh, and create another function to print your list - you destroy the list by printing it at present

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    could you suggest another print function to reverse the elements in my list? i know my code is dodgy but i just want to get it working. can anyone suggest better code to perform what i'm trying to do.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A print function should do just that, print the list. Nothing more, nothing less.
    It's usually a good idea to make your functions to a single task, if possible.
    a) Print a list.
    b) Add a node to a list.
    c) Delete a node.
    d) Delete the whole list.
    e) Whatever.

    For one, it's easier to debug a single function at a time than it is if they're all in one function. It's also easier to read and follow.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    thanks very much, ive sorted out my problem now

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by recluse
    could you suggest another print function to reverse the elements in my list? i know my code is dodgy but i just want to get it working. can anyone suggest better code to perform what i'm trying to do.
    if you want to print the list backwards,you can use recursions
    Code:
     #include<stdio.h>
     #include<conio.h>
    struct node
    {
     int num;
     struct node *next;
    };
    void print(struct node * first)
    {
     if(first->next==NULL)
      {
       printf("%d",first->num);
       return;
      }
     print(first->next);
     printf("%d",first->num);
    }
    int main()
    {
     struct node *head,*current;
     int first=0;
     char in='y';
     clrscr();
     printf("do u wanna enter a no.");
      scanf("%c",&in);
     while(in=='y')
     {
    
       if(first==0)
       {
        head=malloc(sizeof(struct node));
        current=head;
        printf("enter the no.");
        scanf("%d",&head->num);
        first=first+1;
       }
       else
       {
        current->next=malloc(sizeof(struct node));
        printf("enter the no.");
        scanf("%d",&current->next->num);
        current=current->next;
       }
    
      printf("do u wanna enter a no.");
      scanf("%c",&in);
     }
      current->next=NULL;
      print(head);
      getch();
      return 0;
    }
    i think this should work if you want to take in numbers and then print them backwards using the linked list.
    be sure to clear the input buffer before each scanf()
    ciao

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When posting sample code, try at least following the standard as much as possible. Many people don't have old compilers, and as such, don't have conio.h. It isn't needed.

    While I'm aware you were trying to help, giving people the full answer is generally frowned upon.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Quote Originally Posted by quzah
    When posting sample code, try at least following the standard as much as possible. Many people don't have old compilers, and as such, don't have conio.h. It isn't needed.

    While I'm aware you were trying to help, giving people the full answer is generally frowned upon.

    Quzah.
    sorry bout that quzah,i wanted to tell him to use recursions if he wanted to print the list backwards,but then he had said that he is new to programming,and then he might not get that recursions thing.thats y i posted the code.even i dont like doing everything for everyone.programmers should think it out on their own rite??

  15. #15
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    i do know what recursion is ..... that code just looks a bit 'messy' , is that really the easiest way to reverse elements in a list? why cant c programming books have more usefull examples...

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