Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,360 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,254 people online right now. Registration is fast and FREE... Join Now!




Inserting a new line in a linked list [C]

 
Reply to this topicStart new topic

Inserting a new line in a linked list [C]

azuresonata
post 30 Aug, 2008 - 06:05 PM
Post #1


New D.I.C Head

*
Joined: 22 Jul, 2008
Posts: 9

I made this code which asks the user to enter a number of lines and then asks the user, after pressing enter, which line does he want to enter a line. Then prompts the user to type the line he wants to enter. I'm currently have trouble trying to insert the string the user typed to the list. Can someone help me? Here's a copy of what I was able to do:


CODE


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

typedef struct line {
    char* line;
    struct line *prev, *next;
} LINE;

const int LINE_MAX = 80;
char *addlines(LINE** list, unsigned int line_no);
char *pline = NULL;
LINE *list = NULL; // this will serve as the head of a list.
LINE *endList = NULL; // this points to the last element of a list.
int len=0;
int count=0; //serves as the counter for the number of lines.
char mystring[500]; //serves as the new string that will be inserted.

int main() {
    printf("Please enter any number of lines. End with a ^D/^Z:\n");

    char line[LINE_MAX];

    while(fgets(line, LINE_MAX, stdin)){
        len = strlen(line);

        count++;
        line[len-1] = '\0';
        pline = (char *)malloc(sizeof(char) * (len + 1));
        strncpy(pline, line, len);
        pline[len] = '\0';

        LINE *temp;
        // test first if your list is non empty
        if(list != NULL) {
            for(temp = list; temp->next != NULL; temp = temp->next)
            ;
            // at this point, you're at the last element
            temp->next = (LINE*)malloc(sizeof(LINE));
            temp->next->line = pline;
            temp->next->prev = temp;
            temp->next->next = NULL;
            endList = temp->next;


        }

        else {// our list is empty
        // temp at the last element of the list.
            temp = (LINE *)malloc(sizeof(LINE));
            temp->line = pline;
            temp->prev = temp->next = NULL;
            list = temp;
            endList = temp;
        }

    }

    LINE* p;
       int line_no;


        printf("\nEnter the line number where you want to insert a new line:"); //asks the user what line number he wants the string he wants to be printed.
        scanf("%d", &line_no);

    if(line_no > count) {
        printf("\nINVALID.");
    }

    else {
        printf("\nYou want to add a new line on: %d\n\n\n",line_no);
        printf("Please enter the line you want to insert:\n\n");
        fscanf(stdin, "%s", mystring);
        fgets(mystring, 500, stdin);


        addlines (list, line_no);
    }

    return 0;

}

char *addlines (LINE** list, unsigned int line_no) {
    LINE *q, *temp;

    printf("\nYour new lines\n\n");

    for(q = list; q!= NULL; q=q->next) {
        temp = (LINE *)malloc(sizeof(LINE));
            temp->line = pline;
            temp->prev = temp->next = NULL;
            list = temp;
            q->line;



        printf("%s\n", q->line);
    }
}


This part here is the problem:

CODE


for(q = list; q!= NULL; q=q->next) {
        temp = (LINE *)malloc(sizeof(LINE));
            temp->line = pline;
            temp->prev = temp->next = NULL;
            list = temp;
            q->line;
}


I'm actually at the final step of the program which is inserting the line the user entered in the line number the user specified earlier and printing it. As of now, what this code does is to reprint the lines entered by the user during the first part of the program.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 30 Aug, 2008 - 10:25 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


You have lots of issues with this code that you need to solve first before you can worry about before fixing the logic...

1) The char pointer in LINE cannot be called "line" since this is seen as an attempt to make a constructor since your struct is also named "line". So change the name of the char pointer in your struct to another name and then everywhere you reference this member pointer in your code.

2) Your addlines method returns no char pointer.

3) You also pass in a char pointer, not a pointer to a pointer (double asterisk)

4) LINE *p is never used. Either use it, comment it out, or remove it.

Fix these problems and at least get the program to compile and then you can start working on getting down to what the logic says.

Good luck. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 04:49AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month