You are on page 1of 43

Answer:

#include <iostream>

using namespace std;

struct Item

int itemNo;

float price;

};

int main()

Item *itemPtrs[15];

//Insert

for (int index = 0; index <= 9; index++ )

itemPtrs[index] = new Item;

itemPtrs[index]->itemNo = index+1;

if(index == 0)

itemPtrs[index]->price = 1.89;

else if(index == 4)

itemPtrs[index]->price = 2.0;
}

else

float itemPrice;

cout<<"Enter the price of item "<<index+1<<" :"<<endl;

cin>>itemPrice;

itemPtrs[index]->price = itemPrice;

//Traverse

for (int index = 0; index <= 9; index++ )

cout <<"No: "<< itemPtrs[index]->itemNo<<" Price: "<< itemPtrs[index]->price << endl;

//Delete

int position;

cout<<"Enter delete position"<<endl;

cin>>position;

for (int index = position-1; index <= 9; index++)

itemPtrs[index]->itemNo = itemPtrs[index+1]->itemNo;

itemPtrs[index]->price = itemPtrs[index+1]->price;

}
//Traverse

for (int index = 0; index <= 8; index++ )

cout <<"No: "<< itemPtrs[index]->itemNo<<" Price: "<< itemPtrs[index]->price << endl;

return 0;

Output:

Qno2:

Answer:
(a)

first foor loop n times

If{

Second times n times // O(n)

C1

Third loop n times

C2

Forth loop n times

C3

Fifth loop

C4

(N1+c1.n2) * n3 * n4 * n5

O(n) worst case

O(1) best

(b)

First for loop (n)times {

Second while loop n times

Constant c 1;

Constant c 2;

Third for loop n times

C1 * n ;
C2

N + (c1*n)+c2+n

C2 +c1.n+2n

Worst case Time complexity is O(n)

Best case is O(c) =O(1)

(c )

For( I = 0;i<n*100;i++) // n times

For(j = n;j>0;j--) // n times

Sum ++; // c 1

For(k = 0;k<i;++k) // n times

Sum ++; // c 2

O(n^3) worst case

O(c) = O(1) best time

(d)

int main(int n , int m , int sum)

If{

Constant time

Else{

N times for loop

Constant times

}
O(1)+O(n)

Worst case is O(n) best case is O(1)

Qno3:

Answer:

#include <iostream>

using namespace std;

class node

public:

int data;

node *p;

};

node *head=NULL, *t=NULL;

void add_node()

node *temp = new node;

cin>>temp->data;

temp->p=NULL;

if(head==NULL)

head=t=temp;

else

t->p=temp;

t=t->p;

}
}

void showNode ()

for(t=head ; t!=NULL; t=t->p)

cout<<t->data<<" ";

void countNode (node *head)

int count=0;

for(t=head;t!=NULL;t=t->p)

if(t->data==-1)

count++;

cout<<-1<<" has appeared "<<count<<" times";

int main()

cout<<"Enter Node Details :"<<endl;

for (int i=1;i<=15;i++)

{
add_node();

cout<<"The list of data entered is here: "<<endl;

showNode();

cout<<endl;

countNode(head);

Output:

Qno4:

Answer:

Bubble Sort:
#include <iostream>

using namespace std;

class node

public:

int data;

node *p;

};

node *head=NULL, *t=NULL, *temp;

void add_node()

temp = new node;

cin>>temp->data;

temp->p=NULL;

if(head==NULL)

head=t=temp;

else

t->p=temp;

t=t->p;

void showNode ()

