You are on page 1of 11

Assignment no: 3

Semester 3rd(Mor-B)
Class BS(SE)
Subject: Data Structure and Algorithms (CSI-401)
Submitted By: Muhammad salman Razzaq(4532)
Syed Saleeh Shah (4521)
Muhammad Umar Bashart (4539)
Ahmad Mujtaba (4535)
Submitted To: Dr.AWAIS
Question No. 1:
1…..
a. The Node must contain the following information in the DATA/INFO portion
i. Student Name
ii. Student Father Name
iii. Student Roll number
iv. Student Semester
v. Student CGPA
vi. Student GPA
vii. Student CNIC
……………………………………………………………………………………………………………………………………………
(a) …….
struct node
{
char name[5],father[5];
char semester[8];
int rollNo,CNIC;
float GPA,CGPA;
char section;
node *next;
}*head,*lastptr;
//(b) The user may search the List with any of the above mentioned
information and display the information of that node.
void search() //searches record of student//
{
node *prev=NULL;
node *current=NULL;
int CNIC;
cout<<"Enter CNIC to search:"<<endl;
cin>>CNIC;
prev=head;
current=head;
while(current->CNIC!=CNIC)
{
prev=current;
current=current->next;
}
cout<<"\nname: ";
puts(current->name);
cout<<"\nFather:";
puts(current->father);
cout<<"\nRoll_no:";
cout<<current->rollNo;
cout<<"\nSemester:";
puts(current->semester);
cout<<"\nSection:";
cout<<current->section;
cout<<"\nGPA:";
cout<<current->GPA;
cout<<"\nCGPA:";
cout<<current->CGPA;
getch();

//(c) The user can insert a new node with data at the end of the List

void add() //Adds record of student//


{
node *p;
p=new node;
cout<<"Enter name of student:"<<endl;
gets(p->name);
cout<<"enter father name:"<<endl;
gets(p->father);
cout<<"Enter semester of student:"<<endl;
gets(p->semester);
cout<<"Enter Roll Number of student:"<<endl;
cin>>p->rollNo;
cout<<"Enter section of student:"<<endl;
cin>>p->section;
cout<<"enter CNIC:"<<endl;
cin>>p->CNIC;
cout<<"enter GPA:"<<endl;
cin>>p->GPA;
cout<<"enter CGPA:"<<endl;
cin>>p->CGPA;

p->next=NULL;

if(check)
{
head = p;
lastptr = p;
check = false;
}
else
{
lastptr->next=p;
lastptr=p;
}
cout<<endl<<"Recored Entered";
getch();
}

d. The user can delete a node anywhere in the list


void del() //deletes record of a student//
{
node *ptr=NULL;
node *prev=NULL;
node *current=NULL;
int roll_no;
cout<<"Enter CNIC to Delete the record:"<<endl;
cin>>CNIC;
prev=head;
current=head;
while(current->CNIC)
{
prev=current;
current=current->next;
}
prev->next = current->next;
current->next=NULL;
delete current;
cout<<endl<<"Recored Deleted";
getch();
}
//(e) The user can display all the List
void display_list() // displays whole linked list
{

node * ptr = head;


do
{
if(ptr == NULL)
cout<<"No record"<<endl;
else
{
cout<<"Name: "<<ptr->name<<", ";
cout<<"Student dis: " << ptr->discipline<<endl;
cout<<"Roll no. " << ptr->rollNo<<endl;
cout<<"Section " << ptr->section<<endl;
cout<<endl << endl;
getch();
ptr = ptr->next;
}
}while(ptr != NULL);

Question NO: 2
as in the singly linked list, th doubly linked list also has a head and tail. The
previous pointer of the head is set to NULL as this is the first node. The next
pointer of the tail node is set to NULL as this is the last node
.

// (a) The DATA/INFO portion of a Node contains only One Character.


struct node{
int info;
struct node *next;
struct node *prev;
}*start
* Create Double Link List
*/
void double_llist::create_list(int value)
{
struct node *s, *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
if (start == NULL)
{
temp->prev = NULL;
start = temp;
}
else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}
}

//(b) A new Character can only be inserted while maintain the


Alphabetic sequence in the List
void double_llist::add_begin(int value)
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->prev = NULL;
temp->info = value;
temp->next = start;
start->prev = temp;
start = temp;
cout<<"Element Inserted"<<endl;
}

