You are on page 1of 23

Name: Muhammad Muneeb

Roll no. (Bsem-f19-200)

Section: 3D

Presented to: Sir Muhammad Ahmad

Course: Data Structure and Algorithms

Assignment #2
Question no.1
Singly link list code

#include <iostream>
#include<conio.h>
using namespace std;

struct node
{
int data;
node *next;
};

class link_list
{
private:
node *head, *tail;
public:
link_list()
{
head=NULL;
tail=NULL;
}

void create_new_node(int value)


{

node *temp=new node();


temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=tail->next;
}
}
void Add_first(int value)
{

node *temp=new node();


temp->data=value;
temp->next=head;
head=temp;
}
void Add_at_end(int value)
{
node *temp=new node();
temp->data=value;
temp->next=NULL;

tail->next=temp;
tail=tail->next;

}
void Add_at_any_poistion(int value,int pos)
{
node *pre=new node();
node *cur=new node();
node *temp= new node();

cur=head;
for(int i=1; i<pos; i++)

{
pre=cur;
cur=cur->next;
}

temp->data=value;
pre->next=temp;
temp->next=cur;

}
void delete_position(int pos)
{
node *pre;
node *cur;

cur=head;
for(int i=1; i<pos; i++)
{
pre=cur;
cur=cur->next;
}
pre->next=cur->next;
}

void delete_at_first()
{
node *temp=head;
head=head->next;
}
void update_node(int old, int New)
{

node *temp;

temp=head;
while(temp->next!=NULL)
{
if(temp->data==old)
{
temp->data=New;
}
temp=temp->next;
}
}
bool search_node(int value)
{

node *temp;

temp=head;
while(temp->next!=NULL)
{

if(temp->data==value)
return true;

temp=temp->next;

}
return false;
}
void swap_nodes(int x,int y)
{
node *prex;
node *curx;

prex=NULL;
curx=head;

while(curx->data!=x)
{
prex=curx;
curx=curx->next;
}
node *PreY;
node *CurY;
PreY=NULL;
CurY=head;
while(CurY->data!=y)
{
PreY=CurY;
CurY=CurY->next;
}
if (prex != NULL)
prex->next = CurY;
else
head = CurY;

if (PreY != NULL)
PreY->next = curx;
else
head= curx;

node *temp = CurY->next;


CurY->next = curx->next;
curx->next = temp;
}
void count()
{int count=0;
node *temp;
temp=head;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
cout<<"total nodes are : "<<count;
}
void sort()
{

node * temp = head;

int temp_data;

int count = 0;
while (temp!=NULL)
{
temp = temp->next;
count++;
}
temp= head;

for (int j=0; j<count; j++)


{
while (temp->next)
{
if (temp->data > temp->next->data)
{

temp_data = temp->data;
temp->data = temp->next->data;
temp->next->data = temp_data;

temp = temp->next;
}
else
temp = temp->next;
}
temp=head;
}
}
void delete_end()
{
node *second_last;
node *cur;

cur=head;
while(cur->next!=NULL)
{
second_last=cur;
cur=cur->next;
}
second_last->next=NULL;

}
void display()
{
node *temp;
temp=head;
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
};

