Computer Science, asked by bawinaya7080, 1 year ago

Write a 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.

Answers

Answered by chandusrujan17oxlyug
1
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\n";
cout << "1. Add item\n";
cout << "2. Remove item\n";
cout << "3. Show list\n";
cout << "4. Exit\n";
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! :))\n";
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\n";
else
{
cout << "The list contains: \n";
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: \n");
show(list1);
printf("The second list is: \n");
show(list2);
printf("The concatenated lists: \n");
concatenate(list1, list2);
show(list1);
return 0; //This is giving me an error
}
void show(node *list)
{
while (list != NULL)
{
printf("%c\t", list->data);
list = list->next;
}
printf("\n\n");
}
void concatenate(node *listA, node *listB)
{
while (listA->next != NULL)
{
listA = listA->next;
}
listA->next = listB;
}





This is the code use it.
I hope it is helpful.
Please mark me as brainleist Please
Similar questions