0

Hello,
if I compile this code it crashes at the if statement comparison. Giving me an access violation. it is meant to check, wether the elements stored in the linked list are ascending. I would really appretiate help.
Thanks stk123

#include <fstream.h>
typedef char Item;
class Node {
    public:
        Item data;
        Node* next;
};


int main()
{
typedef Node* NodePtr;
NodePtr hdList = new Node;
NodePtr newNode = new Node;
NodePtr lastPtr = new Node;
NodePtr newPtr = new Node;
NodePtr curentPtr = new Node;
NodePtr temp1 = new Node;
NodePtr temp2 = new Node;
NodePtr temp3 = new Node;


char ch;
char le;
char ch1;
char ch2;

ifstream infile ("input_data_file.txt");
ofstream outfile ("output_file_1.txt");
ifstream infile2 ("delete_data_file.txt");
ofstream outfile2 ("output_file_2.txt");

infile.get(ch);
hdList -> data = ch;
hdList -> next = NULL;
//infile into linked list
while (infile.get(ch))
{
NodePtr newNode = new Node;
newNode -> data = ch;
newNode -> next = hdList;
hdList = newNode;
}

//bouble sort
newPtr = hdList;
curentPtr = hdList;
lastPtr = hdList;
while (lastPtr != NULL)
{
newPtr = curentPtr;
curentPtr = curentPtr -> next;
lastPtr = curentPtr -> next;
ch1 = curentPtr -> data;
ch2 = lastPtr -> data;
if (ch1 > ch2 )
{
ch1 = '3';

}

}

//linked list into outfile
while (hdList!=NULL){
le = hdList -> data;
hdList = hdList -> next;
outfile.put(le);
}


}

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Duoas
0

An access violation occurs when you try to access memory that doesn't belong to you.

You need to think a little more carefully about what you are doing with all those variables.

For starters, only mess with one linked list at a time. Also, make sure to get out a piece of paper, draw yourself some variable names and draw little arrows over to little boxes for each node (each node should also have a couple variable names in it: one for the char and one for the next pointer).

Use pencil so you can erase arrows and add and remove things.

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.