Building linked list from text file

Linked list and read from file issues.

Page 1 of 1

3 Replies - 9491 Views - Last Post: 06 December 2007 - 03:02 PM Rate Topic: -----

#1 domjon84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 23-October 07

Building linked list from text file

Posted 30 November 2007 - 09:14 PM

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;
}

Is This A Good Question/Topic? 1

Replies To: Building linked list from text file

#2 domjon84  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 23-October 07

Re: Building linked list from text file

Posted 30 November 2007 - 09:43 PM

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;
}
Was This Post Helpful? 0
  • +
  • -

#3 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: Building linked list from text file

Posted 30 November 2007 - 09:46 PM

Next time
:code:
It's hard to read your codes.

for link list here : http://www.dreaminco...wtopic31357.htm
Was This Post Helpful? 0
  • +
  • -

#4 mohamed moghnia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-December 07

Re: Building linked list from text file

Posted 06 December 2007 - 03:02 PM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1