Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a c++ program that concatenates two linked list objects of characters. The

ID: 669546 • Letter: W

Question

Write a c++ program that concatenates two linked list objects of characters. The program should include function concatenate, which takes references to both list objects as arguments and concatenates the second list to the first list.

Here's my code (the return 0 gives me an error that the value type does not match the function type and the program just keeps generating characters forever:

#include <iostream>


//using namespace std;

typedef struct node
{
   char data;
   int number;
   node *next;
};

bool isEmpty(node *head);
char menu();
void insertAsFirst(node *&head, node *&last, int number);
void insert(node *&head, node *&last, int number);
void remove(node *&head, node *&last);

void show(node *list);
void concatenate(node *listA, node *listB);

/*bool isEmpty(node *head)
{
   if (head == NULL)
       return true;
   else
       return false;

}


char menu()
{
   char choice;
   cout << "Menu ";
   cout << "1. Add item ";
   cout << "2. Remove item ";
   cout << "3. Show list ";
   cout << "4. Exit ";

   cin >> choice;
   return choice;
}


void insertAsFirst(node *&head, node *&last, int number)
{
   node *temp = new node;
   temp->number = number;
   temp->next = NULL;
   head = temp;
   last = temp;
}


void insert(node *&head, node *&last, int number)
{
   if (isEmpty(head))
       insertAsFirst(head, last, number);
   else
   {
       node *temp = new node;
       temp->number = number;
       temp->next = NULL;
       last->next = temp;
       last = temp;
   }
}


void remove(node *&head, node *&last)
{
   if (isEmpty(head))
       cout << "The list is empty! :)) ";
   else if (head == last)
   {
       delete head;
       head == NULL;
       last == NULL;
   }
   else
   {
       node *temp = head;
       head = head->next;
       delete temp;
   }

}


void show(node *current)
{
   if (isEmpty(current))
       cout << "The list is empty ";
   else
   {
       cout << "The list contains: ";
       while (current != NULL)
       {
           cout << current->number << endl;
           current = current->next;
       }
   }

}*/

void main(void)
{

   node *head = NULL;
   node *list1, *list2;
   int i;


   for (i = 74; i >= 65; i--)
   {
       list1 = (node *)malloc(sizeof(node));
       list1->data = i;
       list1->next = head;
       head = list1;
   }

   for (i = 84; i >= 75; i--)
   {
       list2 = (node *)malloc(sizeof(node));
       list2->data = i;
       list2->next = head;
       head = list2;
   }

   printf("The first list is: ");
   show(list1);

   printf("The second list is: ");
   show(list2);

   printf("The concatenated lists: ");
   concatenate(list1, list2);
   show(list1);

   return 0; //This is giving me an error

}

void show(node *list)
{
   while (list != NULL)
   {
       printf("%c ", list->data);
       list = list->next;
   }
   printf(" ");
}

void concatenate(node *listA, node *listB)
{
   while (listA->next != NULL)
   {
       listA = listA->next;
   }
   listA->next = listB;
}

Explanation / Answer

Please use below code::::

#include <iostream.h>
#include <stdlib.h> //for exit(1)
#include <conio.h>
#define MAX 10


struct node{
       int data;
       struct node *lptr;
       struct node *rptr;
};

class dbllist{
    node *head;
    public:
    dbllist(){
       head=NULL;
    }
    void create(); //initial data assignmentvoid display(int); //process is display (assumed)int count(int);
    void insert();
    void del();
    void search();
    void reverse();
    void sort();
};


void dbllist :: create(){
     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->lptr = NULL;
          newl->rptr = NULL;

          //check for first nodeif(start == NULL){
          start->rptr = newl;
          newl->lptr = start;
          start = newl;
          }
          else{
          end->rptr = newl;
          newl->lptr = end;
          }
          end = newl;
    }//end else
     }//end while

    head->rptr = start;
    start->lptr = head;
    head = start;
}

void dbllist :: display(int check){
     node *tmp=NULL;

     cout<<" *****Display***** ";
     cout<<" ";
   if(check==1){ //forward displayfor(tmp=head; tmp!=NULL; tmp=tmp->rptr){
     cout<<tmp->data;
     if(tmp->rptr != NULL)
        cout<<"-->";
     }//end for
   }