{
for(t=head;t!=NULL;t=t->p)

cout<<t->data<<" ";

void countNode (node *head)

int count=0;

for(t=head;t!=NULL;t=t->p)

if(t->data==5)

count++;

cout<<5<<" has appeared "<<count<<" times"<<endl;

void sort()

int nodeCtr, ctr,nodeDataCopy;

//node *currentNode, *firstNode;

for (nodeCtr=13;nodeCtr>=0;nodeCtr--)

temp=head;

t=temp->p;

for(ctr=0;ctr<=nodeCtr;ctr++)

if(temp->data > t->data)


{

nodeDataCopy= temp->data;

temp->data= t->data;

t->data=nodeDataCopy;

temp=t;

t=t->p;

int main()

cout<<"Enter Node Details :"<<endl;

for (int i=1;i<=15;i++)

add_node();

cout<<"The list of data entered is here: "<<endl;

showNode();

cout<<endl;

countNode(head);

cout<<"Now the sorted list is here "<<endl;

sort();

showNode();

Binary Search :
#include <iostream>

using namespace std;

class node

public:

int data;

node *p;

};

node *head=NULL, *t=NULL, *temp;

void add_node()

temp = new node;

cin>>temp->data;

temp->p=NULL;

if(head==NULL)

head=t=temp;

else

t->p=temp;

t=t->p;

void showNode ()

{
for(t=head;t!=NULL;t=t->p)

cout<<t->data<<" ";

void countNode (node *head)

int count=0;

for(t=head;t!=NULL;t=t->p)

if(t->data==-1)

count++;

cout<<-1<<" has appeared "<<count<<" times"<<endl;

void sort()

int nodeCtr, ctr,nodeDataCopy;

//node *currentNode, *firstNode;

for (nodeCtr=13;nodeCtr>=0;nodeCtr--)

temp=head;

t=temp->p;

for(ctr=0;ctr<=nodeCtr;ctr++)

if(temp->data > t->data)


{

nodeDataCopy= temp->data;

temp->data= t->data;

t->data=nodeDataCopy;

temp=t;

t=t->p;

node* middle ()

if (head == NULL)

return NULL;

node* slow = head;

node* fast = head ->p;

while (fast != NULL)

fast = fast -> p;

if (fast != NULL)

slow = slow -> p;

fast = fast -> p;

}
return slow;

void binarySearch()

node *start = head;

node* last = NULL;

node* mid = middle();

cout<<mid->data;

int main()

cout<<"Enter Node Details :"<<endl;

for (int i=1;i<=15;i++)

add_node();

cout<<"The list of data entered is here: "<<endl;

showNode();
cout<<endl;

countNode(head);

cout<<"Now the sorted list is here "<<endl;

sort();

showNode();

cout<<endl;

cout<<"Element present at index 8 is ";

binarySearch();

Append:

#include <iostream>

using namespace std;

class node

public:

int data;

node *p;

};

node *head=NULL, *t=NULL, *temp;

void add_node()

temp = new node;

cin>>temp->data;

temp->p=NULL;

if(head==NULL)

head=t=temp;
}

else

t->p=temp;

t=t->p;

void showNode ()

for(t=head;t!=NULL;t=t->p)

cout<<t->data<<" ";

void countNode (node *head)

int count=0;

for(t=head;t!=NULL;t=t->p)

if(t->data==-1)

count++;

cout<<-1<<" has appeared "<<count<<" times"<<endl;

void sort()
{

int nodeCtr, ctr,nodeDataCopy;

//node *currentNode, *firstNode;

for (nodeCtr=13;nodeCtr>=0;nodeCtr--)

temp=head;

t=temp->p;

for(ctr=0;ctr<=nodeCtr;ctr++)

if(temp->data > t->data)

nodeDataCopy= temp->data;

temp->data= t->data;

t->data=nodeDataCopy;

temp=t;

t=t->p;

node* middle ()

if (head == NULL)

return NULL;

node* slow = head;

node* fast = head ->p;


while (fast != NULL)

fast = fast -> p;

if (fast != NULL)

slow = slow -> p;

fast = fast -> p;

return slow;

void binarySearch()

node *start = head;

node* last = NULL;

node* mid = middle();

cout<<mid->data;

}
void append(node** head_ref)

temp = new node;

node *last = *head_ref;

cout<<"Append data at the end Node ";

cin>>temp->data;

temp->p=NULL;

while (last->p != NULL)

last = last->p;

last->p = temp;

return;

int main()

cout<<"Enter Node Details :"<<endl;

for (int i=1;i<=15;i++)

add_node();

cout<<"The list of data entered is here: "<<endl;

showNode();

cout<<endl;

countNode(head);

cout<<"Now the sorted list is here "<<endl;

sort();
showNode();

cout<<endl;

cout<<"Element present at index 8 is ";

binarySearch();

cout<<endl;

append(&head);

cout<<"After Appending "<<endl;

showNode();

(a) Add node between 10 and 11 index

Answer:

#include <iostream>

using namespace std;

class node

public:

int data;

node *p;

};

node *head=NULL, *t=NULL, *temp;

void add_node()

temp = new node;

cin>>temp->data;

temp->p=NULL;

if(head==NULL)

{
head=t=temp;

else

t->p=temp;

t=t->p;

void showNode ()

for(t=head;t!=NULL;t=t->p)

cout<<t->data<<" ";

void countNode (node *head)

int count=0;

for(t=head;t!=NULL;t=t->p)

if(t->data==-1)

count++;

cout<<-1<<" has appeared "<<count<<" times"<<endl;

}
void sort()

int nodeCtr, ctr,nodeDataCopy;

//node *currentNode, *firstNode;

