Open Ended Lab
Open Ended Lab
Roll No : FA20-BSE-021
Session : 2020-24
Title : Open Ended lab
Mirpur University of Science and
Technology
Program
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};
struct Node *first;
void insert(int n)
{
int i, temp; //temp here is for data
struct Node *second;
struct Node *ptr; //this is new node
if (n >= 1)
{
first = (struct Node *)malloc(sizeof(struct Node));
if (first != NULL)
{
cout << "node 1 : ";
scanf("%d", &temp);
first->data = temp;
first->prev = NULL;
first->next = NULL;
}
second = first;
for (i = 2; i <= n; i++)
{
ptr = (struct Node *)malloc(sizeof(struct Node));
if (ptr != NULL)
{
cout << "\nnode " << i << " : ";
scanf("%d", &temp);
ptr->data = temp;
ptr->prev = second; //here i linked new node with previous node
ptr->next = NULL;
int main()
{
int check;
cout << "\n|\t1) insert\t|\n|\t2) traverse\t|\tmake sure to insert data before
traversing and searching\n|\t3) search\t|" << endl;
cin >> check;
if (check == 1)
{
int no_of_nodes;
cout << "enter number of nodes : ";
cin >> no_of_nodes;
insert(no_of_nodes);
cout << "\n2) traverse\n3) search" << endl;
cin >> check;
}
if (check == 2)
{
traverse();
cout << "\n3) search" << endl;
cin >> check;
}
if (check == 3)
{
cout << "enter data to be searched : ";
int sch;
cin >> sch;
search(sch);
cout << "element present at index " << searchindex(sch);
}
return 0;
}