Professional Documents
Culture Documents
AY 2016-2017(Even Semester)
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
2. (a) 5 Marks
Consider the following algorithm:
Algorithm fun1(x)
if(x<5)
return (3*x)
else
return (2* fun1(x-5)+7)
end if
end fun1
i) 12
ii) 21
iii) 45
Need Explanation for this result to award full marks
2.(b). Write a function that converts the given Infix expression to postfix notation. The
input to the function is an infix string consisting of a single alphabetic characters of
variables. parentheses, and the +, - ,*, and / operators. The function should print the
resulting postfix expression. 5 Marks
i) ENQUEUE
ii) DEQUEUE
void dequeue()
{
int item;
if(front == -1)
printf("\nCircular Queue is underflown.Cannot delete anything.");
else
{
item = cqueue[front];
if(front == rear)
{
front = -1;
rear = -1;
}
else
front=(front+1)%SIZE;
printf("\nThe deleted element is: %d", item);
}
}
3.b Evaluate the following postfix expression with single digit operands using a stack
and also implement a C-function for evaluation.
823/23*+51*-^
Note that ^ is the exponentiation operator.
Step 4
/ y=pop();
X=pop(); 0
Z=x/y=2/3=0 8
Push(z)
SYMBOL STACK SYMBOL STACK
2 2 3 3
0 2
8 0
Step 5 8 Step 6
--------------------------------------------------------------------------------------------------
Step 7
* y=pop();
X=pop(); 6
Z=x*y=2*3=6 0
Push(z) 8
------------------------------------------------------------------------------------------------------
Step 8
+ y=pop();
X=pop();
Z=x+y=0+6=6 6
Push(z) 8
-----------------------------------------------------------------------------------------------------
SYMBOL STACK SYMBOL STACK
5 5 1 1
6 5
8 6
Step 9 8 Step 10
-------------------------------------------------------------------------------------------
Step 11
* y=pop();
X=pop(); 5
Z=x*y=5*1=5 6
Push(z) 8
Step 12
- y=pop();
X=pop();
1
8
Z=x-y=6-5=1
Push(z)
----------------------------------------------------------------------------------------------------
Step 13
^ y=pop();
X=pop();
Z=x^y=8^1=8
Push(z) 8
4. (a) Write a function to insert an element into Deque using left insertion. 5 marks.
Note: Left side insert means, insert at front end of the circular Queue.
#include<stdio.h>
#define MAX 5
int queue[MAX],front=-1,rear=-1;
void insert_at_front()
{
int ele;
printf("\n Enter the number to be inserted: ");
scanf("%d",&ele);
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int stk[20];
int top =-1;
void push(int val)
{
stk[++top] = val;
}
int pop()
{
return(stk[top--]);
}
void main()
{
int op1,op2,len,result,i;
char postfix[30],op;
len=strlen(postfix);
switch(op)
{
case '+': push(op1+op2);
break;
case '-': push(op1-op2);
break;
case '*': push(op1*op2);
break;
case '/': push(op1/op2);
break;
case '%': push(op1%op2);
break;
}
}
}
result=pop();
printf("The evaluated value is = %d",result);
}
5. (a) Implement Deque by using the following operations: 5 Marks + 5 Marks
int queue[MAX],front=-1,rear=-1;
void enqueue_element()
{
int ele;
printf("\n Enter the number to be inserted: ");
scanf("%d",&ele);
Note: Left side delete means, delete at front end of the circular Queue. This is ordinary
dequeue operation of circular queue.
#include<stdio.h>
#define MAX 5
int queue[MAX],front=-1,rear=-1;
void dequeue_element()
{
int ele;
if(front==-1)
{
printf("\n Underflow");
return;
}
else
{
ele=queue[front];
if(front==rear)
front=rear=-1;
else if(front==MAX-1)
front=0;
else
front++;
printf("\n The deleted element is: %d",ele);
}
}
5. (b) Write the following functions to implement stacks using Linked list: 5 marks
(i) Push()
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*top=NULL, *p,*temp;
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
} *top=NULL, *p,*temp;
int pop ()
{ int ele;
p = top;
ele = p->data;
p->next = top;
free(p);
return (ele);
}
voidenqueue()
{
struct node *newnode,*temp;
intval,pri;
newnode=(struct node*)malloc(sizeof(struct node));
printf(“enter value and priority”);
scanf(“%d%d”,&val,&pri);
newnode->data=val;
newnode->priority=pri;
newnode->next=NULL;
if(start=NULL)
start=newnode;
else if(newnode->priority<start->priority)
{
newnode->next=start;
start=newnode;
}
else
{
temp=start;
while(temp->new!=NULL &&temp->next->priority<=newnode->priority)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
}
}
void dequeue()
{
struct node *temp;
printf(“%d is deleted”,start->data);
start=start->next;
free(temp);
}
6.b) 5 marks
void main()
{
chatstr[20];
printf(“enter line of text”);
gets(str);
for(i=0;str[i]!=’\0’;i++)
{
if(str[i]==’ ‘)
continue;
push(str[i]);
}
for(i=0;s.top!=-1 ;i++)
{
str[i]=pop();
}
str[i]=’\0’;
printf(“string after removing spaces is %s”,str);
}
char pop()
{ char ch;
if(s.top==-1)
printf(“stack underflow”);
else
{
ch=s.st[s.top];
s.top--;
}
}