Professional Documents
Culture Documents
Roll no:29
Practical No:3
cout<<"Element Inserted";
}
void pop()
{
if(top <0)
{
cout<<"stack is empty\n";
return;
}
cout<<"Deleted " <<stk[top--]<<endl;
}
void display()
{
if(top<0)
{
cout<<"stack empty \n";
return;
}
for(int i=top;i>=0;i--)
{
cout<<"Inserted Number are:-";
cout<<stk[i] <<" "<<endl;
}
}
};
DS Practical
Roll no:29
int main()
{
cout<<"Roll no 29";
int ch;
stack st; while(1)
{
cout <<"\n\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit\n\nEnter Your choice : ";
cin >> ch;
switch(ch)
{
case 1: cout <<"Enter the element : ";
cin >> ch; st.push(ch);
break;
case 2: st.pop();
break;
case 3: st.display();
break;
case 4:exit(0);
break;
}
}
return (0);
}
DS Practical
Roll no:29
OUTPUT:
struct Node
{
int data;
struct Node* link;
};
if (!temp)
{
cout << "\nHeap Overflow";
exit(1);
}
temp->data = data;
temp->link = top;
top = temp;
}
int isEmpty()
{
return top == NULL;
}
int peek()
{
if (!isEmpty())
return top->data;
else
exit(1);
}
void pop()
{
struct Node* temp;
if (top == NULL)
{
cout << "\nStack Underflow" << endl;
exit(1);
}
else
{
temp = top;
top = top->link;
temp->link = NULL;
free(temp);
}
}
void display()
{
struct Node* temp;
if (top == NULL)
DS Practical
Roll no:29
{
cout << "\nStack Underflow";
exit(1);
}
else
{
temp = top;
while (temp != NULL)
{
cout << temp->data << "-> ";
temp = temp->link;
}
}
}
int main()
{
push(11);
push(22);
push(33);
push(44);
display();
cout << "\nTop element is "
<< peek() << endl;
pop();
pop();
display();
cout << "\nTop element is "
<< peek() << endl;
return 0;
}
OUTPUT:
DS Practical
Roll no:29
};
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));
return stack;
}
int main()
{
char exp[] = "231*+9-";
cout<<"postfix evaluation: "<< evaluatePostfix(exp);
return 0;
}
OUTPUT:
DS Practical
Roll no:29
struct node
{
DS Practical
Roll no:29
int data;
struct node *next;
};
struct node *tmp=NULL;
struct node *tmp1=NULL;
struct node *top=NULL;
struct node *ptr=NULL;
int push(char x)
{
tmp = new node;
tmp->data=x;
tmp->next=NULL;
if(top == NULL)
{
top=tmp;
}
else
{
tmp1=top;
top=tmp;
tmp->next=tmp1;
}
}
char pop()
DS Practical
Roll no:29
{
if(top==NULL)
{
cout<<"Stack is empty.\n";
}
else
{
ptr=top;
top=top->next;
return(ptr->data);
delete(ptr);
}
}
int main()
{
int len,i;
char c,d,e;
char a[30];
cout<<"Enter expression :\n";
cin>>a;
len=strlen(a);
for(i=0;i<len;i++)
{
DS Practical
Roll no:29
case ']':
d=pop();
if(d=='{' || d=='(')
{
getch();
}
break;
case '}':
DS Practical
Roll no:29
e=pop();
if(e=='(' || e=='[')
{
getch();
}
break;
default:
cout<<"Enter the correct choice";
getch();
}
}
}
if(top==NULL)
cout<<"Balanced\n";
else
cout<<"Unbalanced\n";
getch();
return 0;
}
OUTPUT:
DS Practical
Roll no:29
DS Practical
Roll no:29