Concept explainers
Explanation of Solution
The function for concatenating two linked lists:
The function to concatenate two linked is dined as below.
//Define method concatenate
static void concatenate(node *a, node *b)
{
//If condition satisfies
if( a != NULL && b!= NULL )
{
//If condition satisfies
if (a->nxt == NULL)
//Assign value
a->nxt = b;
//If condition satisfies
else
//Call method
concatenate(a->nxt,b);
}
//If condition satisfies
else
{
//Display message
cout << "Either a or b is NULL\n";
}
}
Explanation:
- The function “concatenate()” concatenates values in two linked lists.
- It checks initially whether two linked lists are empty or not.
- If both linked lists are non-empty, it proceeds with checking the end of first linked list.
- Assign second linked list to the end of first linked list.
- The result is been displayed finally.
Complete Program:
//Include library
#include <iostream>
//Use namespace
using namespace std;
//Define structure
struct node
{
//Declare variable
int dta;
//Declare pointer
node *nxt;
};
//Define class
class lnkdLst
{
//Define access specifier
private:
//Define nodes
node *head,*tail;
//Define access specifier
public:
//Define constructor
lnkdLst()
{
//Assign value
head = NULL;
//Assign value
tail = NULL;
}
//Define method
void AdNode(int n)
{
//Assign value
node *tmp = new node;
//Assign value
tmp->dta = n;
//Assign value
tmp->nxt = NULL;
//If condition satisfies
if(head == NULL)
{
//Assign value
head = tmp;
//Assign value
tail = tmp;
}
//If condition satisfies
else
{
//Assign value
...Trending nowThis is a popular solution!
Chapter 8 Solutions
EBK COMPUTER SCIENCE: AN OVERVIEW
- Write a C++ Function that returns the data of the middle node in a linked list and in case the linked list contains only one node return the data inside this node.arrow_forwardProgramming language: Java Topic: linked listarrow_forwardC Language In a linear linked list, write a function named changeFirstAndLast that swaps the node at the end of the list and the node at the beginning of the list. The function will take a list as a parameter and return the updated list.arrow_forward
- QUESTION: NOTE: This assignment is needed to be done in OOP(c++/java), the assignment is a part of course named data structures and algorithm. A singly linked circular list is a linked list where the last node in the list points to the first node in the list. A circular list does not contain NULL pointers. A good example of an application where circular linked list should be used is a items in the shopping cart In online shopping cart, the system must maintain a list of items and must calculate total bill by adding amount of all the items in the cart, Implement the above scenario using Circular Link List. Do Following: First create a class Item having id, name, price and quantity provide appropriate methods and then Create Cart/List class which holds an items object to represent total items in cart and next pointer Implement the method to add items in the array, remove an item and display all items. Now in the main do the following Insert Items in list Display all items. Traverse…arrow_forwardc++ language programarrow_forwardIN PYTHON Linked Lists Consider the implementation of the Linked list class, implement the following functions as part of the class: index(item) returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list. pop() removes and returns the last item in the list. It needs nothing and returns an item. Assume the list has at least one item. pop_pos(pos) removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list. a function that counts the number of times an item occurs in the linked list a function that would delete the replicate items in the linked list (i.e. leave one occurrence only of each item in the linked list) Your main function should do the following: Generate 15 random integer numbers in the range from 1 to 5. Insert each number (Item in a node) in the appropriate position in a linked list, so you will have a sorted linked list in ascending order. Display the…arrow_forward
- Write a C++ function that returns the maximum data value in the linked list.arrow_forwardWrite C code that implements a soccer team as a linked list. 1. Each node in the linkedlist should be a member of the team and should contain the following information: What position they play whether they are the captain or not Their pay 2. Write a function that adds a new members to this linkedlist at the end of the list.arrow_forwardUse C++ Programming language: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list (in ascending order), deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the list, check if the list is empty, and return the length of the list. Be sure to have a class constructor a class destructor, and a class copy constructor for deep copy. Demonstrate your class with a driver program (be sure to include the following cases: insertion at the beginning, end (note that the list should alway insert in ascending order. However, in your test include a case where the inserted item goes at the beginning of the list), and inside the list, deletion of first item, last item, and an item…arrow_forward
- Explain the differences between a statically allocated array, a dynamically allocated array, and a linked list.arrow_forwardİn C language A singly linear list stores integer values in each node and has multiple nodes. Write a function using given prototype below. This function cuts the first node of the list and adds it to the end as last node. It takes beginning address of the list as a parameter and returns the updated list.struct node* cutheadaddlast(struct node* head);struct node { int number; struct node * next; };arrow_forwardPlease write it with comments in C.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning