0% found this document useful (0 votes)
42 views

2 DS Assignment-2

The document contains code for implementing and working with linked lists in C++. It includes: 1) Defining node and list classes to create and manipulate singly linked lists, including functions to add nodes, display the list, rearrange nodes, and find the middle element. 2) A similar implementation for linked lists of characters, with additional functions to join a specified number of consecutive letters when displaying the list. 3) Examples of running the code to create lists, display and modify them, and output the results.

Uploaded by

tejas thorat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

2 DS Assignment-2

The document contains code for implementing and working with linked lists in C++. It includes: 1) Defining node and list classes to create and manipulate singly linked lists, including functions to add nodes, display the list, rearrange nodes, and find the middle element. 2) A similar implementation for linked lists of characters, with additional functions to join a specified number of consecutive letters when displaying the list. 3) Examples of running the code to create lists, display and modify them, and output the results.

Uploaded by

tejas thorat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Name- Tejas Rajendra Thorat Roll no.

2 Batch-A1
Course-Data Structures
Assignment 2
//Q.1

#include<iostream>
using namespace std;

class node
{

public:
int data;
node *next;
};

class SLlist
{
public:
int length ;
node *head;
SLlist()
{
head = NULL;
}
void create();
void display();
void rearrange();
void fun ( node *temp) ;
void getmiddle();
};

void SLlist :: create()


