Part 1
Part 1
Program:
#include <iostream>
#include <string.h> using
namespace std;
//Node struct
node {
int prn; string
name; struct node
*next;
};
//Linked list class list {
node *head, *temp;
public:
list() {
head = NULL;
}
node *create(int val, string n);
void insertEnd(); void
insertBeg(); void deleteAt(int
i); void insertAt(int i); void
display(); int count();
void concatenate(list A,list B);
void op();
};
//Create
node* list::create(int val, string n)
{
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory Allocation Failed!"<<endl;
return 0;
}
else
{
temp -> prn = val;
temp -> name = n;
temp -> next = NULL;
return temp;
}
}
//Insert End void
list::insertEnd() { int
val;
string n;
cout<<"Enter PRN: ";
cin>>val; cout<<"Enter
Name: "; cin>>n;
struct node *t = head;
temp = create(val,n);
if (head == NULL)
{
head = temp;
head -> next = NULL;
}
else
{
while ((t -> next) != NULL)
{
t = t -> next;
}
temp -> next = NULL;
t -> next = temp;
cout<<"Element Inserted at Last"<<endl;
}
}
//Insert At void
list::insertAt(int i)
{
int val,pos = i - 1,counter = 1;
string n; struct node *ptr;
struct node *t = head;
while ((t -> next) != NULL)
{
//loop to count number of items in linked list.
t = t -> next;
counter++;
}
t = head;
//traverse pointer is pointed to head again.
if (i == 1)
{
//equivalent to insert at start.
insertBeg();
}
else if (pos > counter || i <= 0)
{
//if position is greater than the actual linked list.
cout<<"Entered position is out of scope."<<endl;
}
else
{
//insert at required position.
cout<<"Enter PRN: ";cin>>val;
cout<<"Enter Name: "; cin>>n;
temp = create(val,n);
while (pos--)
{ ptr =
t; t = t ->
next;
}
temp -> next = t;
ptr -> next = temp;
cout<<"Member
Inserted at Position:
"<<i<<endl;
}
}
//Delete At
void list::deleteAt(int i)
{
int val,pos = i - 1,counter = 1;
string n; struct node
*ptrl,*ptrr; struct node *t =
head;
while ((t -> next) != NULL)
{
t = t -> next;
counter++;
}
t = head;
if (i == 1)
{
ptrl = head;
head = head -> next;
delete ptrl;
}
else if (pos > counter || i <= 0)
{
cout<<"Entered member doesn't exist."<<endl;
}
else
{
while (pos--)
{ ptrl = t;
t = t -> next;
ptrr = t -> next;
}
ptrl -> next = ptrr;
delete t;
cout<<"Member Deleted at Position: "<<i<<endl;
}
}
//Insert Beg
void list::insertBeg()
{ int val; string n;
cout<<"Enter PRN: ";
cin>>val; cout<<"Enter
Name: "; cin>>n; //v
= val; struct node *t =
head; temp =
create(val,n);
if (head == NULL)
{
head = temp;
head -> next = NULL;
}
else
{
temp -> next = head;
head = temp;
cout<<"We have a New President."<<endl;
}
}
//Display void
list::display() { temp
= head;
cout<<"President: ";
cout<< temp -> prn<<" — "<<temp -> name<<" -> ";
if(temp -> next != NULL)
{
temp = temp -> next;
}
while (temp -> next != NULL)
{
cout<< temp -> prn<<" — "<<temp -> name<<" -> ";
temp = temp -> next;
}
cout<<"Secretary: ";
cout<< temp -> prn<<" — "<<temp -> name<<" -> "; cout<<"NULL"<<endl;
}
//Count
int list::count()
{
temp = head;
int ct = 0;
while (temp != NULL)
{
ct++;
temp = temp -> next;
}
return ct;
}
//Concatenate
void list::concatenate(list A,list B)
{
struct node * last,*last1;
node* t = A.head;
while (t != NULL)
{
int val = t -> prn;
string n = t -> name;
temp = create(val,n);
if (head == NULL)
{
head = temp;head -> next = NULL;
last=head;
}
else
{
//temp -> next = NULL;
last -> next = t;
last=t;
}
t = t -> next;
}
last -> next = B.head;
t = B.head;
while (t != NULL)
{
int val = t -> prn;
string n = t -> name;
temp = create(val,n);
last -> next = temp;
last= temp;
t = t -> next;
}
last->next=NULL;
}
//Accept
void list::op()
{
while(1)
{
int choice;
cout<<"\nEnter: \n1. Add \n2. Delete \n3. Member's Count \n4. Display\n0. Prev Menu"<<endl;
cin>>choice; switch(choice)
{
case 1://Add
{
char c;
cout<<"\nEnter: \nA. Add President \nB. Add Secretary \nC.Add Member"<<endl;
cin>>c; switch(c) { case 'A': case 'a':
{
insertBeg();break;
}
case 'B':
case 'b':
{
insertEnd();break;
}
case 'C':
case 'c':
{
insertAt(2);break;
}
}
break;
}
case 2://Delete
{
char c;
cout<<"\nEnter: \nA. Delete President \nB. Delete Secretary\nC. Delete Member"<<endl;
cin>>c;
switch(c)
{
case 'A':
{
deleteAt(1);
cout<<"Club must have a President. Enter Details"<<endl;
insertBeg();
break;
}
case 'B':
{
deleteAt(count());
cout<<"Club must have a Secretary. Enter Details"<<endl;
insertEnd();
break;
}
case 'C':
{
int j;
cout<<"Enter Position for Deletion"<<endl;
cin>>j; deleteAt(j); break;
}
}
break;
}
case 3://Count
{
cout<<"Count: "<<count()<<endl;
break;
}
case 4://Display
{
if (head == NULL)
{
cout<<"NULL"<<endl;
break;
}
else
{
display();
break;
}
}
case 0://Prev Menu
{
return ;
}
}
}
}
//Main int
main() {
cout<<" Name: Mayur Maruti Mhaske batch – A3 roll no – 23135"<<endl; list
L,X,Y;
int c;
while(1)
{
cout<<"Enter: \n1. List A \n2. List B \n3. Concatenate\n0. Exit"<<endl;
cin>>c; switch(c)
{
case 1: cout<<"\nList A:"; X.op(); break;
case 2: cout<<"\nList B:"; Y.op(); break; case 3:
L.concatenate(X,Y); L.display(); break; case 0:
return 0;
}
}
}
Output :
Name: Mayur Maruti Mhaske batch – A3 roll no – 23135
Name: Mayur Maruti Mhaske batch – A3 roll no – 23135
Program:
#include <iostream> using
namespace std;
struct SLLNode *createSLL(int cnt, struct SLLNode *head);
void displaySLL(struct SLLNode *head); void A_U_B(); void
A_int_B(); void A_Min_B(); void B_Min_A(); void
U_Min_A_U_B();
struct SLLNode
{
char data; struct
SLLNode *next; } *headU,
*headA, *headB; int main()
{
cout<<" Name: Mayur Maruti Mhaske batch – A3 roll no – 23135”
<<endl; int i, no;
cout << "\n\n\t How many Linked Lists: ";
cin >> no;
headU = headA = headB = NULL;
for (i = 1; i <= no; i++)
{
if (i == 1)
{
cout << "\n\n\t Enter 10 Students of SE Comp : \n";
headU = createSLL(10, headU); cout << "\n";
displaySLL(headU);
}
if (i == 2)
{
cout << "\n\n\t Enter 5 Students who like Vanilla Icecreme: \n";
headA = createSLL(5, headA); cout << "\n";
displaySLL(headA);
}
if (i == 3)
{
cout << "\n\n\t Enter 5 Students who like Butterscotch Icecreme: \n";
headB = createSLL(5, headB); cout << "\n";
displaySLL(headB);
}
}
cout << "\n\n Input Sets:------------------------";
cout << "\n\n Set 'U': "; displaySLL(headU);
cout << "\n\n Set 'A': "; displaySLL(headA);
cout << "\n\n Set 'B': "; displaySLL(headB);
cout << "\n\n Output Sets:------------------------";
A_U_B();
A_int_B();
A_Min_B();
B_Min_A();
U_Min_A_U_B(); cout
<< "\n\n";
return 0;
}
struct SLLNode *createSLL(int cnt, struct SLLNode *head)
{
int i;
struct SLLNode *p, *newNode;
for (i = 0; i < cnt; i++)
{
newNode = new (struct SLLNode);
cout << "\t Enter Student Initial: ";
cin >> newNode->data; newNode-
>next = NULL; if (head == NULL)
{
head = newNode;
p = head;
}
else
{
p->next = newNode;
p = p->next;
}
}
return head;
}
void displaySLL(struct SLLNode *head)
{
cout<<"\t";
struct SLLNode *p;
p = head; while (p
!= NULL)
{
cout << " " << p->data;
p = p->next;
}
}
void A_U_B()
{
int i, j;
char a[10]; struct
SLLNode *p, *q;
i = 0;
// Index of Resultant Array p =
headA; // pointer to Set 'A' q
= headB; // pointer to Set 'B'
while (p != NULL && q != NULL)
{
if (p->data == q->data)
{
a[i] = p->data;
i++; p = p-
>next; q = q-
>next;
}
else
{
a[i] = p->data;
i++; p = p-
>next;
}
}
if (p == NULL)
{
while (q != NULL)
{
a[i] = q->data;
i++; q = q-
>next;
}
}
if (q == NULL)
{
while (p != NULL)
{
a[i] = p->data;
i++; p = p-
>next;
}
}
cout << "\n\n\t Set A U B: ";
for (j = 0; j < i; j++)
cout << " " << a[j];
}
void A_int_B()
{
int i, j;
char a[10]; struct
SLLNode *p, *q;
i = 0;
p = headA; while (p != NULL)
{
q = headB; // pointer to Set 'B'
while (q != NULL)
{
if (p->data == q->data)
{
a[i] = p->data;
i++;
}
q = q->next;
}
p = p->next;
}
cout << "\n\n\t Set A ^ B: ";
for (j = 0; j < i; j++)
cout << " " << a[j];
}
void A_Min_B()
{ int i, j, flag; char
a[10]; struct SLLNode
*p, *q;
i = 0;
p = headA; while (p != NULL)
{
flag = 0; q = headB; //
pointer to Set 'B' while (q !=
NULL)
{
if (p->data == q->data)
{
flag = 1;
}
q = q->next;
}
if (flag == 0)
{
a[i] = p->data;
i++;
}
p = p->next;
}
cout << "\n\n\t Set A - B: ";
for (j = 0; j < i; j++) cout
<< " " << a[j];
}
void B_Min_A()
{ int i, j, flag; char
a[10]; struct SLLNode
*p, *q;
i = 0;
q = headB;
while (q != NULL)
{
flag = 0; p = headA; //
pointer to Set 'A'
while (p != NULL)
{
if (q->data == p->data)
{
flag = 1;
}
p = p->next;
}
if (flag == 0)
{
a[i] = q->data;
i++;
}
q = q->next;
}
cout << "\n\n\t Set B - A: ";
for (j = 0; j < i; j++)
cout << " " << a[j];
}
void U_Min_A_U_B()
{ int i, j, flag; char a[10];
struct SLLNode *p, *q, *r;
i = 0;
// Index of Resultant Array
p = headU; while (p !=
NULL)
{
flag = 0; q = headA; //
pointer to Set 'A' r = headB; //
pointer to Set 'B'
while (q != NULL)
{
if (p->data == q->data)
{
flag = 1;
}
q = q->next;
}
while (r != NULL)
{
if (p->data == r->data)
{
flag = 1;
}
r = r->next;
}
if (flag == 0)
{
a[i] = p->data;
i++;
}
p = p->next;
}
cout << "\n\n\t Set U - (A U B): ";
for (j = 0; j < i; j++) cout << " "
<< a[j];
}
Output :
public:
STACK()
{
top=-1;
}
Output :
Name: Mayur Maruti Mhaske batch – A3 roll no – 23135
public:
Queue() { q.front = q.rear = -1; }
int isempty(); int isfull();
void enqueue(int);
int delqueue();
void display();
};
int Queue::isempty()
{
return (q.front == q.rear) ? 1 : 0;
}
int Queue::isfull()
{
return (q.rear == MAX - 1) ? 1 : 0;
}
void Queue::enqueue(int x)
{
q.data[++q.rear] = x;
}