Deleting beginning of a linked list

I go a txt file:
1 2 3 4 5 6 0
My assignment is to create a linked list containing those numbers, delete the first number and then output the rest into another .txt.
My expected output is: 2 3 4 5 6 0
I wrote some codes:
Header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #ifndef _HEADER_H_
#define _HEADER_H_
#include <iostream>
#include <fstream>
using namespace std;

struct Node
{
	int data;
	Node *next;
};
struct LinkedList
{
	Node *head;
	Node *tail;
};

Node *createNode(int x);
void createList(LinkedList &lst);
void insertTail(LinkedList &lst, int x);
Node *deleteBeg(Node *head);
#endif 

functions definition file:
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
#include "Header.h"
Node *createNode(int x)
{
	Node *p = new Node;
	if (p != NULL)
	{
		p->data = x;
		p->next = NULL;
	}
	return p;
}
void createList(LinkedList &lst)
{
	lst.head = lst.tail = NULL;
}
void insertTail(LinkedList &lst, int x)
{
	Node *p = createNode(x);
	if (p == NULL)
	{
		return;
	}
	if (lst.head == NULL && lst.tail == NULL)
	{
		lst.head = lst.tail = p;
	}
	else
	{
		lst.tail->next = p;
		lst.tail = p;
	}
}

Node *deleteBeg(Node *head)
{
	Node *temp = head;
	head = head->next;
	delete temp;
	return head;
}

main:
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
#include "Header.h"
int main()
{
	ifstream in;
	LinkedList lst;
	createList(lst);
	int x;
	in.open("E:/input.txt");
	if (!in)
	{
		cout << "Error!\n";
	}
	else
	{
		in >> x;
		while (x != 0)
		{
			insertTail(lst, x);
			in >> x;
		}
		in.close();
	}

	ofstream out;
	out.open("E:/output.txt");
	if (!out)
	{
		cout << "Error!\n";
	}
	else
	{
		LinkedList l;
		Node *head = NULL;
		head = deleteBeg(head);
		for (Node *p = l.head; p != NULL; p = p->next)
		{
			out << p->data << " ";
		}
		out.close();
	}

	system("pause");
}

When I compiled, all I get is: Error C4700 uninitialized local variable 'l' used. What is it? Isn't l is a way to get access to the linked list?
Isn't l is a way to get access to the linked list?

Yes (but no).

The l is a LinkedList object that you do create on line 32.
The l has nothing to do with LinkedList object lst that you did create on line 5.

Your error is no different from:
1
2
3
int x = 42;
int y;
// does the y have value 42? 
Isn't l is a way to get access to the linked list?
No, l is an uninitialized list without valid data. You want to use lst which contains the data you are looking for.

Line 34: head is a null. Thus there is no data connected to it and since you do not check for null line 37 will crash/is undefined behavior. You need to use lst.head where the data is actually connected to.
Oh I didn't realized I had deleted the head and there is nothing in that variable.
I,ve already fixed that.
Now there's another problem, the output file doesn't show "0", there's only 2 3 4 5 6. I found out the problem is for the while (x!=0). Since this file is just integers, do I have to use getline to read the whole of them?
1
2
3
4
5
while ( in >> x )
{
  insertTail( lst, x );
}
in.close();
Thanks a lot keskiverto!
Good days!
Topic archived. No new replies allowed.