{
int size,data ;
node *ptr;

cout<<"\nEnter the number of employees : ";


cin>>size;

length = size ;
for (int i=1 ; i<=size ; i++)
{
cout<<"\nEnter "<<i<<" Employee's payment : Rs. ";
cin>>data;

node *new_node = new node ;

new_node->data = data ;
new_node->next = NULL ;
if(head == NULL)
{
head = new_node;
ptr = new_node;
}
else
{
ptr->next = new_node ;
ptr = new_node;
}
}

void SLlist ::display()


{

node *temp;
int i=1;
temp = head ;

cout<<"\n\n KOHINOOR Company Employee's payment \n";

while(temp != NULL)
{
cout<<i<<" Employee payment : Rs. "<<temp->data<<endl;
i++;
temp = temp->next;
}
}

void SLlist::rearrange()
{

float sorted_payment;

node *prev , *cur ;


prev = head ;

while(prev->next != NULL)
{

cur = prev->next ;
while(cur != NULL)
{

if(prev->data > cur->data)


{
sorted_payment = prev->data;
prev->data = cur->data ;
cur->data = sorted_payment ;
}
cur=cur->next ;
}
prev = prev->next ;
}
}

void SLlist::getmiddle()
{
node *temp=NULL;
int count1=0;
temp=head;

while(temp!=NULL)
{
temp=temp->next;
count1++;
}

cout<<"\n\n Total employees in KOHINOOR company are: "<<count1;


temp=head;

if(count1%2==1)
{
count1=count1/2;
for(int i=0;i<count1;i++)
{
temp=temp->next;
}

cout<<"\n\n Employee "<<count1+1<<" is at middle Position and


payment is Rs. "<<temp->data;
}

else
{
count1=count1/2;

for(int i=1;i<count1;i++)
{
temp=temp->next;
}
cout<<"\n Employees "<<count1<<" and "<<count1+1<<" are at
middle Position and payments are Rs. "<<temp->data<<" and Rs. "
<<temp->next->data<<" respectively"<<endl;
}
}

int main()
{
SLlist sl;

int choice , pos , info ;


char c = 'y';

while(c == 'y'){

cout<<"\n1.Create \n2.Display \n3.Sort \n4.Get middle\n";


cout<<"Enter your choice: ";
cin>>choice;

switch(choice)
{
case 1 :
sl.create() ;
break;

case 2 :
sl.display();
break;

case 3 :
sl.rearrange();
sl.display();
break;
case 4 :
sl.getmiddle();
break;

default :
cout<<"\nWrong choice";
}

cout<<"\n\nDo you want to continue (y/n)? : ";


cin>>c;
}

/*
Output-

1.Create
2.Display
3.Sort
4.Get middle
Enter your choice: 1

Enter the number of employees : 7


Enter 1 Employee's payment : Rs. 80000

Enter 2 Employee's payment : Rs. 40000

Enter 3 Employee's payment : Rs. 60000

Enter 4 Employee's payment : Rs. 20000

Enter 5 Employee's payment : Rs. 70000

Enter 6 Employee's payment : Rs. 50000

Enter 7 Employee's payment : Rs. 90000

Do you want to continue (y/n)? : y

1.Create
2.Display
3.Sort
4.Get middle
Enter your choice: 2

KOHINOOR Company Employee's payment


1 Employee payment : Rs. 80000
2 Employee payment : Rs. 40000
3 Employee payment : Rs. 60000
4 Employee payment : Rs. 20000
5 Employee payment : Rs. 70000
6 Employee payment : Rs. 50000
7 Employee payment : Rs. 90000

Do you want to continue (y/n)? : y

1.Create
2.Display
3.Sort
4.Get middle
Enter your choice: 3

KOHINOOR Company Employee's payment


1 Employee payment : Rs. 20000
2 Employee payment : Rs. 40000
3 Employee payment : Rs. 50000
4 Employee payment : Rs. 60000
5 Employee payment : Rs. 70000
6 Employee payment : Rs. 80000
7 Employee payment : Rs. 90000
Do you want to continue (y/n)? : y

1.Create
2.Display
3.Sort
4.Get middle
Enter your choice: 4

Total employees in KOHINOOR company are: 7

Employee 4 is at middle Position and payment is Rs. 60000

Do you want to continue (y/n)? :


*/
//Q.2
#include<iostream>
using namespace std;

class node
{
public:
char letter;
node * next;
};

class Llist
{
private:
node *head;

public:
Llist()
{
head=NULL;
}
void create_List();
void display_List();
void join();

};
void Llist:: create_List()
{
node * temp= NULL;
node * p=NULL;
char c='y';

while(c=='y')
{
temp=new node;
temp->next=NULL;

cout<<"\nEnter the Letter: ";


cin>>temp->letter;
if(head==NULL)
{
head=temp;
head->next=NULL;
p=head;
}

else
{
p->next=temp;
p=temp;
}

cout<<"\n Do you want to continue(y/n)? : ";


cin>>c;
}
}

void Llist::display_List()
{
node *p=NULL;
p= head;
cout<<"\n\nGiven Linked List :"<<endl;

while(p!=NULL)
{
cout<<p->letter<<"->";
p=p->next;
}
cout<<"NULL";
}

void Llist::join()
{
int num;
cout<<"\nEnter the number of letters (n): ";
cin>>num;
cout<<"\nJoining "<<num<<" Consecutive Letters:"<<endl;
node *temp=NULL;
temp=head;

while(temp!=NULL)
{
for(int i=0;i<num;i++)
{
if(temp==NULL)
{
break;
}

else
{
cout<<temp->letter;
temp=temp->next;
}
}
cout<<" ";
}
}
int main()
{
Llist l1;
l1.create_List();
l1.display_List();
l1.join();
return 0;
}

/*
Output-

Enter the Letter: c

Do you want to continue(y/n)? : y

Enter the Letter: a

Do you want to continue(y/n)? : y

Enter the Letter: t

Do you want to continue(y/n)? : y

Enter the Letter: m

Do you want to continue(y/n)? : y


Enter the Letter: o

Do you want to continue(y/n)? : y

Enter the Letter: b

Do you want to continue(y/n)? : y

Enter the Letter: h

Do you want to continue(y/n)? : y

Enter the Letter: e

Do you want to continue(y/n)? : y

Enter the Letter: n

Do you want to continue(y/n)? : n

Given Linked List :


c->a->t->m->o->b->h->e->n->NULL
Enter the number of letters (n): 3

Joining 3 Consecutive Letters:


cat mob hen
--------------------------------
Process exited after 27.39 seconds with return value 0
Press any key to continue . . .
*/
Q.3
Q.4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy