Ds Solbank U2
Ds Solbank U2
Ds Solbank U2
Linked list: Definition, Representation of Singly Linked List in memory, Traversing a Singly linked list, Searching in
a Singly linked list, Memory allocation, Garbage collection, Insertion into a singly linked list, Deletion from a
singly linked list; Doubly linked list, Header linked list, Circular linked list. Stacks: Definition, Array representation
of stacks, Linked representation of stacks, Stack as ADT, Arithmetic Expressions: Polish Notation, Conversion of
infix expression to postfix expression, Evaluation of Post fix expression, Application of Stacks, Recursion, Towers
of Hanoi, Implementation of recursive procedures by stack. Queues: Definition, Array representation of queue,
Linked list representation of queues. Types of queue: Simple queue, Circular queue, Double-ended queue,
Priority queue, Operations on Queues, Applications of queues
2. What are the advantages and disadvantages of linked list over arrays.
Advantages • Efficient Memory Utilization (Arbitrary memory allocation)
• Insertion and deletion operations are easier and efficient
Disadvantages • Extra Memory spaces
• Random Access
• malloc() takes a single argument, which is the number of • calloc() takes two arguments:
bytes to allocate. 1) Number of blocks to be allocated.
2) Size of each block in bytes.
1. Write a C-function or algorithm to insert an item at the beginning of the single linked list.
typedef struct node
{
int info;
struct node *link;
}NODE;
NODE *header=NULL;
2. Write a C-function or algorithm to insert an item at the end of the single linked list.
typedef struct node
{
int info;
struct node *link;
}NODE;
NODE *header=NULL;
NODE *header=NULL;
if(header==NULL)
{
printf("\n EMPTY LIST");
}
else if(header->info==item)
{
header=header->link;
free(curptr);
}
else
{
while(curptr!=NULL)
{
if(curptr->info==item)
{
prevptr->link=curptr->link;
free(curptr);
curptr=curptr->link->link;
}
else
{
prevptr=curptr;
curptr=curptr->link;
}
}
}
}
4. Write a recursive program to find the GCD of three numbers.
# include<stdio.h>
# include<conio.h>
void main()
{
// three nos 4,6,8
int gcd12, gcd3;
clrscr();
gcd12=GCD(4,6);
printf("\n GCD between 4 & 6 = %d",gcd12);
gcd3=(GCD(gcd12,8));
printf("\n GCD between 4,6 & 8 = %d",gcd3);
getch();
}
#define size 4
int front = - 1;
int rear = - 1;
int queue[size];
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void delete_Q()
{
if (front == - 1 || front > rear)
{
printf("\nQueue Underflow ");
return ;
}
else
{
printf("\nElement deleted from queue is : %d", queue_array[front]);
front = front + 1;
}
}
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct polynomial
{
int coeff;
int power;
struct polynomial *LINK;
};
typedef struct polynomial NODE;
int main()
{
clrscr();
printf("\n Create 1st Polynomial: ");
poly1=create_poly();
printf("\n First polynomial : ");
display(poly1);
printf("\n Create 2nd Polynomial:");
poly2=create_poly();
printf("\n Second polynomial :");
display(poly2);
add_poly(poly1,poly2);
printf("\n Addition of Two polynomials : ");
display(poly3);
getch();
}
9. Evaluate Postfix expression using stack 6, 3, +, 5, *, 2, 3, +, +
• Add # at the end of P
• Repeat scanning P from left to right and repeat step 3 and 4 until # is encountered
• If an operand is encountered, push it onto stack
• If an operator is encountered
• (a) Remove top two elements A & B
• (b) Evaluate B (op) A
• (c) Push the result back onto Stack
• The result will be on the top of stack
• Exit
P=63+5*23++#
Symbol Operation Performed STACK
6 PUSH(6) 6
3 PUSH(3) 6 3
+ POP A=3 , B = 6 => B + A = 6 + 3 = 9 , PUSH(9) 9
5 PUSH(5) 9 5
* POP A=5, B = 9 => B * A = 5 * 9 = 45 , PUSH(45) 45
2 PUSH(2) 45 2
3 PUSH(3) 45 2 3
+ POP A=3, B=2 => 3 + 2 = 5 , PUSH(5) 45 5
+ POP A=5 , B=45 => 5 + 45 = 50 , PUSH(50) 50
# # STOP
Therefore P[top] = 50
P=874*+3-2*#
Symbol Operation Performed STACK
8 PUSH(8) 8
7 PUSH(7) 8 7
4 PUSH(4) 8 7 4
* A=4, B=7 B * A => 7 * 4 = 28 , PUSH(28) 8 28
+ A=28, B=8 B + A => 28 + 8 =36, PUSH(36) 36
3 PUSH(3) 36 3
- A=3, B=36, B - A=> 36-3=33, PUSH(33) 33
2 PUSH(2) 33 2
* A=2, B=33 , B * A => 33 * 2 = 66 PUSH(66) 66
# STOP
10. Convert infix to postfix Q = A + ( B * C – ( D / E F ) * G ) * H )
• Push ( on the stack and add right parenhesis at the end
• Scan Q from left to right
• if operand is found, add it to P
• if operator is encountered
• (a) pop from stack and add to p which has the same or higher precedence
• (b)push operator on to stack
• if ) encountered
• (a) Pop from stack add to P each operator until ( parenthis is encountered
• (b) Remove (
• Exit
Convert infix to postfix Q = ( x ^ y / ( 5 * z ) + 2 )