else{   //Reverse displayfor( tmp=head; tmp->rptr!=NULL; tmp=tmp->rptr);//points to last nodewhile(tmp!=NULL)
     {
     cout<<tmp->data;
     if(tmp->lptr != NULL)
        cout<<"-->";
     tmp = tmp->lptr;
     }//end whiile
}//end if
getch();
}

int dbllist :: count(int check){
     node *tmp=NULL;
     int cnt;

     for(tmp=head,cnt=0 ; tmp!=NULL; tmp=tmp->rptr,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 dbllist :: insert(){
     node *newl=NULL,*tmp=NULL,*prev=NULL,*next=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->lptr = NULL;
      newl->rptr = NULL;
    }
    elsereturn;

    switch(choice){
         case 1 :   newl->rptr = head;
            head->lptr = newl;
            head = 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,prev=head; i < (pos-1); prev=prev->rptr,i++);

               next = prev->rptr;
               newl->rptr = next;
               next->lptr = newl;
               newl->lptr = prev;
               prev->rptr = newl;
               break;
            }
         case 3 :   //For pointing to last nodefor(tmp=head; tmp->rptr != NULL; tmp=tmp->rptr);

            tmp->rptr = newl;
            newl->lptr = tmp;
    }//end switch
     }//end while
}

void dbllist :: del(){
     node *delnode=NULL,*tmp=NULL,*prev=NULL,*next=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 = head;
            head = head->rptr;
            head->lptr = NULL;
            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,prev=head; i < (pos-1); prev=prev->rptr,i++);

               next=prev->rptr->rptr;
               delnode = prev->rptr;
               prev->rptr = prev->rptr->rptr;
               next->lptr = prev;
               break;
            }
         case 3 :   //For pointing to last nodefor(tmp=head; tmp->rptr->rptr != NULL; tmp=tmp->rptr);
            delnode = tmp->rptr;
            tmp->rptr = NULL;
            break;
         case 4 : return;
         default :   cout<<" Invalid Position";
             getch();
    }//end switch
    delete(delnode);
     }//end while
}

void dbllist :: search(){
     node *tmp=NULL;
     int item,n;
     cout<<" *****Search***** ";
     cout<<" Enter data to be searched : ";
     cin>>item;
     for(tmp=head,n=1; tmp!=NULL; tmp=tmp->rptr,n++){
     if(tmp->data == item){
        cout<<" Search is located at "<<n<<" location";
        getch();
        return;
     }
     }
     cout<<" Search Not Found";
     getch();
}


void dbllist :: sort(){
     node *i,*j;
     int tmp;
     cout<<" *****Sort***** ";
     for(i=head;i!=NULL;i=i->rptr){
       for(j=i;j!=NULL;j=j->rptr){
      if(i->data > j->data){
         tmp = i->data;
         i->data = j->data;
         j->data = tmp;
      }
       }
     }
     cout<<" After Sort...";
     cout<<" ==List==";
     display(1);
}


void main()
{
   int choice;
   dbllist obj;
   while(1){
   clrscr();
   cout<<" DOUBLY LINK-LIST OPERATIONS ";
   cout<<" 1) Create List ";
   cout<<" 2) Display List ";
   cout<<" 3) List Status ";
   cout<<" 4) List Insertion ";
   cout<<" 5) List Deletion ";
   cout<<" 6) Search List ";
   cout<<" 7) Display Reverse List ";
   cout<<" 8) Sort List ";
   cout<<" 9) Exit ";
   cout<<" Enter your Choice : ";
   cin>>choice;
   switch(choice){
     case 1 : obj.create(); // 1 for A listbreak;
     case 2 : obj.display(1);// 1 for A listbreak;
     case 3 : choice = obj.count(1);
           //choice value is not used anywhere//it is just a placeholderbreak;
     case 4 : obj.insert();
           break;
     case 5 : obj.del();
           break;
     case 6 : obj.search();
           break;
     case 7 : obj.display(2);
           break;
     case 8 : obj.sort();
           break;
     case 9 : gotoout;
     default: cout<<" Invalid Choice ";
           getch();
           break;
   }
}
out:
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote