Question

C++ PROGRAM/ DLL • int count(node * head) recursively compute and return the number of nodes...

C++ PROGRAM/ DLL

• int count(node * head)
recursively compute and return the number of nodes in the doubly linked list.
• void insert(node * & head, int newInt, int position) recursively insert newInt at index “position” where index starts from 0
• int remove(node * & head, int position)
recursively remove the integer at index “position” and return it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<cstdlib>
using namespace std;

struct node{ //structure of Doubly Linked List
int data;
struct node *next, *prev;
};

//takes head pointer, newInt val and position of node in DLL
//insert function inserts node in the specified position
//if position is valid else inserts at the end of the DLL
void insert(node* &head, int newInt, int pos)
{
pos -= 1;
if(head==NULL){
node *p = new node;
p->data = newInt;
p->prev = p->next = NULL;
head = p;
return;
}
else if(pos==0){
node *p = new node;
p->data = newInt;
p->next = head;
head->prev = p;
p->prev = NULL;
head = p;
return;
}
else if(head->next==NULL||pos==1){
node *p = new node;
p->data = newInt;
p->prev = head;
p->next = head->next;
head->next = p;
if(head->next)
head->next->prev = p;
return;
}
insert(head->next,newInt,pos);
}

//remove function takes head and position of node to be removed
//if position is valid then removes the node from the DLL and returns the node value
//else return -1
int remove(node* &head,int pos)
{
pos -= 1;
if(head==NULL)
return -1;
else if(pos==1&&head->next){
int data = head->next->data;
node *t = head->next->next;
head->next = t;
if(t)
t->prev = head;
return data;
}
else if(pos==0){
int data = head->data;
if(head->next==NULL)
head = NULL;
else{
node *t = head->next;
t->prev = NULL;
head = head->next;
}
return data;
}
return remove(head->next,pos);
}

//count function takes head of DLL and returns number of nodes in DLL
int count(node *head)
{
if(head==NULL){
return 0;
}
return count(head->next)+1;
}

//print function takes head and prints the DLL nodes values
void print(node *head){
if(head==NULL){
cout<<"NULL";
return;
}
cout<<head->data<<"->";
print(head->next);
}

//main function implements the above functions
int main()
{
node *head = NULL;
//inserts 10,30,40,20,50 into DLL by calling insert function
insert(head,10,1);
insert(head,30,2);
insert(head,40,3);
insert(head,20,2);
insert(head,50,2);
print(head); //prints the DLL to console
//prints the value returned by count function
cout<<"\nCount of nodes: "<<count(head)<<endl<<endl;
//calls remove function to remove node at position 2.
int val = remove(head,2);
if(val!=-1) //if val is not -1 then node is successfully removed from DLL
cout<<val<<" removed from DLL"<<endl;

print(head);
cout<<"\nCount of nodes: "<<count(head)<<endl;
return 0;
}

//output screenshot

//any query, post in the comment section

Add a comment
Know the answer?
Add Answer to:
C++ PROGRAM/ DLL • int count(node * head) recursively compute and return the number of nodes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write program in C language. 2.Write a recursive int function to return the number of...

    Please write program in C language. 2.Write a recursive int function to return the number of even integers in a linked list. Each node in the list has an integer number (declared as int number) and a pointer to the next node (declared as NODE *next). The function is defined as int count (NODE *); and is called as follows: x = count(head);

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    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...

  • IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node;...

    IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node; typedef struct { int size; // Number of items on user’s list Node *head, *tail; } List; //In addition to creating an empty list, this function will also create two dummy nodes; one for the head, and the other for the tail. List* createList(); //This function creates a node containing the provided item, then inserts it into the list pointed by the provided list...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • (WRITE THIS PROGRAM IN C LANGUAGE).Before I ask the question, i'm just letting you know that...

    (WRITE THIS PROGRAM IN C LANGUAGE).Before I ask the question, i'm just letting you know that I asked this question before but I unfortunately the answer was wrong, in fact, it didn't even answer my questions. So please make an effort to answer this question. In this question, we will learn about a variant of linked list called \doubly Linked List". In addition to the \next" pointer pointing to the next node in the list, a node in a doubly...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT