0% found this document useful (0 votes)
7 views21 pages

Part 1

Uploaded by

mhaskemayur3092
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)
7 views21 pages

Part 1

Uploaded by

mhaskemayur3092
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/ 21

Name: Mayur Maruti Mhaske batch – A3 roll no – 23135

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 :

Name: Mayur Maruti Mhaske batch – A3 roll no – 23135


Name: Mayur Maruti Mhaske batch – A3 roll no – 23135
Program:
#include<iostream>
#include<string.h>
#define max 50 using
namespace std;
class STACK
{
private: char
a[max];
int top;

public:
STACK()
{
top=-1;
}

void push(char); void


reverse(); void
convert(char[]);
void palindrome();
};
void STACK::push(char c)
{
top++; a[top]
= c;
a[top+1]='\0';
cout<<endl<<c<<" is pushed on stack ...";
}
void STACK::reverse()
{
char str[max];
cout<<"\n\nReverse string is : ";
for(int i=top,j=0; i>=0; i--,j++)
{
cout<<a[i]; str[j]=a[i];
}
cout<<endl<<endl;
}
void STACK::convert(char str[])
{
int j,k,len = strlen(str);
for(j=0, k=0; j<len; j++)
{
if( ( (int)str[j] >= 97 && (int)str[j] <=122 ) || ( (int)str[j] >= 65 && (int)str[j] <=90 ))
{
if( (int)str[j] <=90 )
{
str[k] = (char)( (int)str[j] + 32 );
}else
{
str[k] = str[j];
}
k++;
}
}
str[k]='\0';
cout<<endl<<"Converted String : "<<str<<"\n";
}
void STACK::palindrome()
{
char str[max];
int i,j;
for(i=top,j=0; i>=0; i--,j++)
{
str[j]=a[i];
}
str[j]='\0'; if(strcmp(str,a)
== 0)
cout<<"\n\nString is palindrome..."; else
cout<<"\n\nString is not palindrome...";
}
int main()
{
cout<<" Name: Mayur Maruti Mhaske batch – A3 roll no – 23135 "<<endl;
STACK stack; char
str[max]; int i=0;
cout<<"\nEnter string to be reversed and check is it palindrome or not : ";
cin.getline(str , 50); stack.convert(str);
while(str[i] != '\0')
{
stack.push(str[i]); i++;
}
stack.palindrome(); stack.reverse();
}
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;
#define size 10 class
stackexp
{ int top;
char stk[size];
public:
stackexp()
{
top=-1;
}
void push(char);
char pop(); int
isfull();
int isempty();
};
void stackexp::push(char x)
{
top=top+1;
stk[top]=x;
}
char stackexp::pop()
{
char s;
s=stk[top];
top=top-1; return
s;
}
int stackexp::isfull()
{
if(top==size)
return 1;
else
return 0;
}
int stackexp::isempty()
{
if(top==-1)
return 1;
else
return 0;
}
int main()
{
cout<<" Name: Mayur Maruti Mhaske batch – A3 roll no – 23135 "<<endl;
stackexp s1; char exp[20],ch; int i=0;
cout << "\n\t!! Parenthesis Checker !!" << endl;
cout<<"\nEnter the expression to check whether it is in well form or not : ";
cin>>exp;
if((exp[0]==')')||(exp[0]==']')||(exp[0]=='}'))
{
cout<<"\n Invalid Expression.....\n";
return 0;
}
else
{
while(exp[i]!='\0')
{
ch=exp[i];
switch(ch)
{
case '(':s1.push(ch);break;
case '[':s1.push(ch);break;
case '{':s1.push(ch);break;
case ')':s1.pop();break; case
']':s1.pop();break; case
'}':s1.pop();break;
}
i=i+1;
}
}
if(s1.isempty())
{
cout<<"\nExpression is well parenthesised...\n\n";
}
else
{
cout<<"\nSorry !!! Invalid Expression or not in well parenthesized....\n\n";
}
return 0;
}

Output :
Name: Mayur Maruti Mhaske batch – A3 roll no – 23135

Name: Mayur Maruti Mhaske batch – A3 roll no – 23135

Name: Mayur Maruti Mhaske batch – A3 roll no – 23135


Program:
#include <iostream>
#define MAX 10 using
namespace std; struct
queue
{
int data[MAX];
int front, rear;
};
class Queue
{
struct queue q;

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;
}

You might also like