Hello All,
I am writing c++ program to insert, sort, display and merge two linked lists. Insertion, sorting and display functions are working fine but merge function is not working as expected. Please help me correcting merge function. I have used One header file "intSLLst.h" and two cpp file "intSLLst.cpp" and "main.cpp". Below are the codes:
intSLLst.h
ifndef INT_LINKED_LISTdefine INT_LINKED_LISTclass IntNode{
public:
int info;
IntNode *next;
IntNode (int el, IntNode *ptr = 0){
info = el;
next = ptr;
}};
class IntSLList{
public:
IntSLList(){
head = tail = 0;
}
~IntSLList();
int isEmpty(){
return head == 0;
}
void addToHeadLink(int);
/void addToTail(int);
int deleteFromHead();
int deleteFromTail();
void deleteNode(int);
bool isInList(int) const;/
void sort();
void displayLink();
void merge(IntSLList,IntSLList);private:
IntNode *head, *tail;
};endifintSLLst.cpp
include <iostream>include "intSLLst.h"using namespace std;
IntSLList::~IntSLList(){
for(IntNode *p; !isEmpty();){
p = head->next;
delete head;
head = p;
}
}void IntSLList::addToHeadLink(int el){
head = new IntNode(el,head);
if (tail == 0)
tail = head;
}void IntSLList::displayLink(){
IntNode *temp;
if (head == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = head;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}void IntSLList::sort(){
IntNode *temp, *s;
int value;
if (head == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = head;
while (temp != NULL)
{
for (s = temp->next;s !=NULL;s = s->next)
{
if (temp->info > s->info)
{
value = temp->info;
temp->info = s->info;
s->info = value;
}
}
temp = temp->next;
}
}void IntSLList::merge(IntSLList obj1, IntSLList obj2)
{
IntNode *temp, *list3;
int value=0;
IntNode *list1 = obj1.head;
IntNode *list2 = obj2.head;if (list1 == NULL) { temp = obj2.head; cout<<"Elements of list are: "<<endl; while (temp != NULL) { cout<<temp->info<<"->"; temp = temp->next; } cout<<"NULL"<<endl; } else if (list2 == NULL) { temp = obj1.head; cout<<"Elements of list are: "<<endl; while (temp != NULL) { cout<<temp->info<<"->"; temp = temp->next; } cout<<"NULL"<<endl; } else { while(list1->next!=0) { list1=list1->next; } list1->next=list2; } return;
}
main.cpp
include "intSLLst.h"include <iostream>using namespace std;
int main()
{
IntSLList MyIntSLList1;
IntSLList MyIntSLList2;
IntSLList MyIntSLList3;int num1, num2, choice; //perform operation on Linked List1 while(1) { cout << "select operation"<<endl; cout<<"1.Insert Node in Link1at beginning"<<endl; cout<<"2.Display Linked List1"<<endl; cout<<"3.Sort Linked List1"<<endl; cout<<"4.Insert Node in Link2 at beginning"<<endl; cout<<"5.Display Linked List2"<<endl; cout<<"6.Sort Linked List2"<<endl; cout<<"7.Merge"<<endl; cout<<"8.Exit"<<endl; cout<<"Enter your choice : "; cin>>choice; switch(choice) { case 1: cout<<"Inserting Node in Link1 at Beginning: "<<endl; cout << "enter value:"<<endl; cin >> num1; MyIntSLList1.addToHeadLink(num1); cout<<endl; break; case 2: cout << "Displaying Linked List1:"<< endl; MyIntSLList1.displayLink(); break; case 3: cout<<"Sort Link List: "<<endl; MyIntSLList1.sort(); cout<<endl; break; case 4: cout<<"Inserting Node in Link2 at Beginning: "<<endl; cout << "enter value:"<<endl; cin >> num2; MyIntSLList2.addToHeadLink(num2); cout<<endl; break; case 5: cout << "Displaying Linked List2:"<< endl; MyIntSLList2.displayLink(); break; case 6: cout<<"Sort Link List: "<<endl; MyIntSLList2.sort(); cout<<endl; break; case 7: cout<<"Merged Linked list:"<<endl; MyIntSLList3.merge(MyIntSLList1, MyIntSLList2); MyIntSLList3.displayLink(); cout<<endl; break; case 8: cout<<"Exiting..."<<endl; exit(1); break; } } system("pause"); return 0;
}