Assuming:
struct node
{
int data;
node * next;
node * tail;
};
and
copyList(node * head);
write a function to recursively copy a circular linked list.
HERE IS MY CODE :-
#include <iostream>
using namespace std;
struct node{
int data;
node* next;
node* tail;
};
node* add(node *head,int val)
{
if(head==NULL)
{
node* temp = new node();
temp->data = val;
temp->next = temp;
return temp;
}
node* ptr = head;
while(ptr->next!=head)
{
ptr = ptr->next;
}
node* temp = new node();
temp->data = val;
temp->next = head;
ptr->next = temp;
return head;
}
void print(node* head)
{
node* ptr = head;
if(ptr==NULL)
{
cout<<"\n";
return ;
}
do
{
cout<<ptr->data<<" ";
ptr = ptr->next;
}
while(ptr!=head);
cout<<endl;
return ;
}
node* recursiveCopy(node* head,node* start)
{
if(head->next==start)
{
node* temp = new node();
temp->data = head->data;
head->next = NULL;
return temp;
}
node* temp = new node();
temp->data = head->data;
temp->next = recursiveCopy(head->next,start);
}
node* copyList(node* head)
{
if(head!=NULL)
{
node* start = recursiveCopy(head,head);
node* ptr = start;
while(ptr->next!=NULL)
{
ptr = ptr->next;
}
ptr->next = start;
return start;
}
}
int main()
{
node* head = NULL;
head = add(head,5);
head = add(head,6);
head = add(head,6);
head = add(head,8);
head = add(head,6);
cout<<"Old List: ";
print(head);
node* newHead = copyList(head);
cout<<"New List: ";
print(newHead);
}
WHAT I HAVE DONE IS COPY A OLD LIST TO NEW LIST
OUTPUT IS :-
OLD LIST :- 5 6 6 8 9
NEW LIST :- 5 6 6 8 9
Assuming: struct node { int data; node * next; node * tail; }; and copyList(node *...
In C++ Assuming: struct node { int data; node * next; }; and copyList(node * head); Write a recursive function (copyList(node * head)) that will create a copy of the singly linked list.
13. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list as the last node of the linked list struct node *insertLast(stuct node **head, int newId) { } 14. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list after a node with the same id as the input parameter...
Write a function with the following prototype struct node* copyList(struct node* 1ist) The struct node is the same used in question 18. The function should create a new, separate copy of the linked list pointed to by the list parameter and return a pointer to the head of the newly created linked list copy.
A linked list of integers is built using the following struct: struct node { int data; struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!
C Programming Explain what these function(s) do: struct thing{ int x; float y; }; struct node { struct thing *t; struct node *next; struct node *prev; }; struct dll{ struct node *head; struct node *tail; int length; }; struct thing *func( struct dll *list, int num) { struct node *curr = list ->head; while(curr != list ->tail) { if(curr ->t->x== num) return curr->t; curr=curr->next; } return NULL; }
Submit a source.cpp file with the following: struct node { int data; node* next; } a for loop that creates a singly linked list of nodes with the values 0-9 stored in them. a for loop that prints off the data of the singly linked list, each one on its own line ******************************************************************************************
Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...
Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...
Consider a Linked List program with the following class: typedef int datatype; struct node { datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...
C programming
Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;