int main()
{
int choice,value,x,y,pre,New,position,flag=1;
link_list obj;
cout<<"\t :******************:"<<endl;
cout<<"\t :Singly Linked list:"<<endl;
cout<<"\t :******************:"<<endl;

while(flag==1)
{
cout<<"\n\nMenu"<<endl;
cout<<"\tPress 1 for Create a new node"<<endl;
cout<<"\tPress 2 for Display node"<<endl;
cout<<"\tPress 3 for Add a node at beginning"<<endl;
cout<<"\tPress 4 for Add a note at any end"<<endl;
cout<<"\tPress 5 for Add a note at any position"<<endl;
cout<<"\tPress 6 for Delete a node at beginning"<<endl;
cout<<"\tPress 7 for Delete a node at end"<<endl;
cout<<"\tPress 8 for Delete a note at any position"<<endl;
cout<<"\tPress 9 for Update a existing node"<<endl;
cout<<"\tPress 10 for Search an element from a node"<<endl;
cout<<"\tPress 11 for Swapping any two nodes"<<endl;
cout<<"\tPress 12 for Sorting nodes"<<endl;
cout<<"\tPress 13 for Counting no. of nodes"<<endl;
cout<<"Enter your choice ";
cin>>choice;
system("cls");
switch(choice)
{

case 1:
{
cout<<"Enter value to create a new node : ";
cin>>value;
obj.create_new_node(value);
break;
}
case 2:
{
cout<<"Displaying current list "<<endl;
obj.display();
break;
}
case 3:
{
cout<<"Enter value to add node at beginning : ";
cin>>value;
obj.Add_first(value);
break;
}
case 4:
{
cout<<"Enter value to add node at end : ";
cin>>value;
obj.Add_at_end(value);
break;
}
case 5:
{
cout<<"Enter value to add node at any position : ";
cin>>value;
cout<<"\nEnter position : ";
cin>>position;
obj.Add_at_any_poistion(value,position);
break;
}
case 6:
{
cout<<"Deleting node at beginning ";
obj.delete_at_first();
break;
}
case 7:
{
cout<<"Deleting node at end ";
obj.delete_end();
break;
}
case 8:
{
cout<<"\nEnter position : ";
cin>>position;
obj.delete_position(position);
break;
}
case 9:
{
cout<<"Enter the value to be updated : ";
cin>>pre;
cout<<"Enter the new value to be updated : ";
cin>>New;
obj.update_node(pre,New);
break;
}
case 10:
{
cout<<"Enter the Element to be searched : ";
cin>>value;
obj.search_node(value)? cout<<"Yes "<<value<<" is present in the
list " : cout<<"No invalid value";
break;
}
case 11:
{
cout<<"Enter values to be swapped : ";
cin>>x>>y;
obj.swap_nodes(x,y);
break;
}
case 12:
{
cout<<"Sorting the nodes";
obj.sort();
break;
}
case 13:
{
cout<<"Counting the nodes ";
obj.count();
break;
}

case 14:
{
cout<<"Invalid choice ";
system("pause");
break;
}

}
getch();

}
Question no.2
Stack and Queue Implementation in Singly
linked list

Queue:
#include <iostream>
#include<conio.h>
using namespace std;

struct node
{
int data;
node *next;
};

class link_list
{
private:
node *head, *tail;
public:
link_list()
{
head=NULL;
tail=NULL;
}

void Enqueue(int value)


{

node *temp=new node();


temp->data=value;
temp->next=head;
head=temp;
}
bool isempty()
{
if (head==NULL && tail==NULL)
return true;
else
return false;
}

void display()
{
if (isempty())
{
cout << "Queue is empty!\n";
}
else {

node *temp;
temp=head;
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
}
void dequeue()
{
node *temp=head;
head=head->next;
}
};

int main()
{
link_list obj;
int choice, flag = 1, value;
while (flag == 1)
{
cout << "\n1.Enqueue 2.Dequeue 3.DISPLAY_Queue 5.EXIT\n";
cin >> choice;
switch (choice)
{
case 1: cout << "Enter Value:\n";
cin >> value;
obj.Enqueue(value);
break;
case 2:

{
obj.dequeue();
}
case 3: obj.display();
break;
case 5: flag = 0;
break;
}
}
return 0;
}

Stack:
#include <iostream>
#include<conio.h>
using namespace std;

struct node
{
int data;
node *next;
};

class link_list
{
private:
node *head, *tail;
public:
link_list()
{
head=NULL;
tail=NULL;
}

void Enqueue(int value)


{
node *temp=new node();
temp->data=value;
temp->next=head;
head=temp;
}
bool isempty()
{
if (head==NULL && tail==NULL)
return true;
else
return false;
}

void display()
{
if (isempty())
{
cout << "Queue is empty!\n";
}
else {

node *temp;
temp=head;
while(temp!=NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
}
void dequeue()
{
node *temp=head;
head=head->next;
}
};

int main()
{
link_list obj;
int choice, flag = 1, value;
while (flag == 1)
{
cout << "\n1.Enqueue 2.Dequeue 3.DISPLAY_Queue 5.EXIT\n";
cin >> choice;
switch (choice)
{
case 1: cout << "Enter Value:\n";
cin >> value;
obj.Enqueue(value);
break;
case 2:

{
obj.dequeue();
}
case 3: obj.display();
break;
case 5: flag = 0;
break;
}
}
return 0;
}

You might also like