for (nodeCtr=13;nodeCtr>=0;nodeCtr--)

temp=head;

t=temp->p;

for(ctr=0;ctr<=nodeCtr;ctr++)

if(temp->data > t->data)

nodeDataCopy= temp->data;

temp->data= t->data;

t->data=nodeDataCopy;

temp=t;

t=t->p;

node* middle ()

if (head == NULL)

return NULL;

node* slow = head;


node* fast = head ->p;

while (fast != NULL)

fast = fast -> p;

if (fast != NULL)

slow = slow -> p;

fast = fast -> p;

return slow;

void binarySearch()

node *start = head;

node* last = NULL;

node* mid = middle();

cout<<mid->data;

}
void append(node** head_ref)

temp = new node;

node *last = *head_ref;

cout<<"Append data at the end Node ";

cin>>temp->data;

temp->p=NULL;

while (last->p != NULL)

last = last->p;

last->p = temp;

return;

void insertPos(node** current, int pos)

// This condition to check whether the

// position given is valid or not.

if (pos < 1 || pos > 16 + 1)

cout << "Invalid position!" << endl;

else {

// Keep looping until the pos is zero

while (pos--) {

if (pos == 0) {

temp = new node;


cin>>temp->data;

temp->p=NULL;

// Making the new Node to point to

// the old Node at the same position

temp->p = *current;

// Changing the pointer of the Node previous

// to the old Node to point to the new Node

*current = temp;

else

// Assign double pointer variable to point to the

// pointer pointing to the address of next Node

current = &(*current)->p;

int main()

cout<<"Enter Node Details :"<<endl;

for (int i=1;i<=15;i++)

add_node();

}
cout<<"The list of data entered is here: "<<endl;

showNode();

cout<<endl;

countNode(head);

cout<<"Now the sorted list is here "<<endl;

sort();

showNode();

cout<<endl;

cout<<"Element present at index 8 is ";

binarySearch();

cout<<endl;

append(&head);

cout<<"After Appending "<<endl;

showNode();

cout<<endl;

cout<<"Insert Node between 10th and 11th node : ";

int pos=11;

insertPos(&head, pos);

showNode();

(b) Removing the Head Node

Answer:

#include <iostream>

using namespace std;

class node

public:
int data;

node *p;

};

node *head=NULL, *t=NULL, *temp;

void add_node()

temp = new node;

cin>>temp->data;

temp->p=NULL;

if(head==NULL)

head=t=temp;

else

t->p=temp;

t=t->p;

void showNode ()

for(t=head;t!=NULL;t=t->p)

cout<<t->data<<" ";

}
void countNode (node *head)

int count=0;

for(t=head;t!=NULL;t=t->p)

if(t->data==-1)

count++;

cout<<-1<<" has appeared "<<count<<" times"<<endl;

void sort()

int nodeCtr, ctr,nodeDataCopy;

//node *currentNode, *firstNode;

for (nodeCtr=13;nodeCtr>=0;nodeCtr--)

temp=head;

t=temp->p;

for(ctr=0;ctr<=nodeCtr;ctr++)

if(temp->data > t->data)

nodeDataCopy= temp->data;

temp->data= t->data;

t->data=nodeDataCopy;

temp=t;

t=t->p;
}

node* middle ()

if (head == NULL)

return NULL;

node* slow = head;

node* fast = head ->p;

while (fast != NULL)

fast = fast -> p;

if (fast != NULL)

slow = slow -> p;

fast = fast -> p;

return slow;

void binarySearch()

