Data structure
download pdfQue 1 and que 2
Both are same because edit if needed
Que 3 - program to create c
Code -
#include<iostream>
#include<string>
#include<iomanip>
#include<conio.h> #include<dos.h>
using namespace std; class Customer
private:
char name [20];
char telephone [12]; Customer *next;
public:
void createRecord (Customer**);
void delete Record (Customer**);
void displayRecord (Customer*);
};
void Customer::createRecord (Customer **START) {
*new Node, tempNode;
char cName [20];
char telNo[12];
cout<<"Enter customer name (Do not add spaces): ";
cin>>cName;
cout<<< "Enter customer telephone: ";
cin>>telNo;
new Node = new Customer;
if (new Node == NULL) {
cout<< "Insufficient Memory!\n";
} {
else
strcpy (new Node->name, cName);
strcpy (new Node->telephone, telNo);
new Node->next = NULL;
if (*START == NULL) {
*START= new Node;
else {
tempNode = *START;
(tempNode->next!= NULL) {
tempNode tempNode->next;
tempNode->next = new Node;
cout<< "New
customer is added successfully!";
void Customer::delete Record (Customer **START) ( Customer tempNode, *pNode;
char cName [20];
cout<<"Enter customer name to delete (Do not add spaces): "; cin>>cName;
if (*START == NULL) { cout<<"\nList is empty!";
else (
pNode = tempNode = *START;
int isFound = 0; while (tempNode != NULL) {
if (strcmpi (tempNode->name, cName) == 0) {
is Found = 1;
break;
pNode = tempNode;
tempNode tempNode->next; =
if (isFound == 0) {
cout<<<Name<<" name is not found!\n";
else {
pNode->next = tempNode->next;
cout<<"\n" <<tempNode->name <<< " is delete d!\n";
delete tempNode;
void Customer::displayRecord (Customer *START) {
Customer tempNode;
if (START == = NULL) {
cout<<< "List is empty!\n";
else { tempNode = START;
cout<<setw(10) << "NAME" <<setw (20) << "TELEPHONE NO" << "\n";
while (tempNode != NULL) {
cout<<setw(10) <<tempNode->name <<setw (20) <<tempNode- >telephone << "\n";
tempNode = tempNode->next;
0;
default:
break;
break; case 3:
case 2:
break;
switch (choice) {
case 1:
system("CLS");
}
Customer cList; int choice;
while (1) {
cout<<"2. Delete Customer\n";
cout<<"3. Display all Customer\n";
cout<<"1. Add New Customer\n";
Customer *START = NULL;
cout<< "4. Exit\n"; cout<<"Enter your choice: ";
cin>> choice;
if (choice == 4) {
break;
cList.createRecord (&START);
cList.delete Record (&START);
cList.displayRecord (START);
cout<<"\nInvalid Choice!";
getch();
System ("cls");
}
return 0;
}
Que 4 - program to reverse the linked list
Code -
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node * next;
Node (int arg_data) : data (arg_data), next (nullptr)
{}
};
class SingleLinkedList {
public:
SingleLinkedList()
{}
void Display (Node * head) {
Node * current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
}
Node * Reverse (Node * head)
if (head == nullptr || head->next == nullptr)
return head;
Node * current = head;
Node * next_to_current = head->next;
head->next = nullptr;
while (next_to_current != nullptr) {
Node * temp = next_to_current->next;
next_to_current->next = current;
current = next_to_current;
next_to_current = temp;
}
return current;
}
void Free (Node *head) {
while (head != nullptr) {
Node * temp = head->next;
head->next = nullptr;
delete head;
head = temp;
}
}
};
int main() {
Node * head = new Node(2);
Node * node_3 = new Node(3);
Node * node_5 = new Node(5);
Node * node_7 = new Node(7);
Node * node_11 = new Node(11);
Node * node_13 = new Node(13);
head->next = node_3;
node_3->next = node_5;
node_5->next = node_7;
node_7->next = node_11;
node_11->next = node_13;
SingleLinkedList s;
cout <<"\nSingle linked list : " << endl;
s.Display(head);
cout <<"\nReversing the single linked list : " << endl;
head = s.Reverse(head);
s.Display(head);
s.Free(head);
return 0;
}
Que 6 program to insert a node at the beginning at the end and middle of dll
And
Que 7 program to insert a node at beginning and end of dll
Que 13 program to slove the tower of hanoi
#include<iostream>
using namespace std;
void TOH(int n,char Sour, char Aux,char Des)
{
if(n==1)
{
cout<<"Move Disk "<<n<<" from "<<Sour<<" to "<<Des<<endl;
return;
}
TOH(n-1,Sour,Des,Aux);
cout<<"Move Disk "<<n<<" from "<<Sour<<" to "<<Des<<endl;
TOH(n-1,Aux,Sour,Des);
}
int main()
{
int n;
cout<<"Enter no. of disks:";
cin>>n;
//calling the TOH
TOH(n,'A','B','C');
return 0;
}
Que 17 - program to sort an array using selection sort
#include<iostream>
using namespace std;
int main()
{
int tot, arr[50], i, j, temp, small, chk, index;
cout<<"Enter the Size of Array: ";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
for(i=0; i<(tot-1); i++)
{
chk=0;
small = arr[i];
for(j=(i+1); j<tot; j++)
{
if(small>arr[j])
{
small = arr[j];
chk++;
index = j;
}
}
if(chk!=0)
{
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
}
cout<<"\nSorted Array is:\n";
for(i=0; i<tot; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
Que 20 - program to traverse preorder of binary tree
#include<iostream>
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *createNode(int val) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
void preorder(struct node *root) {
if (root != NULL) {
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
struct node* insertNode(struct node* node, int val) {
if (node == NULL) return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}
int main() {
struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
insertNode(root, 3);
cout<<"Pre-Order traversal of the Binary Search Tree is: ";
preorder(root);
return 0;
}
Que 21 - program to traverse post order of binary tree
Code -
#include<iostream>
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *createNode(int val) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
void postorder(struct node *root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}
struct node* insertNode(struct node* node, int val) {
if (node == NULL) return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}
int main() {
struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
insertNode(root, 3);
cout<<"Post-Order traversal of the Binary Search Tree is: ";
postorder(root);
return 0;
}
Que 19- program to traverse inorder of binary tree
#include<iostream>
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *createNode(int val) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
struct node* insertNode(struct node* node, int val) {
if (node == NULL) return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}
int main() {
struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
insertNode(root, 3);
cout<<"In-Order traversal of the Binary Search Tree is: ";
inorder(root);
return 0;
}