Thread: OMG I'm Going Insane (insertion!)

  1. #1
    Registered User
    Join Date
    Apr 2003

    Angry OMG I'm Going Insane (insertion!)

    Ok, I've gone through my notes, books, tutorials, etc. I'm still having a problem with this! Can anyone help me!?!?! Here's what I have to do:

    Write an insertion function that inserts an element in the nth position in a list, where 0 means the element is placed at the head of the list. If n is larger than the length of the list, insert the element at the tail of the list. The type definition DATA for elements in the list is defined below (code given to me)
    typedef struct {
    char name[10];
    int age;
    int weight;
    } DATA;
    Write a complete program testing your function. You define a sample list. An element to be inserted to the list and n you should read from the standard input.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    It sounds like you're talking about a simple linked list insertion. It can be done like this:
    #include <stdio.h>
    #include <stdlib.h>
    typedef int DATA;
    typedef struct List {
        DATA data;
        struct List* next;
    } List;
    List* insert(List* list, DATA item, int pos)
        if (list == NULL)
            list = malloc(sizeof (*list));
            list->data = item;
            list->next = NULL;
        else if (pos == 0)
            List* front = malloc(sizeof (*list));
            front->data = item;
            front->next = list;
            list = front;
            List* iter = list;
            List* save;
            while (iter->next != NULL && --pos > 0)
                iter = iter->next;
            save = iter->next;
            iter->next = malloc(sizeof (*iter));
            iter->next->data = item;
            iter->next->next = save;
        return list;
    int main(void)
        List* list = NULL;
        List* forward;
        /* After insertions: 3->5->6->4->1->2-> */
        list = insert(list, 1, 0);
        list = insert(list, 2, 5);
        list = insert(list, 3, 0);
        list = insert(list, 4, 1);
        list = insert(list, 5, 1);
        list = insert(list, 6, 2);
        while (list)
            forward = list->next;
            printf("%d->", list->data);
            list = forward;
    Note that I didn't do any error checking, you should always check the return value of malloc. But doing so means you also have to devise an error recovery strategy that makes the code longer and hides the concept.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Ok, I understand that a little but what I don't get is where to insert
    typedef struct{
    char name[10];
    int age;
    int weight;

    Should I insert it near typedef struct List{ ?
    Also, am I supposed to make up a text file called DATA in which I put in someone's name, age, and weight? I guess I'm a little confused about what the problem is asking. If someone could point me in the right direction that would help out alot. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
Website Security Test