C++ Linked List. Okay so I have this source code for a simple text editor. It ta
ID: 673352 • Letter: C
Question
C++ Linked List.
Okay so I have this source code for a simple text editor. It takes a function and is able to insert, delete and print and everything seems to work but I can't pass my file through for some reason; which as you can see is the cornerstone to my program. Can somebody please help me with my code and see whats wrong so that my list can pass through?
The text file looks like this:
1> The first line
2> The second line
3>
4> And another line
5> One more line
6>
7> what do you try to type here?
8> I like sugar cookies
9>
10> This line is boring......
11> This is the last line
My header file looks like this
#include<iostream>
using namespace std;
struct List_Node
{
char *textLine;
struct List_Node *nextEle;
friend class listTextEditor;
}*startList;
class listTextEditor
{
public:
List_Node* create_node(char *);
void insertLineFromFile(char *);
void insertLineList(int linPos);
void deleteLineList(int linPos);
void displayElement();
void printInstruct();
listTextEditor()
{
startList = NULL;
}
};
And my source code looks like this, the comments is where I'm having trouble. This also NEEDS TO COMPILE IN A LINUX/UNIX ENVIRONMENT.
#include <cstdlib>
#include <iostream>
#include <string>
#include "Textlist.h"
#include <fstream>
using namespace std;
List_Node *listTextEditor::create_node(char *value)
{
struct List_Node *tempNode;
struct List_Node *s;
tempNode = new(struct List_Node);
if (tempNode == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
tempNode->textLine = value;
tempNode->nextEle = NULL;
return tempNode;
}
}
void listTextEditor::insertLineFromFile(char *line )
{
struct List_Node *tempNode, *s;
tempNode = new(struct List_Node);
tempNode->textLine=line;
tempNode->nextEle=NULL;
if (startList == NULL)
{
startList = tempNode;
startList->nextEle = NULL;
}
else
{
s = startList;
while (s->nextEle != NULL)
{
s = s->nextEle;
}
tempNode->nextEle = NULL;
s->nextEle = tempNode;
}
}
void listTextEditor::insertLineList(int linPos)
{
int value;
int cntLin = 0;
char line[200];
cout<<"Enter the line to be inserted: ";
cin>>line;
struct List_Node *tempNode, *s, *ptrNode;
tempNode = create_node(line);
int k1;
s = startList;
while (s != NULL)
{
s = s->nextEle;
cntLin++;
}
if (linPos == 1)
{
if (startList == NULL)
{
startList = tempNode;
startList->nextEle = NULL;
}
else
{
ptrNode = startList;
startList = tempNode;
startList->nextEle = ptrNode;
}
}
else if (linPos > 1 && linPos <= cntLin)
{
s = startList;
for (k1 = 1; k1 < linPos; k1++)
{
ptrNode = s;
s = s->nextEle;
}
ptrNode->nextEle = tempNode;
tempNode->nextEle = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
void listTextEditor::deleteLineList(int linPos)
{
int i, cntLin = 0;
if (startList == NULL)
{
cout<<"List is empty"<<endl;
return;
}
struct List_Node *s, *ptrNode;
s = startList;
if (linPos == 1)
{
startList = s->nextEle;
}
else
{
while (s != NULL)
{
s = s->nextEle;
cntLin++;
}
if (linPos > 0 && linPos <= cntLin)
{
s = startList;
for (i = 1;i < linPos;i++)
{
ptrNode = s;
s = s->nextEle;
}
ptrNode->nextEle = s->nextEle;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
void listTextEditor::displayElement()
{
int linNo=1;
struct List_Node *tempNode;
if (startList == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
tempNode = startList;
cout<<"Elements of list are: "<<endl;
while (tempNode != NULL)
{
cout<<linNo<<" "<<tempNode->textLine;
tempNode = tempNode->nextEle;
linNo++;
}
cout<<"NULL"<<endl;
}
void listTextEditor::printInstruct()
{
cout << "==============================================================" << endl;
cout << "Welcome to my text editor." << endl;
cout << " " << "To insert text at the end of the file, type a line and " << endl;
cout << "press enter." << endl;
cout << " " << "To insert text at a certain line number, type 'I'" << endl;
cout << "followed by a space and the desired line number." << endl;
cout << " " << "To delete a line, type 'D' followed by a space and the" << endl;
cout << "line number." << endl;
cout << " " << "To print all the lines, type 'L' and press enter." << endl;
cout << " " << "To exit, type 'E' and press enter." << endl;
cout << " " << "To display this introduction, type 'H' and press enter enter" << endl;
cout << "=============================================================="<< endl;
}
int main(int argc, char* argv[])
{
char choice;
int linNo;
listTextEditor command;
startList = NULL;
int linPos;
fstream inRFile;
char *text;
//my problem is occuring here!! claims my text pointer is not initiliazed
inRFile.open("CS216PA2.txt");
while(inRFile>>linPos>>text)
{
command.insertLineFromFile(text);
}
command.printInstruct();
command.displayElement();
do
{
cin>>choice;
cin.ignore(256, ' ');
switch(choice)
{
case 'I':
cout<<"Enter the line Number where you want to insert:"<<endl;
cin>>linNo;
command.insertLineList(linNo);
cout<<endl;
break;
case 'D':
cout<<"Enter the line Number where you want to delete:"<<endl;
cin>>linNo;
command.deleteLineList(linNo);
break;
case 'L':
cout<<"Display elements of link list"<<endl;
command.displayElement();
cout<<endl;
break;
case 'H':
command.printInstruct();
break;
case 'Q':
break;
case 'E':
break;
default:
cout << "Invalid choice! Please try again!. " << endl;
break;
}
}while (choice != 'E');
cout << "Thank you for using the program!" << endl;
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
struct List_Node
{
char *textLine;
struct List_Node *nextEle;
friend class listTextEditor;
}*startList;
class listTextEditor
{
public:
List_Node* create_node(char *);
void insertLineFromFile(char *);
void insertLineList(int linPos);
void deleteLineList(int linPos);
void displayElement();
void printInstruct();
listTextEditor()
{
startList = NULL;
}
};
void addNode(struct Node *head, int n)
{
List_Node *newNode = new List_Node;
newNode->data = n;
newNode->next = NULL;
List_Node *cur = head;
while(cur)
{
if(cur->next == NULL)
{
cur->next = newNode;
return;
}
cur = cur->next;
}
}
void insertFront(struct Node **head, int n) {
List_Node *newNode = new List_Node;
newNode->data = n;
newNode->next = *head;
*head = newNode;
}
struct List_Node *searchNode(struct List_Node *head, int n) {
Node *cur = head;
while(cur) {
if(cur->data == n) return cur;
cur = cur->next;
}
cout << "No Node " << n << " in list. ";
}
bool deleteNode(struct Node **head, List_Node *ptrDel) {
List_Node *cur = *head;
if(ptrDel == *head)
{
*head = cur->next;
delete ptrDel;
return true;
}
while(cur) {
if(cur->next == ptrDel) {
cur->next = ptrDel->next;
delete ptrDel;
return true;
}
cur = cur->next;
}
return false;
}
struct List_Node* reverse(struct List_Node** head)
{
Node *parent = *head;
List_Node *me = parent->next;
List_Node *child = me->next;
parent->next = NULL;
while(child) {
me->next = parent;
parent = me;
me = child;
child = child->next;
}
me->next = parent;
*head = me;
return *head;
}
void copyLinkedList(struct List_Node *node, struct Node **pNew)
{
if(node != NULL) {
*pNew = new Node;
(*pNew)->data = node->data;
(*pNew)->next = NULL;
copyLinkedList(node->next, &((*pNew)->next));
}
}
int compareLinkedList(struct List_Node *node1, struct List_Node *node2)
{
static int flag;
if(node1 == NULL && node2 == NULL) {
flag = 1;
}
else {
if(node1 == NULL || node2 == NULL)
flag = 0;
else if(node1->data != node2->data)
flag = 0;
else
compareLinkedList(node1->next, node2->next);
}
return flag;
}
void deleteLinkedList(struct List_Node **node)
{
struct Node *tmpNode;
while(*node) {
tmpNode = *node;
*node = tmpNode->next;
delete tmpNode;
}
}
void display(struct List_Node *head) {
List_Node *list = head;
while(list) {
cout << list->data << " ";
list = list->next;
}
cout << endl;
cout << endl;
}
int main()
{
struct List_Node *newHead;
struct Node *head = new Node;
initNode(head,10);
display(head);
addNode(head,20);
display(head);
addNode(head,30);
display(head);
addNode(head,35);
display(head);
addNode(head,40);
display(head);
insertFront(&head,5);
display(head);
int numDel = 5;
Node *ptrDelete = searchNode(head,numDel);
if(deleteNode(&head,ptrDelete))
cout << "Node "<< numDel << " deleted! ";
display(head);
cout << "The list is reversed ";
reverse(&head);
display(head);
cout << "The list is copied ";
copyLinkedList(head,&newHead);
display(newHead);
cout << "Comparing the two lists... ";
cout << "Are the two lists same? ";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same! ";
else
cout << "No, they are different! ";
cout << endl;
numDel = 35;
ptrDelete = searchNode(newHead,numDel);
if(deleteNode(&newHead,ptrDelete)) {
cout << "Node "<< numDel << " deleted! ";
cout << "The new list after the delete is ";
display(newHead);
}
cout << "Comparing the two lists again... ";
cout << "Are the two lists same? ";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same! ";
else
cout << "No, they are different! ";
cout << endl;
cout << "Deleting the copied list ";
deleteLinkedList(&newHead);
display(newHead);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.