inserting to a linked list?

So I'm trying out this code that was given to me by a friend, and I can't seem to get the data to be inserted to my linked list. retrieveData() was able to retrieve the data from the file but everything goes awry when it's trying to insert the data into the linked list.

I'm not all to familiar with using pointer operations, so can someone come help explain how the insertData() operations are supposed to work? I mean, the algorithm is supposed to simulate a stack operation, but since I can't seem to understand the concept of pointer operations. Well, not yet, I only know the basic ones like *strData = charData[] but not the ones using -> operators...

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
typedef struct List {
	int x;
	struct List *next;
} LIST;

LIST *L;

void retrieveData(){
	int y;
	FILE *fp;
	makenull();
	
	fp = fopen("/data.dbf", "r+");
	
	while(!feof(fp)) {
		printf("wtf");
		fscanf(fp,"%d\n",&y);
		insertData(y);
	}
	
	fclose(fp);
}

void insertData(int y) { 
	LIST *q, *p, *new;
	q = p = L;
	new = (LIST *)malloc(sizeof(LIST));
	new->x = y;

	while (p != NULL) {
		q = p;
		p = p->next;

		if (L == p) {
			L = new;
		} else {
			q->next = new;
		}
		new->next = p;
	}
}
Last edited on
What is that insert function supposed to be doing? It looks like it is going to be trying to insert 'new' repeatedly into the list every loop.

Also, you may not want to use 'new' as an identifier since it is reserved in C++. It won't be a problem compiling C, of course.
Well, it's supposed to simulate a stack implementation, like if you had a list of numbers (i.e.: 2,5,9), once you insert all the numbers into the linked list, output should be like this:

9
5
2

But as I've said, I can't understand how the insertData() part of the program works, what with all the lists that was written there and the pointer operations (which I haven't seen, at least from the references that I've read so far). This was the original code. I just added brackets and indention on several parts of the code:

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
#include <stdio.h>
#include <stdlib.h>

typedef struct list {
	int x;
	struct list *next;
	} LIST;

LIST *L;

void retrieve(){
int y;
FILE *fp;
makenull();
fp=fopen("mit594.dbf","r+");
while(!feof(fp)){
  fscanf(fp,"%d\n",&y);
  insert(y);
  }
fclose(fp);
}

void insert(int y)
{ 
	LIST *q, *p, *new;
	q = p = L;
	new = (LIST *)malloc(sizeof(LIST));
	new->x = y;
	while (P!=NULL)
	{
		q=p;
		p=p->next;
		
		if (L==P)
			L = new;
		else
			q->next = new;
		new->next = P;
		
	}
}
A linked list insert should not need to repeatedly insert new like that; if you're inserting at the end, simply seek to the end of the list (keep going through until the current node->next is NULL, then set that node->next to the node you are inserting.
To be honest, I looked up some sample code and it seems to be much more simple than this. I'm gonna go study it and follow up with questions, if you don't mind.
Topic archived. No new replies allowed.