C How to Program (8th Edition)
C How to Program (8th Edition)
8th Edition
ISBN: 9780133976892
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
Question
Book Icon
Chapter 12, Problem 12.6E
Program Plan Intro

Program plan:

  1. Item, start variablesare used for input. There is structure listnode having data, nextPtr member variables which represents the linked list node.
  2. void insert(node **head, int value) function inserts the node in the a linked list.
  3. node *concat(node *flist, node *slist) function concat the two linked list and return the resultant linked list.
  4. 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.

Expert Solution & Answer
Check Mark

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:

  C How to Program (8th Edition), Chapter 12, Problem 12.6E

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Topic: Singly Linked ListImplement the following functions in C++ program. Read the question carefully. (See attached photo for reference)  int removeAt(int pos) Removes the number in the posth position of the list and returns the element removed. Performing removeAt(3) in the example list will remove the 3rd element of the linked list and the updated list will be: 10 -> 30 -> 50 When the value of pos is greater than the size or less than one, return -1. int removeAll(int num) Removes all instances of num in the linked list and returns the number of instances removed. In this list 10 -> 10 -> 20 -> 30 -> 10, performing removeAll(10) will remove all three 10's and the list will look like this: 20 -> 30. Then, it will return the number of instances removed, in this case, 3. int contains(int num) This will return the position of the first instance of the element num in the list. If num is not found, return 0. In the example, having the method contains(30) will…
(True/False): Arrays are passed by reference to avoid copying them onto the stack
(Attach Python file only) Write a program that does the following 1- Declare the list my_list that has the following integer numbers 6, 5, 1, -3, 10, 0, and 4 as items of the list. 2- Modify the value of list items based on the following If the item of the list is an integer and less than three, increment its value by 2. If the item of the list is an integer is greater than or equal to three, decrement its value by 3. 3- after modification, print out the items of the list that have only the even index in the reverse order. A- В I T Ff ♥ = E E E !
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education