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
MYPROGRAMMINGLAB WITH PEARSON ETEXT
- True or False: Given the sets F and G with F being an element of G, is it always ture that P(F) is an element of P(G)? (P(F) and P(G) mean power sets). Why?arrow_forwardCan you please simplify (the domain is not empty) ∃xF (x) → ¬∃x(F (x) ∨ ¬G(x)). Foarrow_forwardHistogramUse par(mfrow=c(2,2)) and output 4 plots with different argument settings.arrow_forward
- (use R language)Scatter plot(a). Run the R code example, and look at the help file for plot() function. Try different values for arguments:type, pch, lty, lwd, col(b). Use par(mfrow=c(3,2)) and output 6 plots with different argument settings.arrow_forward1. Draw flow charts for each of the following;a) A system that reads three numbers and prints the value of the largest number.b) A system reads an employee name (NAME), overtime hours worked (OVERTIME), hours absent(ABSENT) and determines the bonus payment (PAYMENT).arrow_forwardScenario You work for a small company that exports artisan chocolate. Although you measure your products in kilograms, you often get orders in both pounds and ounces. You have decided that rather than have to look up conversions all the time, you could use Python code to take inputs to make conversions between the different units of measurement. You will write three blocks of code. The first will convert kilograms to pounds and ounces. The second will convert pounds to kilograms and ounces. The third will convert ounces to kilograms and pounds. The conversions are as follows: 1 kilogram = 35.274 ounces 1 kilogram = 2.20462 pounds 1 pound = 0.453592 kilograms 1 pound = 16 ounces 1 ounce = 0.0283 kilograms 1 ounce = 0.0625 pounds For the purposes of this activity the template for a function has been provided. You have not yet covered functions in the course, but they are a way of reusing code. Like a Python script, a function can have zero or more parameters. In the code window you…arrow_forward
- make a screen capture showing the StegExpose resultsarrow_forwardWhich of the following is not one of the recommended criteria for strategic objectives? Multiple Choice a) realistic b) appropriate c) sustainable d) measurablearrow_forwardManagement innovations such as total quality, benchmarking, and business process reengineering always lead to sustainable competitive advantage because everyone else is doing them. a) True b) Falsearrow_forward
- Vision statements are more specific than strategic objectives. a) True b) Falsearrow_forwardThe three components of the __________ approach to corporate accounting include financial, environmental, and social performance measures. Multiple Choice a) stakeholder b) triple dimension c) triple bottom line d) triple efficiencyarrow_forwardCompetitors, as internal stakeholders, should be included in the stakeholder management consideration of a company and in its mission statement. a) True b) Falsearrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage Learning