/*
* Insertion of element at a particular position
*/
void double_llist::add_after(int value, int pos)
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *tmp, *q;
int i;
q = start;
for (i = 0;i < pos - 1;i++)
{
q = q->next;
if (q == NULL)
{
cout<<"There are less than ";
cout<<pos<<" elements."<<endl;
return;
}
}
tmp = new(struct node);
tmp->info = value;
if (q->next == NULL)
{
q->next = tmp;
tmp->next = NULL;
tmp->prev = q;
}
else
{
tmp->next = q->next;
tmp->next->prev = tmp;
q->next = tmp;
tmp->prev = q;
}
cout<<"Element Inserted"<<endl;
}
//(c) A node can be deleted……
* Deletion of element from the list
*/
void double_llist::delete_element(int value)
{
struct node *tmp, *q;
/first element deletion/
if (start->info == value)
{
tmp = start;
start = start->next;
start->prev = NULL;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = start;
while (q->next->next != NULL)
{
/Element deleted in between/
if (q->next->info == value)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = q->next;
}
/last element deleted/
if (q->next->info == value)
{
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}
cout<<"Element "<<value<<" not found"<<endl;
}

// (d) WHOLE LIST….


/*
* Display elements of Doubly Link List
*/
void double_llist::display_dlist()
{
struct node *q;
if (start == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}
q = start;
cout<<"The Doubly Link List is :"<<endl;
while (q != NULL)
{
cout<<q->info<<" <-> ";
q = q->next;
}
cout<<"NULL"<<endl;
}

/*
* Number of elements in Doubly Link List
*/
void double_llist::count()
{
struct node *q = start;
int cnt = 0;
while (q != NULL)
{
q = q->next;
cnt++;
}
cout<<"Number of elements are: "<<cnt<<endl;
}

/*
//(e) USIN LINKED LIST FOR IMPLEMENTING…..
class Node
{
public:
string c;
string a;
int data;
int key;
Node* next;
Node* customer;
};

struct Node* newNode(string c)


{
Node* temp = new Node;
temp->c = c;
temp->next = NULL;
return temp;
}

struct Node* new_Node(string a)


{
Node* temp = new Node;
temp->a = a;
temp->customer = NULL;
return temp;
}

void printList(struct Node* head)


{
while (head != NULL) {
cout << head->c << " ";
head = head->next;
}
}

void printf(struct Node* start)


{
while (start != NULL) {
cout << start->a << " ";
start = start->customer;
}
}
void push(Node** head_ref, int new_key)
{
Node* new_node = new Node();
new_node->key = new_key;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

bool search(Node* head, int x)


{
Node* current = head;
while (current != NULL)
{
if (current->key == x )
return true;
current = current->next;
}
return false;
}
int main()
{
cout<<"List of BROKERS :\n";

Node* head = newNode(" 1.Bond\n");

head->next = newNode("2.Kelly\n");

head->next->next = newNode("3.Nelson\n");

head->next->next->next = newNode("4.Hall\n");
printList(head);

cout<<"Respective Query Numbers : BOND = 1" <<" KELLY = 2, NELSON = 3, HALL = 4 "<<endl;

cout<<"Enter QUUERY NUMBER of Broker: \n";


int x;
cin>>x;

push(&head, 1);
push(&head, 2);
push(&head, 3);
push(&head, 4);

search(head, x)? cout<<"Yes" : cout<<"No" ;


cout<<endl;

Node* start = new_Node("Customers:\n");


if (x==1) //start if
{
cout<<"Broker Name: Bond \n";
start->customer= new_Node("1.Grant 2.Scott 3.Vitto 4. Katz\n");
printf(start);
}
else if (x==2)
{
cout<<"Broker Name: Kelly";
start->customer= new_Node("1.Hunter 2.McBride 3.Evans \n");
printf(start);
}

else if (x==3)
{
cout<<"Broker Name: Neslson";
start->customer= new_Node("1.Teller 2.Jones 3.Adams 4.Rogers\n");
printf(start);
}
else
{
cout<<"Broker Name: Hall \n NO CUSTOMERS!";
} // end if
} //end main

You might also like