{
node *start = head;

node* last = NULL;

node* mid = middle();

cout<<mid->data;

void append(node** head_ref)

temp = new node;

node *last = *head_ref;

cout<<"Append data at the end Node ";

cin>>temp->data;

temp->p=NULL;

while (last->p != NULL)

last = last->p;

last->p = temp;

return;

void insertPos(node** current, int pos)

{
// This condition to check whether the

// position given is valid or not.

if (pos < 1 || pos > 16 + 1)

cout << "Invalid position!" << endl;

else {

// Keep looping until the pos is zero

while (pos--) {

if (pos == 0) {

temp = new node;

cin>>temp->data;

temp->p=NULL;

// Making the new Node to point to

// the old Node at the same position

temp->p = *current;

// Changing the pointer of the Node previous

// to the old Node to point to the new Node

*current = temp;

else

// Assign double pointer variable to point to the

// pointer pointing to the address of next Node

current = &(*current)->p;

}
}

node* removeFirstNode(node* head)

if (head == NULL)

return NULL;

// Move the head pointer to the next node

node* temp = head;

head = head->p;

delete temp;

return head;

int main()

cout<<"Enter Node Details :"<<endl;

for (int i=1;i<=15;i++)

add_node();

cout<<"The list of data entered is here: "<<endl;

showNode();

cout<<endl;
countNode(head);

cout<<"Now the sorted list is here "<<endl;

sort();

showNode();

cout<<endl;

cout<<"Element present at index 8 is ";

binarySearch();

cout<<endl;

append(&head);

cout<<"After Appending "<<endl;

showNode();

cout<<endl;

cout<<"Insert Node between 10th and 11th node : ";

int pos=11;

insertPos(&head, pos);

showNode();

cout<<endl;

cout<<"Removing the Head Node "<<endl;

head = removeFirstNode(head);

showNode();

(c ) Delete 4rth node from the list:

Answer:

#include <iostream>

using namespace std;

class node

{
public:

int data;

node *p;

};

node *head=NULL, *t=NULL, *temp;

void add_node()

temp = new node;

cin>>temp->data;

temp->p=NULL;

if(head==NULL)

head=t=temp;

else

t->p=temp;

t=t->p;

void showNode ()

for(t=head;t!=NULL;t=t->p)

cout<<t->data<<" ";

}
}

void countNode (node *head)

int count=0;

for(t=head;t!=NULL;t=t->p)

if(t->data==-1)

count++;

cout<<-1<<" has appeared "<<count<<" times"<<endl;

void sort()

int nodeCtr, ctr,nodeDataCopy;

//node *currentNode, *firstNode;

for (nodeCtr=13;nodeCtr>=0;nodeCtr--)

temp=head;

t=temp->p;

for(ctr=0;ctr<=nodeCtr;ctr++)

if(temp->data > t->data)

nodeDataCopy= temp->data;

temp->data= t->data;

t->data=nodeDataCopy;

temp=t;
t=t->p;

node* middle ()

if (head == NULL)

return NULL;

node* slow = head;

node* fast = head ->p;

while (fast != NULL)

fast = fast -> p;

if (fast != NULL)

slow = slow -> p;

fast = fast -> p;

return slow;

void binarySearch()
{

node *start = head;

node* last = NULL;

node* mid = middle();

cout<<mid->data;

void append(node** head_ref)

temp = new node;

node *last = *head_ref;

cout<<"Append data at the end Node ";

cin>>temp->data;

temp->p=NULL;

while (last->p != NULL)

last = last->p;

last->p = temp;

return;

void insertPos(node** current, int pos)


{

// This condition to check whether the

// position given is valid or not.

if (pos < 1 || pos > 16 + 1)

cout << "Invalid position!" << endl;

else {

// Keep looping until the pos is zero

while (pos--) {

if (pos == 0) {

temp = new node;

cin>>temp->data;

temp->p=NULL;

// Making the new Node to point to

// the old Node at the same position

temp->p = *current;

// Changing the pointer of the Node previous

// to the old Node to point to the new Node

*current = temp;

else

// Assign double pointer variable to point to the

// pointer pointing to the address of next Node

current = &(*current)->p;

}
}

node* removeFirstNode(node* head)

if (head == NULL)

return NULL;

// Move the head pointer to the next node

node* temp = head;

head = head->p;

delete temp;

return head;

//Function for finding the node

node *findNodeInLL()

node* index = 0;

temp = head;

//LinkedList traversal for finding the node

for(int i=1;i<3;i++)

temp = temp->p;
}

index=temp->p;

return index;

void deleteNode(node* pos)

t=head;

for(int i=1;i<5;i++)

t= t->p;

temp=t;

delete pos;

t=head;

for(int i=1;i<=2;i++)

t= t->p;

t->p=temp;

int main()

cout<<"Enter Node Details :"<<endl;

for (int i=1;i<=15;i++)

{
add_node();

cout<<"The list of data entered is here: "<<endl;

showNode();

cout<<endl;

countNode(head);

cout<<"Now the sorted list is here "<<endl;

sort();

showNode();

cout<<endl;

cout<<"Element present at index 8 is ";

binarySearch();

cout<<endl;

append(&head);

cout<<"After Appending "<<endl;

showNode();

cout<<endl;

cout<<"Insert Node between 10th and 11th node : ";

int pos=11;

insertPos(&head, pos);

showNode();

cout<<endl;

cout<<"Removing the Head Node "<<endl;

head = removeFirstNode(head);

showNode();

cout<<endl;

cout<<"After Deleting node at 4th index :";

node* l=findNodeInLL();

deleteNode(l);
showNode();

(d ) Answer:

Output:

You might also like