Program plan:
- Item, start variablesare used for input. There is structure listnode having data, nextPtr member variables which represents the linked list node.
- void insert(node **head, int value) function inserts the node in the a linked list.
- node *concat(node *flist, node *slist) function concat the two linked list and return the resultant linked list.
- void printList(node *head) function display the contents of the linked list.
Program description:
The main purpose of the program is to perform the concatenation of two linked list by implanting the concept which is same as strcat() function of string.
Explanation of Solution
Program:
#include <stdio.h> #include <conio.h> #include <alloc.h> #include <ctype.h> //structure of node of the linked list typedefstruct listnode { int data; struct listnode *nextPtr; } node; //function to insert a node in a linked list void insert(node &head,int value); //recursive function to concate the linked list node *concat(node *flist, node *slist); //function to print the content of linked list void printList(node *head); //main starts here void main() { int item; node *flist,*slist; clrscr(); //initialization of start node of linked list flist =NULL; slist =NULL; //loop to getting input from the user for first linked list while(1) { printf("\nEnter value to insert in a First List: 0 to End "); scanf("%d",&item); //check condition to terminate while loop if(item ==0) break; //insert value in a linked list insert(&flist, item); } //loop to getting input from the user for second linked list while(1) { printf("\nEnter value to insert in a Second List: 0 to End "); scanf("%d",&item); //check condition to terminate while loop if(item ==0) break; //insert value in a linked list insert(&slist, item); } // print the contents of linked list printf("\n Content of First List are as follows : "); printList(flist); // print the contents of linked list printf("\n Content of Second List are as follows : "); printList(slist); //call the function to reverse the linked list // and store the address of first node flist = concat(flist, slist); //print the contents of concated list printf("\n Concatenated List contents are as follows : "); printList(flist); getch(); } //function definition to insert node in a linked list void insert(node &head,int value) { node *ptr,*tempnode; //memory allocation for the new node ptr = malloc(sizeof(node)); //copy the value to the new node ptr->data = value; //set node's pointer to NULL ptr->nextPtr =NULL; //if the list is empty if(*head ==NULL) //make the first node *head = ptr; else { //copy the address of first node tempnode =*head; //traverse the list using loop until it reaches //to the last node while(tempnode->nextPtr !=NULL) tempnode = tempnode->nextPtr; //make the new node as last node tempnode->nextPtr = ptr; } } //function defintion to concat the list node *concat(node *flist, node *slist) { node *ptr; //check if list is empty if(flist ==NULL|| slist ==NULL) { printf("\none of the lists is empty"); //return the node returnNULL; } else { //store first node address ptr = flist; //traverse list until last node is not encountered while(ptr->nextPtr !=NULL) ptr = ptr->nextPtr; //store the address of first node of second list ptr->nextPtr = slist; } //return new list return flist; } //function definition to display linked list contents void printList(node *head) { node *ptr; //stores the address of first node ptr = head; //traverse the list until it reaches to the NULL while(ptr !=NULL) { //print the content of current node printf("%d ", ptr->data); //goto the next node ptr = ptr->nextPtr; } }
Explanation:In the above code, a structure is created which represents the node of the linked list. Two starting nodes are initialized which contains the address of first node of each list. User is asked to enter the values for first linked list and linked list is created using insert() function by passing the starting pointer and value. This process is repeated to create the second linked list. printList() function is used to display the contents of both lists. Both lists are concatenated using the concat() function by passing the both list. In this function first list is traversed up to last node and then last node pointer points to the first node of second list. Starting node address is returned and stored in the pointer. Finally, concatenated list is displayed using printList() function.
Sample output:
Want to see more full solutions like this?
Chapter 12 Solutions
C How to Program (8th Edition)
- [ ] [] power In the cell below, you are to write a function "power(list)" that takes in a list as its input, and then returns the power set of that list. You may assume that the input will not have any duplicates (i.e., it's a set). After compiling the above cell, you should be able to compile the following cell and obtain the desired outputs. print (power ( ["A", "B"]), power ( ["A", "B", "C"])) + Code + Markdown This should return [[], ["A"], ["B"], ["A", "B"]] [[], ["A"], ["B"], ["C"], ["A", "B"], ["A", "C"], ["B", "C"], ["A", "B", "C"]] (the order in which the 'sets' appear does not matter, just that they are all there) Python Pythonarrow_forward(Difficulty Level 3)Write a function frequency_match(text, chars) that receives a string text and a dictionary chars that has characters as keys and numbers as values. The function must return a list with all the characters that occur in the text as many times as their associated value in the char dictionary. Not all the chars in the text will be present in the dictionary, but you may assume that all the chars in the dictionary are in the text. You are allowed to change the values in the dictionary. solve using pythonarrow_forwardTopic: Singly Linked ListImplement the following functions in C++ program. Read the question carefully. Below are the "CODE THAT NEEDS TO BE IMPROVED" or "NOT FINAL CODE" (See attached photo for reference) int add(int num) This will add the element num into the linked list and return the position of the newly-added element. In the above example, having add(60) will return 5 as it is the fifth position in the list. int remove(int num) This will remove the first instance of the element and return the position of the removed element. In the above example, having remove(40) will return 3 as 40 was the third element in the linked list before having removed. int get(int pos) This method will get the element at the specified position. This will return -1 if the specified position is invalid. int add(int num) { addTail(num); return index; } int remove(int num) { node* currnode = head; node* prevnode = NULL;…arrow_forward
- (Using Python)Write a function that returns a new list by eliminating theduplicate values in the list. Use the following function header:def eliminateDuplicates(lst):Write a test program that reads in a list of integers, invokes the function,and displays the result. Here is the sample run of the program Enter ten numbers: 2 3 2 1 6 3 4 5 2The distinct numbers are: 1 2 3 6 4 5arrow_forwardDo the whole program in C++ & Please kindly share the whole programarrow_forward(Replace strings) Write the following function that replaces the occurrence of a substring old_substring with a new substring new_substring in the string s. The function returns true if string s is changed, and otherwise, it returns false. bool replace_strings (string& s, const string& old_string, const string& new_string) Write a test program that prompts the user to enter three strings, i.e., s, old string, and new_string, and display the replaced string.arrow_forward
- (USING PYTHON) create a function create employee_objects that takes a list of data as input and returns back a list with each element as an object that belongs to class Employee def create employee_objects (data): #start of code # end of code employees_list = create employee_objects (employees_data) Expected output: 29arrow_forwardPsecode code explaination of the problem C++arrow_forward(USING PYTHON) create a function create employee_objects that takes a list of data as input and returns back a list with each element as an object that belongs to class Employee def create employee_objects (data): # start of code # end of code employees_list = create_employee_objects (employees_data) Expected output: 29|arrow_forward
- Create a c++ shopping cart program using: linked list- - this will be used as the customer's cart. This is where the items that the customer will buy are placed before checking them out. Array - to store your store products. Pointers - since you will be using linked lists, it is imperative that you also will be using pointers. Functions - Make sure that your program will have user-defined functions (e.g.: function for adding nodes, function for displaying the cart, etc.) The menu options would be coutarrow_forwardPython Programming - (Duplicate Elimination) Part 1: Create a function that receives a list and returns a (possibly shorter) list containing only the unique values in sorted order. Test your function with a list of numbers and a list of strings. Program is to also display results, that's the sorted list containing values. Part 2: Instead of hard coding the list passed to the sorting functions, request list values from user. (Hint: Request two lists from user, one containing values and another containing string.) Have the program eliminate duplicated values and display the lists (tasks required in part 1).arrow_forwardscheme: Q9: Sub All Write sub-all, which takes a list s, a list of old words, and a list of new words; the last two lists must be the same length. It returns a list with the elements of s, but with each word that occurs in the second argument replaced by the corresponding word of the third argument. You may use substitute in your solution. Assume that olds and news have no elements in common. (define (sub-all s olds news) 'YOUR-CODE-HERE )arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education