Linked Lists In C

Hello,
How would I go about making this program add new Nodes to the list. It seems it is only adding each entry to one node. I put a while loop to re - input data, but the new data just erases the old data. Does anyone know how I could achieve this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
struct Node
{
   char first [15], number[11];
   struct Node *nextPtr;
} ;
struct Node *Head = 0;
struct Node *tailPtr = 0;
void insertEntry (char first [], char num []);
void printList () ;
struct Node *findNode(char first []);
void deleteNode(char first []);

int main ()
{
    char first [15], numb[11], decide;
    do{
    printf("Enter Name\n");
    scanf("%s", &first);
    getchar();
    printf("Enter Number\n");
    scanf("%s", &numb);
    getchar();
    insertEntry (first, numb);
    printf("Again?\n");
    scanf("%c", &decide);
    getchar ();
    }while(decide == 'y' || decide == 'Y');
    printList () ;
    system("pause");
}

void insertEntry (char entry [], char num [])
{
     struct Node *temp;
     temp = (struct Node *) malloc (sizeof(struct Node));
     int count;
     if(temp != 0)
     {
        for(count = 0; count < 15; count ++)
        {
           temp->first [ count ] = entry [ count ];
           temp->number [ count] = num [ count ];
        }
        if(Head == 0)
          {
          Head = temp;
          Head->nextPtr = 0;
          }
         else
         {
             temp->nextPtr = 0;
             Head = temp;
         }   
             
     }
     
     
}


void printList ()
{
     struct Node *currPtr = Head;
     if (currPtr == 0)
     {
        printf("The List Is Empty\n");
     }
else
{
    while(currPtr != 0)
    {
       printf("Name: %s\n", currPtr->first);
       printf("Number: %s\n", currPtr->number);
       currPtr = currPtr -> nextPtr;           
    }
    
}
        
}
That is because you don't store your data.
You just make a copy of your structure in another instance of the same structure but too bad you can only store one structure. You need to store your structures in a container like vector or list or map (you choose) and when you finish adding new elements show all the data from that container not from the structure like you are doing now cos that structure has just the last data from input.
Last edited on
In insert(), you shouldn't copy the elements with:
1
2
3
4
5
        for(count = 0; count < 15; count ++)
        {
           temp->first [ count ] = entry [ count ];
           temp->number [ count] = num [ count ];
        }


They are char arrays, but you should think of them as null terminated strings with a maximum length. In this case, number is only 11 bytes long, but you're copying 15 bytes. We call this a buffer overrun; it's a serious error.

Instead use:
1
2
        strncpy(temp->first, entry, sizeof(temp->first));
        strncpy(temp->number, num, sizeof(temp->nummber));


You will need to include <string.h>

You're not inserting the new node into the list correctly. You need to add the new element at the end. You've declared a pointer to the last element, tailPtr, but you don't use it.

The first element should be done as:
1
2
3
4
5
6
        if(Head == 0)
          {
          Head = temp;
          Head->nextPtr = 0;
          tailPtr = temp;
          }


Then, when the list has more than one element, you append to the tail (not the head) and update tailPtr.
Last edited on
Thank You VERY MUCH kbw for strncpy, that helps alot! But im still confused about updating the tail pointer. Sorry for all the questions, but would you mind showing me an example perhaps on how to update the tail pointer, so the data is inserted after the head. Then print it in that order?

That would be awesome!

Thanks again for all the help!!!
Topic archived. No new replies allowed.