I have written the following program that gets two linkedlists from the user and then sholud cimpare them by the function " comparing " and if the number of their component be equal and their counterpart components be equal to each other, then cout " two lists are equal " and otherwise....and then print list1.but after running it and entering two linkedlists datas.there appeares an message with this contents" linlis.exe has stopped working"(linlis is my program name)and the problem details are here:
Problem signature:
Problem Event Name: APPCRASH
Application Name: linlisexe
Application Version: 0.0.0.0
Application Timestamp: 528508c2
Fault Module Name: linlis.exe
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 528508c2
Exception Code: c0000005
Exception Offset: 00011501
OS Version: 6.1.7601.2.1.0.256.1
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
my program is here also:
// linlis.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
struct LinkedList {
int data;
struct LinkedList *next;
};
struct LinkedList1 {
int data;
struct LinkedList1 *next;
};
void comparing(const struct LinkedList *head,const struct LinkedList1 *head1,int count, int count1)
{
int i=0;
if(count!=count1)
cout<<"your lists aren't equal"<<"\n";
while(head->data!=-1){
if (head->data==head1->data)
i++;
head = head->next;
head1 = head1->next;
}
if(i==count-1)
cout<<"two lists are equal"<<endl;
cout<<"your lists aren't equal"<<"\n";
}
void main()
{
int current=1,current1=1,counter=0,counter1=0;
struct LinkedList *head=NULL,*temp=0;
struct LinkedList1 *head1=NULL,*temp1=0;
cout<<"enter list1:"<<"\n";
while(current!=-1){
counter++;
temp=new LinkedList;
cin>>temp->data;
current=temp->data;
"\n";
head=temp;
head=head->next;
}
cout<<"enter list2:"<<"\n";
while(current1!=-1){
counter1++;
temp1=new LinkedList1;
cin>>temp1->data;
current1=temp1->data;
"\n";
head1=temp1;
head1=head1->next;
}
comparing(head,head1,counter,counter1);
printList(head);
}
I don't know what is the problem with my program.Is ther any idea about that?
Every loop you are setting the value of the head to some unknown value.
You need to either reset the head on every interation or set the head
once and append to the back of the list (you will need another pointer
to keep track of the last node in the list)
EDIT: Looking at your compare you want to append the new value to the end of the list because you assume that when you see -1 you are at the end of the list. So, -1 must be at the end of the list for your logic to work. Or you could change that logic. It's all how you want it to work.
thank you for your reply. but so sorry I am so beginner in linkedlists and that is my second program in programming via linkedlists. you know, the logic must be -1 because my master wanted me to enter two lists and the end of the lists must be -1. and I can't understand clearly what you mean. the head in every iteration of loop is setted to the temp value and the next head must be placed in head. you know I do the iteration and loops for my other programs but I don't know exactly how to do it for linkedlists.I thought it have to be as the same manner as others but as if it is a little different. I alter two last statements in many various moods and as you suggested add another pointer to track the last node but none of them make any difference and the message persists as it did before.!
Make sure you understand the picture and the code that goes with it.
Its about linking each node to the other!
You can make it do whatever you want, just think about it and draw a picture to help you see what you want to do. Then make it do it.
The values for the addresses are just fake (there for example only), each run of the code new will most likely return different values.
What you want to happen:
head tail
------ ------
| Null | | Null |
------ ------
temp = new LinkedList
Enter a number: 4
head
------- Address 0x456
| 0x456 | ----> --------------
------- | data=4 |
| next=??? |
--------------
tail ^
------- |
| 0x456 | ------------+
-------
temp = new LinkedList
Enter a number: 10
**** Don't change the value of head ****
Add the new node to the end of the link list.
Q: Where is the end of the link list?
A: tail
tail
------- Address 0x456
| 0x456 | ----> --------------
------- | data=4 | Address 0x678
| next=0x678 ------> --------------
-------------- | data=10 |
| next=??? |
--------------
Then reset the tail and the list looks like:
head
------- Address 0x456
| 0x456 | ----> --------------
------- | data=4 | Address 0x678
| next=0x678 ------> --------------
-------------- | data=10 |
| next=??? |
--------------
tail ^
------- |
| 0x678 | --------------------------------+
-------
temp = new LinkedList
Enter a number: -1
Add the new node to the end of the link list.
Q: Where is the end of the link list?
A: tail
tail
------- Address 0x678
| 0x678 | ----> --------------
------- | data=10 | Address 0x789
| next=0x789 ------> --------------
-------------- | data=-1 |
| next=??? |
--------------
Then reset the tail and the list looks like:
head
------- Address 0x456
| 0x456 | ----> --------------
------- | data=4 | Address 0x678
| next=0x678 ------> --------------
-------------- | data=10 |
| next=0x789 ----> Address 0x789
-------------- --------------
| data=-1 |
| next=??? |
--------------
tail ^
------- |
| 0x789 | ---------------------------------------------------+
-------
int current=1,current1=1,counter=0,counter1=0;
struct LinkedList *head(NULL),
*temp(NULL),
*tail(NULL);
struct LinkedList1 *head1(NULL),
*temp1(NULL),
*tail1(NULL);
cout<<"enter list1:"<<"\n";
while(current!=-1){
counter++;
temp=new LinkedList;
cin>>temp->data;
current=temp->data;
// Check to see if the head is set
if (head == NULL){
// Only want to set the head ONCE if
// appending to the end of the list
head=temp;
} else {
// Append new values to the end
tail->next=temp;
}
// Reset the tail pointer
tail = temp;
}
// Same for the other
yes I did understand those picture and codes and I correct only my two loops for entering two lists . the comparing function was correct and it worked. but now the list is printed and thereafter the same message appeares again! and this happens only when I call the printlist function. when I want to print both lists and add another prinlist function(printlist1) as the same I had done for the first list, it just print the list the function of which was called first . when I call the comparing func last and the other two before just one func form three funcs that were called works and when call the comparing first and the two printlist funcs later just two first funcs work. I have no idea about that message and it's relation to the printlist funcs . altough I have seen that message many a time while I was working with my other programs.
EDIT: I think it is while (head!=NULL). There is no reason to think the next pointer will be NULL went you get to the end of the link list. When you new the node you need set the. Ext pointer to NULL. That is the only way you can garuntee that pointer will be NULL went you get to the end.