Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Practical 4

Implement link list


Aim: write a program to insert a element in singly link list
Program:
#include <iostream>
#include<cstdlib>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct
Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display()
{
struct Node* ptr;
ptr = head;
while (ptr != NULL)
{
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main()
{
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The linked list is: ";
display();
return 0;
}
Output:
Practical 5A
Aim: Write a program to implement the concept of Stack with
Push, Pop, Display and Exit operations.
Program:
#include <iostream>
using namespace std;
int stack[100], n = 100, top = -1;
void push(int val)
{
if(top >= n-1)
cout<<"Stack Overflow"<<endl;
else
{
top++;
stack[top] = val;
}
}
void pop() {
if(top <= -1)
cout<<"Stack Underflow"<<endl;
else
{
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>= 0) {
cout<<"Stack elements are:";
for(int i = top; i>= 0; i--)
cout<<stack[i]<<" ";
cout<<endl;
}
else
cout<<"Stack is empty";
}
int main()
{
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do
{
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1:
{
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
cout<<"Exit"<<endl;
break;
}
default:
{
cout<<"Invalid Choice"<<endl;
}
}
}
while(ch! = 4);
return 0;
}
Output:
Practical 5B
Aim: Write a program to convert an infix expression to postfix
and prefix conversion.
Program:
#include<iostream>
#include<cstring>
using namespace std;
class stack
{
private:
int arr[5];
int top;
int size;

public:
stack()
{
top=-1;
size=5;
}
bool IsEmpty()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}

bool IsFull()
{
if(top==size-1)
{
return true;
}
else
{
return false;
}
}
void push(int data)
{
if(IsFull())
{
cout<<"STACK IS FULL..."<<endl;
}
else
{
top++;
arr[top]=data;
}
}

int pop()
{
if(IsEmpty())
{
cout<<"STACK IS EMPTY..."<<endl;
}
else
{
int c;
c=arr[top];
top--;
return c;
}
}

void show()
{
if(IsEmpty())
{
cout<<"STACK IS EMPTY..."<<endl;
}
else
{
for (int i=0;i<=top;i++)
{
cout<<"DATA ---- "<<arr[i]<<endl;
}
}
}

int getweight(char ch)


{
switch(ch)
{
case '/':
case '*': return 2;
case '+':
case '-': return 1;
default: return 0;
}
}
void in2p(char in[],char po[],int size)
{
int i=0,k=0;
int w;
char ch;

while(i<size)
{
ch=in[i];
w=getweight(ch);

if(w==0)
{
po[k++]=ch;
}
else
{
if(IsEmpty())
{
push(ch);
}

else
{
while(!IsEmpty() && w<=getweight(arr[top]))
{
po[k++]=arr[top];
pop();
}
push(ch);
}
}
i++;
}
while(!IsEmpty())
{
po[k]=arr[top];
k++;
pop();
}
po[k]=0;
}
};

int main()
{
char given[]="1+2*3";
int s=strlen(given);
char desired[s];
stack obj;
obj.in2p(given,desired,s);
cout<<"\n\n\n\t\t----------------------------------------------";
cout<<"\n\t\t| |";
cout<<"\n\t\t| INFIX TO POSTFIX EXPRESSION CONVERTER |";
cout<<"\n\t\t| |";
cout<<"\n\t\t----------------------------------------------\n";
cout<<"\t\tINFIX EXPRESSION = "<<given;
cout<<"\n\n\t\tPOSTFIX EXPRESSION = "<<desired<<endl<<endl;
system("pause");
};
Output:

You might also like