Thread: Insert node in a linked list.

  1. #1
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32

    Insert node in a linked list.

    Hi everybody!
    I asked yesterday something about my project: TOPIC
    and now that I have almost finished I have problems with the last function that inserts a new line after the current. Here there is the function:
    Code:
    void InsertAfter(struct list *buffer, char *newline)
       struct node *p=MALLOC(sizeof(*p));                   //line to be inserted
            p->line=newline;
            struct node *n=buffer->head;                           //current line
            int i;
            for(i=0;i<(buffer->currentpos)-1;++i){             //to fint the current line
                    n=n->next;
            }
    
            p->prev=n;
            p->next=n->next;
            n->next->prev=p;
            n->next=p;
            ++buffer->size;
    }
    here the structs:
    Code:
    struct node {
            char         *line;
            struct node  *prev;
            struct node  *next;
    };
    
    struct list {
            struct node  *head;
            struct node  *tail;
            int           size;
            int     currentpos;
            char      *current;
    };
    It gets compiled, and runs perfect, but when I have inserted a new line, and whant to print the file via the function WriteFile which prints the buffer->head the buffer->head-> next etc, I get segmentation fault. I think the pointers do not align correctly, but I don't know why.
    Can somebody help me, please?
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > p->line=newline;
    Well my previous post explained why not to do this, how to do it properly and yet here we are.
    You're just copying a pointer to someone elses memory.

    > struct node *n=buffer->head;
    Only C99 (and some extensions to various compilers) allow mixing declarations and statements.

    > p->prev=n;
    Yes, this is the thing to do - but it only works in the middle of the list.
    Both 'head' and 'tail' are special cases which can change, so if you insert a node at either the head or the tail, these pointers need to be updated as well.

    > ++buffer->size;
    Check your operator precedence, I think you'll find this increments buffer, then dereferences it.
    buffer->size++;
    or
    ++(buffer->size);
    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.

  3. #3
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Code:
    void InsertAfter(struct list *buffer, char *newline)
    {
            //if currentpos is last line: I have to add....
            struct node *makeNode(char *newline);
            struct node *p=MALLOC(sizeof(*p));
            struct node *n;
            n=buffer->head;
            int i;
            for(i=0;i<(buffer->currentpos)-1;++i){
                    n=n->next;
            }
            printf("%s",n->line);
    
            p->prev=n;
            p->next=n->next;
            n->next->prev=p;
            n->next=p;
            ++(buffer->size);
    }
    What do you think now.
    Now its a lot better, no segmentation faults, but the text:
    Code:
    The Department of Applied Mathematics is the latest academic unit of the
    School of Sciences and Technology of the University of Crete. It began
    operating in the academic year 1999-2000. Its mission is to provide the
    appropriate educational and research infrastructure for the study of problems
    arising in technological fields, natural sciences, business and economics.
    
    The development of Mathematics has been historically linked with the
    advancement of natural sciences. The ever increasing rate of
    development in technology and in scientific fields such as Biology, Chemistry,
    Materials, Medicine, Business and Economics have elevated the status of
    Applied Mathematics to a new level. In our modern era of specialization,
    the interplay and interactions between diverse scientific disciplines suggest
    the study of Applied Mathematics as an efficient way for preparing scientists
    capable to deal with and adapt to a rapidly changing reality.
    
    The Department of Applied Mathematics aims to train scientists who will not
    only contribute to Mathematics itself but will also have developed modelling
    and computational skills and will be able to compete favourably in the job
    market for highly skilled individuals. The graduates of the Department of
    Applied Mathematics will have the training, knowledge and the problem solving
    ability that will make them especially useful in a continuously evolving job
    market. In addition, their broad scientific background will enable them to
    continue post-graduate work in a wide field of applied sciences.
    with a newline added before the second one, looks like this:
    Code:
    ==> append : to filename to view the  data source
    The Department of Applied Mathematics is the latest academic unit of the
    School of Sciences and Technology of the University of Crete. It began
    @operating in the academic year 1999-2000. Its mission is to provide the
    appropriate educational and research infrastructure for the study of problems
    arising in technological fields, natural sciences, business and economics.
    The development of Mathematics has been historically linked with the
    advancement of natural sciences. The ever increasing rate of
    development in technology and in scientific fields such as Biology, Chemistry,
    Materials, Medicine, Business and Economics have elevated the status of
    Applied Mathematics to a new level. In our modern era of specialization,
    the interplay and interactions between diverse scientific disciplines suggest
    the study of Applied Mathematics as an efficient way for preparing scientists
    capable to deal with and adapt to a rapidly changing reality.
    The Department of Applied Mathematics aims to train scientists who will not
    only contribute to Mathematics itself but will also have developed modelling
    and computational skills and will be able to compete favourably in the job
    market for highly skilled individuals. The graduates of the Department of
    Applied Mathematics will have the training, knowledge and the problem solving
    ability that will make them especially useful in a continuously evolving job
    market. In addition, their broad scientific background will enable them to
    continue post-graduate work in a wide field of applied sciences.
    What isn't written properly???
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM