build linked list from text file

Hello Everyone!
I need help to create a buildList function that will build a linked list by inserting one int at a time into the front of the linked list from a text file.
I also have to use a stack template class.

Here is what I have so far in main and also a problem with the buildList function, not exactly sure how to write it correctly.

Thank you for your time!

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "stackADT.h"
void buildList ();


int main () 
{
	buildList ();
	return 0;
}
	
void buildList ()

{
string line;
  ifstream myfile ("stackNumbers.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      if (tempPtr !=NULL)
		{
			cin << tempPtr->info << endl; //NOT SURE ABOUT THIS PART!
}
 
}
 myfile.close();
 }
 
else cout << "Unable to open file"; 
}

you need to create your list.. which you haven't done it looks liek..

then you need something that creates a new node per line when it finds tmpPtr->next == null

then set your ptr to point to the null

prevPtr = tmpPtr;
tmpPtr = tmpPtr->next;


then create the new node

then assign prevPtr = new Node() //what ever your constructor is for the new node
Here is a stack template class, need some help in main. Thank you!

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
///a stack template class. 
#include <iostream>
#pragma once
#include <string>
using namespace std;

class EmptyStack
////Exception class used by Pop and Top when stack is empty
{ 
	//return (topPtr==NULL);
};

class FullStack
{
	//return false;
};

//defenition of the node
template <class T> 
struct nodeType
{
	T info;
	nodeType <T> * link;
};

template <class T>
class StackADT{

public:
	StackADT();
	/*Function: Constructor
	Precondition: None;
	Postcondition : stackADT is initialized, maxsize is set to 250, top is set to -1, items array is initialized with the size set to maxsize
	*/
	StackADT(int max);
		/*Function: Constructor
	Precondition: None;
	Postcondition : stackADT is initialized, maxsize is set to max, top is set to -1, items array is initialized with the size set to maxsize 
	*/
	bool isEmpty() const;
		/*Function: Determines whether the stack is empty
	Precondition: Stack has been initialized;
	Postcondition : Returns True if stack is empty, False if stack is not empty
	*/
	bool isFull();
		/*Function: Determines whether the stack is full
	Precondition: Stack has been initialized;
	Postcondition : Returns True if stack is full, False if stack is not full
	*/
	void Push(T newItem);
		/*Function: Adds new item T to the top of the stack
	Precondition: Stack has been initialized;
	Postcondition :if stack is full, exception "The stack is full" is thrown, else the new item T is added to the stack
	*/
	void Pop();
		/*Function: removes top item from stack
	Precondition: Stack has been initialized;
	Postcondition :if stack is empty, exception "The stack is empty is thrown, else the top element is removed
	*/
	T Top() ;
		/*Function: returns a copy of the top item on the stack
	Precondition: Stack has been initialized;
	Postcondition :if stack is empty, exception "The stack is Empty" is thrown, else the top element is returned
	*/
	~StackADT();
		/*Function: Destructor
	Precondition: Stack has been initialized;
	Postcondition :if stack is empty, exception 10 is thrown, else the top element is returned
	*/
private:
	nodeType <T> * topPtr;// Pointer to a stack
	
};

//Implementatation, because the class is a template class, contains the implementation of the class memeber for the stack class
template<class T> StackADT<T>::StackADT()//default constructor
{
	topPtr= NULL;
}

template<class T> StackADT<T>::~StackADT()
// Post: Stack is empty; all items have been deallocated.
{
   NodeType* tempPtr;

   while (topPtr != NULL)
   {
     tempPtr = topPtr;
     topPtr = topPtr->next;
     delete tempPtr;
   }
}

template <class T> bool StackADT <T>:: isEmpty () const
// Returns true if there are no elements on the stack and false otherwise.
{
  return (topPtr == NULL);
}

template <class T> void StackADT <T>::Pop()
// Removes top item from stack
// Pre:  Stack has been initialized.
// Post: If stack is empty, EmptyStack exception is thrown;
//       else, top element has been removed.
{
  if (IsEmpty())
    throw EmptyStack();
  else
  {
    NodeType* tempPtr;
    tempPtr = topPtr;
    topPtr = topPtr->next;
    delete tempPtr;
  }
}


template <class T> void StackADT <T>::Push(T newItem)
// Adds newItem to the top of the stack.
// Pre:  Stack has been initialized.
// Post: If stack is full, FullStack exception is thrown;
//       else, newItem is at the top of the stack.

{
  if (isFull())
    throw FullStack();
  else
  {
    NodeType* location;
    location = new NodeType;
    location->info = newItem;
    location->next = topPtr;
    topPtr = location;
  }
}

 template <class T> bool StackADT <T>:: isFull () 
// Returns true if there is no room for another NodeType object
//  on the free store and false otherwise.
 {
  NodeType* location;
  try
  {
    location = new NodeType;
    delete location;
    return false;
  }
  catch(std::bad_alloc exception)
  {
    return true;
  }
}

template <class T> T StackADT <T> :: Top () 
// Returns a copy of the top item in the stack.
// Pre:  Stack has been initialized.
// Post: If stack is empty. EmptyStack exception is thrown;
//       else, a copy of the top element is returned.
{
  if (IsEmpty())
    throw EmptyStack();
  else
    return topPtr->info;
}
help:)
Topic archived. No new replies allowed.