I need to create this function using c++, linked list and no strings 1. appendTu
ID: 3682096 • Letter: I
Question
I need to create this function using c++, linked list and no strings
1. appendTurnToEnd: this function should create a new node with value specified by turn and append
it to the end of the list.
2. freeAllMemoryInTurnList: this function frees all the memory in the list.
3. lengthTurnList: this function computes the number of turns in the linked list.
4. giveAddressLocations this function returns the number of addresses (’U’ turns) and pointers
of the nodes corresponding to the addresses. After this function is called,
addressLocations is an array of pointers to nodes in the list with U turns. These pointers can be
used later for the simplification of the turns linked list.
5. concatenateTurnArrays: this function appends a linkedlist to the end of another linked list.
Note that once the linked lists are concatenated, you only need to free the top linked list. If you
attempt to free both, you will get a segmentation fault.
You are required to create the function giveOptimizedTurnList to create the optimized path
from the original path. The main steps in this function are:
(a) use getAddressLocations to obtain the pointers to the nodes with the stations.
(b) get the delivery addresses from the user.
(c) Using the above inputs, obtain the simplified segments and concatenate them to obtain the
final list.
Explanation / Answer
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 10
struct node{
int data;
struct node *next;
};
class sngllist{
node *A,*B,*MRG;
int cnt;
public:
sngllist(){
cnt=0;
A=NULL;
B=NULL;
MRG=NULL;
}
//ALL THE OPERATIONS ARE PERFORMED ON NODE A//Except Merge, union, intersection.void create(int); //initial data assignmentvoid display(int); //process is display (assumed)int count(int);
void insert();
void del();
void search();
void copy();
void reverse();
void sort();
void merge();
void unionList();
void intersectionList();
};
void sngllist :: create(int check){
node *start=NULL,*newl=NULL,*end=NULL;
int takedata;
clrscr();
cout<<" *****Create List***** ";
while(1){
cout<<" -999 to Quit ";
cout<<" Enter data : ";
cin>>takedata;
if(takedata == -999)
break;
else{
//create memory for new node
newl = new node;
if(newl == NULL){
cout<<" Not Enough Memory";
getch();
exit(1);
}
newl->data = takedata;
newl->next = NULL;
cnt++;
//check for first nodeif(start == NULL)
start = newl;
else
end->next = newl;
end = newl;
end->next = NULL;
}//end else
}//end while//check to create which listif(check==1){
A->next = start;
A = start;
}
if(check==2){
B->next = start;
B = start;
}
if(check==3){
MRG->next = start;
MRG = start;
}
}
void sngllist :: display(int check){
node *tmp;
//check to print which listif(check==1)
tmp=A;
if(check==2)
tmp=B;
if(check==3)
tmp=MRG;
cout<<" *****Display***** ";
cout<<" ";
for( ; tmp!=NULL; tmp=tmp->next){
cout<<tmp->data;
if(tmp->next != NULL)
cout<<"-->";
}//end for
getch();
}
int sngllist :: count(int check){
node *tmp;
int cnt;
for(tmp=A,cnt=0 ; tmp!=NULL; tmp=tmp->next,cnt++);//do nothingif(check==1){
cout<<" *****Status of List***** ";
cout<<" Total Items : "<<cnt;
getch();
return(cnt);
}
elsereturn(cnt); //To use count value in other functions
}
void sngllist :: insert(){
node *newl=NULL,*tmp=NULL;
int choice,takedata,pos,i;
while(1){
clrscr();
cout<<" *****Insertion***** ";
cout<<" 1) Begining ";
cout<<" 2) In Between ";
cout<<" 3) End ";
cout<<" 4) Return to Main Menu ";
cout<<" Enter your choice : ";
cin>>choice;
if(choice==1 || choice==2 || choice==3){
//create memory for new node
newl = new node;
if(newl == NULL){
cout<<" Not Enough Memory";
getch();
exit(1);
}
cout<<" Enter data : ";
cin>>takedata;
newl->data = takedata;
newl->next = NULL;
}
elsereturn;
switch(choice){
case 1 : newl->next = A;
A = newl;
break;
case 2 : cout<<" Enter Position : ";
cin>>pos;
if(pos <=1 || pos >= count(2)){
cout<<" Invalid Position";
getch();
break;
}
else{
//to points to previous node from where//to insertfor(i=1,tmp=A; i < (pos-1); tmp=tmp->next,i++);
newl->next = tmp->next;
tmp->next = newl;
break;
}
case 3 : //For pointing to last nodefor(tmp=A; tmp->next != NULL; tmp=tmp->next);
tmp->next = newl;
}//end switch
}//end while
}
void sngllist :: del(){
node *delnode=NULL,*tmp=NULL;
int choice,deldata,pos,i;
while(1){
clrscr();
cout<<" *****Deletion***** ";
cout<<" 1) Begining ";
cout<<" 2) In Between ";
cout<<" 3) End ";
cout<<" 4) Return to Main Menu ";
cout<<" Enter your choice : ";
cin>>choice;
switch(choice){
case 1 : delnode->next = A;
delnode = A;
A = A->next;
break;
case 2 : cout<<" Enter Position : ";
cin>>pos;
if(pos <=1 || pos >= count(2)){
cout<<" Invalid Position";
getch();
break;
}
else{
//to points to previous node from where//to insertfor(i=1,tmp=A; i < (pos-1); tmp=tmp->next,i++);
delnode = tmp->next;
tmp->next = tmp->next->next;
break;
}
case 3 : //For pointing to last nodefor(tmp=A; tmp->next->next != NULL; tmp=tmp->next);
delnode = tmp->next;
tmp->next = NULL;
break;
case 4 : return;
default : cout<<" Invalid Position";
getch();
}//end switch
delete(delnode);
}//end while
}
void sngllist :: search(){
node *tmp;
int item,n;
cout<<" *****Search***** ";
cout<<" Enter data to be searched : ";
cin>>item;
for(tmp=A,n=1; tmp!=NULL; tmp=tmp->next,n++){
if(tmp->data == item){
cout<<" Search is located at "<<n<<" location";
getch();
return;
}
}
cout<<" Search Not Found";
getch();
}
void sngllist :: copy(){
node *tmp=NULL,*newl=NULL,*end=NULL;
B=NULL;
cout<<" *****Copy***** ";
for(tmp=A; tmp!=NULL; tmp=tmp->next){
//create memory
newl = new node;
newl->data = tmp->data;
newl->next = NULL;
if(B == NULL){
B->next = newl;
B = newl;
}
else
end->next=newl;
end=newl;
}
cout<<" After Copy...";
cout<<" ==List A==";
display(1); //List A
cout<<" ==List B==";
display(2); //List B
}
void sngllist :: reverse(){
//Reversing Logic without using another list
node *prev=NULL,*curr=A,*succ=A->next;
while(succ!=NULL){
curr->next = prev;
prev = curr;
curr = succ;
succ = succ->next;
}
curr->next = prev;
prev = curr;
A = prev->next;
A = prev;
display(1);
}
void sngllist :: sort(){
node *i,*j;
int tmp;
cout<<" *****Sort***** ";
for(i=A;i!=NULL;i=i->next){
for(j=i;j!=NULL;j=j->next){
if(i->data > j->data){
tmp = i->data;
i->data = j->data;
j->data = tmp;
}
}
}
cout<<" After Sort...";
cout<<" ==List==";
display(1); //List B
}
void sngllist :: merge(){
node *i,*newl=NULL,*end=NULL;
MRG=NULL;
create(1); //Create List A
create(2); //Create List B
cout<<" *****Merge***** ";
//Merging A to listfor(i=A;i!=NULL;i=i->next){
//create memory
newl = new node;
newl->data = i->data;
newl->next = NULL;
if(MRG == NULL){
MRG->next = newl;
MRG = newl;
}
else
end->next=newl;
end=newl;
}
//Merging B to listfor(i=B;i!=NULL;i=i->next){
//create memory
newl = new node;
newl->data = i->data;
newl->next = NULL;
end->next=newl;
end=newl;
}
cout<<" After Merge...";
cout<<" ==List==";
display(3); //List MRG
}
void sngllist :: unionList(){
node *i,*j,*newl=NULL,*end=NULL;
MRG=NULL;
int flag=0;
create(1); //Create List A
create(2); //Create List B
cout<<" *****Union of List***** ";
//Union A to listfor(i=A;i!=NULL;i=i->next){
if(MRG == NULL){
newl = new node;
newl->data = i->data;
newl->next = NULL;
MRG->next = newl;
MRG = newl;
end->next=newl;
end=newl;
continue;
}
for(j=MRG;j!=NULL;j=j->next){
if(i->data != j->data)
flag=1;
}
if(flag==1){
flag=0;
//create memory
newl = new node;
newl->data = i->data;
newl->next = NULL;
end->next=newl;
end=newl;
}//end if
}//end for//Union B to listfor(i=B;i!=NULL;i=i->next){
for(j=MRG;j!=NULL;j=j->next){
if(i->data != j->data)
flag=1;
}
if(flag==1){
flag=0;
//create memory
newl = new node;
newl->data = i->data;
newl->next = NULL;
end->next=newl;
end=newl;
}//end if
}//end for
cout<<" After Union...";
cout<<" ==List==";
display(3); //List MRG
}
void sngllist :: intersectionList(){
node *i,*j,*newl=NULL,*end=NULL;
MRG=NULL;
int flag=0;
create(1); //Create List A
create(2); //Create List B
cout<<" *****Intersection of List***** ";
//Intersection to listfor(i=A;i!=NULL;i=i->next){
for(j=B;j!=NULL;j=j->next){
if(i->data == j->data)
flag=1;
}
if(flag==1){
flag=0;
if(MRG == NULL){
newl = new node;
newl->data = i->data;
newl->next = NULL;
MRG->next = newl;
MRG = newl;
end->next=newl;
end=newl;
continue;
}
//create memory
newl = new node;
newl->data = i->data;
newl->next = NULL;
end->next=newl;
end=newl;
}//end if
}//end for
cout<<" After Intersection...";
cout<<" ==List==";
display(3); //List MRG
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.