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

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




Building linked list from text file

 
Reply to this topicStart new topic

Building linked list from text file, Linked list and read from file issues.

domjon84
30 Nov, 2007 - 08:14 PM
Post #1

New D.I.C Head
*

Joined: 23 Oct, 2007
Posts: 5


My Contributions
Hello all,

The problem I am currently trying to solve is I need to read in integers from a file "Big Integers" to be exact, store them in a linked list and the perform addition and subtraction. My first thought at attacking this issue is to traverse the list and them perform the addition and subtraction. My issue is the file opens and will not build the list properly so you would imagine I cannot go any futher. This is some of the code I have been working on.

Thanks for any input.




#include <iostream>
#include <fstream>
using namespace std;

struct node
{
int num;
node *link;
};

void print(node*);

int main()
{
node *first = NULL, *last = NULL, *curr = NULL;
int num;
ofstream infile;

infile.open("input.txt");
if (!infile)
{
cout << "Error opening data file.\n";
exit(0);
}
while (infile >> num, infile) //this line keeps giving me an operator error
{
curr = new node;
curr->num = num;
curr->link = NULL;

if (first == NULL)
first = last = curr;
else
{
last->link = curr;
last = curr;
}


}
print(first);
infile.close();
return 0;
}

void print(node *first)
{
if (first == NULL)
return;

do
{
cout << first->num << " ";
first = first->link;
} while (first != NULL);
cout << endl;
}

User is offlineProfile CardPM
+Quote Post

domjon84
RE: Building Linked List From Text File
30 Nov, 2007 - 08:43 PM
Post #2

New D.I.C Head
*

Joined: 23 Oct, 2007
Posts: 5


My Contributions
ok I went back and worked more on it and I fixed the compilation error but it will not output anything.......
here is a new copy of the code




#include <iostream>
#include <fstream>
using namespace std;

struct node
{
int num;
node *link;
};

void print(node*);

int main()
{
node *first = NULL, *last = NULL, *curr = NULL;
int num;
char number;
ofstream infile;

infile.open("input.txt");
if (!infile)
{
cout << "Error opening data file.\n";
exit(0);
}
cin.get(number);
num = number;
while (num != number, infile)
{
curr = new node;
curr->num = num;
curr->link = NULL;

if (first == NULL)
first = last = curr;
else
{
last->link = curr;
last = curr;
}


}
print(first);
infile.close();
return 0;
}

void print(node *first)
{
if (first == NULL)
return;

do
{
cout << first->num << " ";
first = first->link;
} while (first != NULL);
cout << endl;
}

User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: Building Linked List From Text File
30 Nov, 2007 - 08:46 PM
Post #3

D.I.C Foot
Group Icon

Joined: 1 Aug, 2007
Posts: 983



Thanked: 2 times
Dream Kudos: 375
My Contributions
Next time
code.gif
It's hard to read your codes.

for link list here : http://www.dreamincode.net/forums/showtopic31357.htm
User is offlineProfile CardPM
+Quote Post

mohamed moghnia
RE: Building Linked List From Text File
6 Dec, 2007 - 02:02 PM
Post #4

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 4


My Contributions
hello, please I need some one to solve my assignment in link list
and I would appreciate your feedback

Write a program that maintains and operates on lists of integers. The user of the program may
print a list, start a new list, add or delete integers from a list, etc. The program reads instructions
from a text file called hw04.txt. Each instruction is specified on a separate text line. After an
instruction is performed, the program should take another instruction until instructed to quit. Here’s a
complete list of possible instructions:
w x → switch to list # x, thus list # x becomes the current list. The program maintains 9 lists,
numbered 1 to 9. Initially, list # 1 is the current list.
p → print the current list.
n → clears the current list to empty.
a x → add integer x to the current list. if x already in the list, ignore, but prints a warning
message.
d y → delete integer y from the current list. print a warning message if y is not in the list.
c → print the number of elements in the current list.
s → print the sum of the elements in the current list.
g z → sets the current list to a new list made of the first z fibonacci numbers.
q → quits the program.
The List must be maintained in an ascending order.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:43AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month