Assignment No. 2: Shantanu Arun Jambhule Roll No: 205 D-Div

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

SHANTANU ARUN JAMBHULE

ROLL NO : 205 D- DIV

Assignment No. 2
1 Write a program in C++ for counting the number of nodes present in a given singly linked list using
recursive function. 4M CO2 L3

2 Write a program in C++ to generate reverse of strings stored in a doubly linked list. Ex Input: abcd
---> wxyz----> pqrs Output: dcba ---> zyxw ---> srqp 8M CO1 L3

3 Evaluate the following given prefix expression * + 2 – 2 1 / - 4 2 + - 5 3 1 4M CO5 L2

4 The following sequence of operations are performed on stack: PUSH (10), PUSH (20),POP, PUSH
(10) ,PUSH (20) ,POP ,POP, POP, PUSH (20) ,POP Write the final content of stack with representation
of stack content

#include <bits/stdc++.h>

using namespace std;

class Node {

public:

int data;

Node* next;

};

void push(Node** head_ref, int new_data)

Node* new_node = new Node();

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

int getCount(Node* head)

if (head == NULL) {

return 0;
}

else {

return 1 + getCount(head->next);

int main()

Node* head = NULL;

push(&head, 1);

push(&head, 2);

push(&head, 3);

push(&head, 4);

push(&head, 5);

push(&head, 6);

push(&head, 7);

cout << "Count of nodes is " << getCount(head);

return 0;

OUTPUT :
#include <bits/stdc++.h>

using namespace std;

class Node

public:

int data;

Node *next;

Node *prev;

};

void reverse(Node **head_ref)

Node *temp = NULL;

Node *current = *head_ref;

while (current != NULL)

temp = current->prev;

current->prev = current->next;

current->next = temp;

current = current->prev;

if(temp != NULL )

*head_ref = temp->prev;

void push(Node** head_ref, int new_data)

Node* new_node = new Node();

new_node->data = new_data;

new_node->prev = NULL;

new_node->next = (*head_ref);

if((*head_ref) != NULL)

(*head_ref)->prev = new_node ;
(*head_ref) = new_node;

void printList(Node *node)

while(node != NULL)

cout << node->data << " ";

node = node->next;

int main()

Node* head = NULL;

push(&head, 123);

push(&head, 456);

push(&head, 789);

push(&head, 1011);

cout << "Original Linked list" << endl;

printList(head);

reverse(&head);

cout << "\nReversed Linked list" << endl;

printList(head);

return 0;

OUTPUT :

You might also like