SlideShare una empresa de Scribd logo
1 de 20
Q) Session10 a)Execute program to remove negative values from list of values by using
queues
Program:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int queue[MAX],f=-1,r=-1;
void enq();
void deq();
void display();
void deletenegative();
main()
{
int n,i;
printf("enter the number of elements");
scanf("%d",&n);
for(i=1;i<=n;i++)
enq();
display();
printf("nAfter deleting negative valuesn");
deletenegative();
display();
}
void enq()
{
int val;
if(r==MAX-1) {
printf("Queue full");
return;
}
else if(f == -1 && r == -1)
f=r=0;
else
r=r+1;
printf("enter the data to be enteredn");
scanf("%d",&val);
queue[r]=val;
}
void deq()
{
if(f==-1)
{
printf("underflow");
return;
}
if(f ==r)
f=r=-1;
else
f=f+1;
}
void display()
{
int i;
if(f ==-1)
printf("queue emptyn");
else
{
for(i=f; i<=r;i++)
printf("%dt",queue[i]);
}
}
void move(int i)
{
for( ;i<r;i++)
queue[i]=queue[i+1];
}
void deletenegative()
{
int i;
if(f ==-1)
printf("queue emptyn");
else
{
for(i=f; i<=r;)
{
if(queue[i] <0)
{
if(i==f)
{
deq();
i=i+1;
}
else
{
move(i);
r=r-1;
i=i-1;
}
}
else
i=i+1;
}
}
}
To see output... See my blog
http://enthusiaststudent.blogspot.in/2017/01/removing-negative-values-in-queues-c.html
Q) 10 bEnqueue, Dequeue and Display operations in Circular Queue using Arrays
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int cqueue[MAX], front=-1, rear=-1;
void deq();
void enq();
void display();
main()
{
int n,i;
printf("enter the number of elements");
scanf("%d",&n);
for(i=1;i<=n;i++)
enq();
display();
deq();
display();
}
void enq()
{
int val;
if((front == 0 && rear == MAX-1)||(front == rear+1))
{
printf("Queue full");
return;
}
if(front == -1 && rear == -1)
front = rear = 0;
else if(rear == MAX-1)
rear== 0;
else
rear=rear+1;
printf("enter the data to be enteredn");
scanf("%d",&val);
cqueue[rear]=val;
}
void deq()
{
if(front == -1 && rear == -1)
{
printf("queue is empty");
return;
}
printf("ndeleted element=%dn ",cqueue[front]);
if(front == rear)
front = rear = -1;
else if(front == MAX-1)
front = 0;
else
front = front+1;
}
void display()
{
int i;
if(front == -1 && rear == -1
{
printf(“queue is empty”);
return;
}
if(front <= rear)
{
for(i=front;i<=rear;i++)
printf("%dt",cqueue[i]);
}
else
{
for(i=front;i<MAX;i++)
printf("%dt",cqueue[i]);
for(i=0;i<=rear;i++)
printf("%dt",cqueue[i]);
}
}
Q11a) Implement Input Restricted and output Restricted DEQUEUE(Double ended
Queue)
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
/*DEQUEUE IS ALWAYS IMPLEMENTED USING CIRCULAR QUEUE*/
int dequeue[MAX],front=-1,rear=-1;
void deleteFront();//deq();
void deleteRear();
void insertFront();
void insertRear(); //enq()
void display();
main()
{
int opt1,opt2;
while(1)
{
printf("Press 1. Input Restricted 2. Output Restricted 3. display 4. exitn");
scanf("%d",&opt1);
switch(opt1)
{
case 1:
do{
printf("Press 1. insertRear 2. deleteFront 3. deleteRear
4. Display() 5. Backn");
scanf("%d",&opt2);
switch(opt2)
{
case 1: insertRear();
break;
case 2: deleteFront();
break;
case 3: deleteRear();
break;
case 4: display();
break;
}
}while(opt2>=1 && opt2 <= 4);
break;
case 2:
do{
printf("Press 1. deleteFront 2. insertRear 3. insertFront
4. Display 5. Back n");
scanf("%d",&opt2);
switch(opt2)
{
case 1: deleteFront();
break;
case 2: insertRear();
break;
case 3: insertFront();
break;
case 4: display();
break;
}
}while(opt2>=1 && opt2 <= 4);
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void insertRear() // enq()
{
int val;
if((front == 0 && rear == MAX-1)||(front == rear+1))
{
printf("Queue full");
return;
}
if(front == -1 && rear == -1)
front = rear = 0;
else if(rear == MAX-1)
rear== 0;
else
rear=rear+1;
printf("enter the data to be enteredn");
scanf("%d",&val);
dequeue[rear]=val;
}
void deleteFront() //deq()
{
if(front == -1 && rear == -1)
{
printf("queue is empty");
return;
}
printf("ndeleted element=%dn ",dequeue[front]);
if(front == rear)
front = rear = -1;
else if(front == MAX-1)
front = 0;
else
front = front+1;
}
void insertFront()
{
int val;
if((front == 0 && rear == MAX-1)||(front == rear+1))
{
printf("Queue full");
return;
}
if(front == -1 && rear == -1)
front = rear = 0;
else if(front == 0)
front = MAX-1;
else
front=front-1;
printf("enter the data to be enteredn");
scanf("%d",&val);
dequeue[front]=val;
}
void deleteRear()
{
if(front == -1 && rear == -1)
{
printf("queue is empty");
return;
}
printf("ndeleted element=%dn ",dequeue[front]);
if(front == rear)
front = rear = -1;
else if(rear == 0)
rear = MAX-1;
else
rear = rear-1;
}
void display()
{
int i;
if(front == -1 && rear == -1)
{
printf("queue is empty");
return;
}
if(front <= rear)
{
for(i=front;i<=rear;i++)
printf("%dt",dequeue[i]);
}
else
{
for(i=front;i<MAX;i++)
printf("%dt",dequeue[i]);
for(i=0;i<=rear;i++)
printf("%dt",dequeue[i]);
}
}
Q) 12 Program to implement Ascending Priority queue
#include<stdio.h>
#include <stdlib.h>
struct node
{
int data,pri;
struct node *next;
};
struct node *head=NULL,*c,*p;
void create();
void display();
void deleteMin();
main()
{
int n,i;
printf("enter the number of elements");
scanf("%d",&n);
for(i=1;i<=n;i++)
create();
display();
deleteMin();
printf("n after deleting one element, the contents of apq are :n");
display();
}
void create()
{
int v,priority;
printf("enter value and priorityn");
scanf("%d%d",&v,&priority);
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data =v;
newnode->pri=priority;
newnode->next = NULL;
if(head == NULL)
head = newnode;
else if( newnode->pri < head->pri)
{
newnode->next=head;
head=newnode;
}
else
{
c=head;
while(newnode->pri >= c->pri && c->next != NULL)
{
p=c;
c=c->next;
}
if(c->next == NULL && newnode->pri >= c->pri)
c->next=newnode;
else
{
p->next = newnode;
newnode->next=c;
}
}
}
void display()
{
if(head == NULL)
printf("list is empty");
else
{
c=head;
while(c != NULL)
{
printf("%d %d->",c->data,c->pri);
c=c->next;
}
}
}
void deleteMin()
{
/* delete the first node as the first node is minimum in ascending priority queue*/
c=head;
head=head->next;
free(c);
}
Session-13 A) What is a recursive solution to summing up a list of numbers? First you
should note that the sum of [2 13 4 25 66 71 82 91]) is equal to 2 + sum of [ 13 4 25 66 71
82 91]
#include <stdio.h>
int sum(int *a,int index,int n)
{
if(index == n)
return 0;
return(a[index]+sum(a,index+1,n));
}
main()
{
int n,i,a[100];
printf("enter size of arrayn");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array sum =%d",sum(a,0,n));
}
Session-13b) . Write a recursive function called elfish? that, given a word, tells us if that
word is elfish or not.
#include <stdio.h>
int elfish(char *s,char *b, int i)
{
if(b[i]=='0')
return 1;
int k,flag=0;
for(k=0;s[k]!='0';k++)
{
if(s[k]==b[i])
{
flag=1;
break;
}
}
if(flag == 1)
return elfish(s,b,i+1);
else
return 0;
}
main()
{
char s[100],b[4]="elf";
int result;
printf("enter the stringn");
gets(s);
result=elfish(s,b,0);
if(result ==0)
printf("given string is not elfish");
else
printf("given string is elfish");
}
Session-14a)Write a recursive function to Find the total number of sequences of length
n (using H and T) such that no two Hs are next to each other.
#include <stdio.h>
int total_seq(int n)
{
if(n == 1)
return 2;
if(n ==2)
return 3;
return(total_seq(n-1)+total_seq(n-2));
}
main()
{
int n,i;
printf("enter n value");
scanf("%d",&n);
printf("n Total number of sequences = %d",total_seq(n));
}
Session14 b) Given an array of positive numbers, find the maximum sum of a
subsequence with the constraint that no 2 numbers in the sequence should be adjacent
in the array.
#include<stdio.h>
int excl,incl;
/*Function to return max sum such that no two elements are adjacent */
int FindMaxSum(int *a, int index, int n)
{
int temp;
if(index == n)
return incl;
else
{
temp=incl;
if(incl<(excl+a[index]))
incl=excl+a[index];
excl = temp;
return FindMaxSum(a,index+1,n);
}
}
main()
{
int n, a[100],i;
printf(“enter the number of elementsn”);
scanf(“%d”, &n);
printf(“enter array elementsn”);
for(i=0;i<n;i++)
scanf(“%d”, &a[i]);
incl = a[0];
excl = 0;
printf("nMaximum sum of non- adjacent elements= %d n", FindMaxSum(a,1, n));
}
Session-15a)
Infix to postfix conversion
#include<stdio.h>
#include <process.h>
#define MAX 50
char stack[MAX];
int top=-1;
int getpriority(char);
void push(char);
char pop();
main()
{
char infix[50],temp,ch,postfix[50];
int i,j=0;
printf("enter the infix expressionn");
scanf("%s",&infix);
for(i=0; infix[i] != '0'; i++)
{
ch=infix[i];
if(ch == '(')
push(ch);
else if(ch == ')')
{
while(stack[top]!='(')
postfix[j++]=pop();
temp=pop();// popping the (
}
else if(isalnum(ch))
postfix[j++]=ch;
else
{
while(getpriority(stack[top])>=getpriority(ch))
postfix[j++]=pop();
push(ch);
}
}
while(top != -1)
postfix[j++]=pop();
postfix[j]='0';
puts(postfix);
}
int getpriority(char ch)
{
if( ch == '*'|| ch == '/'|| ch == '%')
return 2;
else if(ch == '+' || ch == '-')
return 1;
else
return 0;
}
void push(char item)
{
if(top == MAX-1)
{
printf("stack is full");
return;
}
top=top+1;
stack[top]=item;
}
char pop()
{
if(top == -1)
{
printf("stack empty");
return;
}
char temp;
temp=stack[top];
top=top-1;
return temp;
}
Evaluating PostFix expression
#include<stdio.h>
#include <process.h>
#include <string.h>
#define MAX 50
char stack[MAX];
int top=-1;
void push(float);
float pop();
void main()
{
char exp[MAX],temp,ch;
int i;
float op1,op2,value;
printf("Enter an expression : ");
gets(exp);
for(i=0;i<strlen(exp);i++)
{
ch=exp[i];
if(isdigit(ch))
push((float)ch-'0');
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+':
value=op1+op2;
break;
case '-':
value=op1-op2;
break;
case '*':
value=op1*op2;
break;
case '/':
value=op1/op2;
break;
case '%':
value=(int)op1%(int)op2;
break;
}
push(value);
}
}
printf("n Result=%f",pop());
}
void push(float item)
{
if(top == MAX-1)
{
printf("stack is full");
return;
}
top=top+1;
stack[top]=item;
}
float pop()
{
if(top == -1)
{
printf("stack empty");
return;
}
float temp;
temp=stack[top];
top=top-1;
return temp;
}
15b) symbol Balancing Using Stacks
#include<stdio.h>
#include <process.h>
#include <string.h>
#define MAX 50
char stack[MAX];
int top=-1;
void push(char);
char pop();
void main()
{
char exp[MAX],temp;
int i, flag=1;
printf("Enter an expression : ");
gets(exp);
for(i=0;i<strlen(exp);i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push(exp[i]);
if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
if(top == -1)
{
flag=0; break;
}
else
{
temp=pop();
if(exp[i]==')' && (temp=='{' || temp=='['))
flag=0;
if(exp[i]=='}' && (temp=='(' || temp=='['))
flag=0;
if(exp[i]==']' && (temp=='(' || temp=='{'))
flag=0;
}
}
if(top>=0)
flag=0;
if(flag==1)
printf("n Valid expression");
else
printf("n Invalid expression");
}
void push(char item)
{
if(top == MAX-1)
{
printf("stack is full");
return;
}
top=top+1;
stack[top]=item;
}
char pop()
{
if(top == -1)
{
printf("stack empty");
return;
}
char temp;
temp=stack[top];
top=top-1;
return temp;
}
17a) Use Queue data structure to print binary numbers from 1 to n.
1) Create an empty queue of strings
2) Enqueue the first binary number “1” to queue.
3) Now run a loop for generating and printing n Binary Numbers.
a) Dequeue and Print the front of queue.
b) Append “0” at the end of front item and enqueue it.
c) Append “1” at the end of front item and enqueue it.
Input: n=5
Output: 1,10,11,100,101
#include <stdio.h>
#include <string.h>
#define MAX 20
char queue[MAX][MAX],temp[MAX];
int front=-1, rear=-1;
void enq(char *s)
{
if(rear == MAX-1)
{
printf("Queye full");
return;
}
if(front == -1 && rear == -1)
front=rear=0;
else
rear=rear+1;
strcpy(queue[rear],s);
}
char* deq()
{
if(front == -1)
printf("stack is empty");
else
{
strcpy(temp,queue[front]);
if(front == rear)
front = rear = -1;
else
front=front+1;
return temp;
}
}
void bin()
{
char temp2[MAX];
strcpy(temp,deq());
printf("Binary numbers = %sn",temp);
strcpy(temp2,temp);
strcat(temp,"0");
enq(temp);
strcat(temp2,"1");
enq(temp2);
}
main()
{
int i;
char temp[2]="1";
enq(temp);
for(i=1;i<=5;i++)
bin();
}
18a) Implementing Stacks using linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL,*c,*p;
void push();
void pop();
void display();
main()
{
int opt;
while(1)
{
printf("nPress 1. Pusht 2. Popt3. Display t4. Exit n");
scanf("%d",&opt);
switch(opt)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void push()
{
int val;
printf("nenter a value to insert into stackn");
scanf("%d",&val);
struct node * newnode=malloc(sizeof(struct node));
newnode->data=val;
newnode->next = NULL;
if(top == NULL)
top = newnode;
else
{
newnode->next=top;
top=newnode;
}
}
void pop()
{
if(top == NULL)
{
printf("n Stack is EMPTY..Deletion is not possible");
return;
}
printf("n Deleted element = %dn",top->data);
c=top;
top=top->next;
free(c);
}
void display()
{
if(top == NULL)
{
printf("stack is empty");
return;
}
else
{
for(c=top;c!=NULL;c=c->next)
printf("%dt",c->data);
}
}
18b) Implementing Queues using Linked List
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *f=NULL,*r = NULL,*c;
void enq();
void deq();
void display();
main()
{
int opt;
while(1)
{
printf("n Press 1. Enqt 2. Deqt 3. Display t 4. Exitn");
scanf("%d",&opt);
switch(opt)
{
case 1: enq();
break;
case 2: deq();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void enq()
{
int val;
printf("enter value to be enqueuedn");
scanf("%d",&val);
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=NULL;
if(f == NULL && r == NULL)
f=r=newnode;
else
{
r->next=newnode;
r= newnode;
}
}
void deq()
{
if(f == NULL && r == NULL)
{
printf("queue is empty");
return;
}
if(f == r)
f = r = NULL;
else
{
c=f;
f=f->next;
free(c);
}
}
void display()
{
if(f == NULL && r == NULL)
{
printf("queue is empty");
return;
}
else
{
c=f;
while(c != NULL)
{
printf("%dt",c->data);
c=c->next;
}
}
}
Applications of Stack:
Reversing a list
Parentheses checker
Conversion of an infix expression into a postfix expression
Evaluation of a postfix expression
Conversion of an infix expression into a prefix expression
Evaluation of a prefix expression
Recursion
Tower of Hanoi
Applications of Queue:
 Operating systems often maintain a queue of processes that are ready to execute or
that are waiting for a particular event to occur.
 Computer systems must often provide a “holding area” for messages between two
processes, two programs, or even two systems. This holding area is usually called a
“buffer” and is often implemented as a queue.
 .In real life, Call Center phone systems will use Queues, to hold people calling them
in an order, until a service representative is free. We wait in queues to buy pizza, to
enter movie theaters etc, queue data structure help us simulate and analyze such real
world queues.
 Handling of interrupts in real-time systems. The interrupts are handled in the same
order as they arrive, First come first served.
Q)Addition of Two polynomials using linked list algorithm
Let p and q be the two polynomials represented by the linked list.
1. while p and q are not null, repeat step 2.
2. If exponents of the two terms are equal
then if the terms do not cancel then insert the sum of the terms into the sum Polynomial
Advance p
Advance q
Else if the exponent of the first polynomial> exponent of second
Then insert the term from first polynomial into sum polynomial
Advance p
Else insert the term from second polynomial into sum polynomial
Advance q
3. copy the remaining terms from the non empty polynomial into the
sum polynomial.

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

C programms
C programmsC programms
C programms
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Daa practicals
Daa practicalsDaa practicals
Daa practicals
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
C basics
C basicsC basics
C basics
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Single linked list
Single linked listSingle linked list
Single linked list
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
 
C Prog - Strings
C Prog - StringsC Prog - Strings
C Prog - Strings
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 

Similar a DataStructures notes

Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresLakshmi Sarvani Videla
 
Data structure output 1
Data structure output 1Data structure output 1
Data structure output 1Balaji Thala
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C CodeSyed Ahmed Zaki
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdfanujmkt
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab filesNitesh Dubey
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diplomamustkeem khan
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 

Similar a DataStructures notes (20)

Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
Data structure output 1
Data structure output 1Data structure output 1
Data structure output 1
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Ada file
Ada fileAda file
Ada file
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
cpract.docx
cpract.docxcpract.docx
cpract.docx
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Circular queue
Circular queueCircular queue
Circular queue
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
7 functions
7  functions7  functions
7 functions
 

Más de Lakshmi Sarvani Videla (20)

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 
Functions
FunctionsFunctions
Functions
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
 
Graphs
GraphsGraphs
Graphs
 
B trees
B treesB trees
B trees
 
Functions in python3
Functions in python3Functions in python3
Functions in python3
 
Dictionary
DictionaryDictionary
Dictionary
 
Sets
SetsSets
Sets
 
Lists
ListsLists
Lists
 
C programs
C programsC programs
C programs
 
Data Mining: Data Preprocessing
Data Mining: Data PreprocessingData Mining: Data Preprocessing
Data Mining: Data Preprocessing
 

Último

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 

Último (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

DataStructures notes

  • 1. Q) Session10 a)Execute program to remove negative values from list of values by using queues Program: #include <stdio.h> #include <stdlib.h> #define MAX 10 int queue[MAX],f=-1,r=-1; void enq(); void deq(); void display(); void deletenegative(); main() { int n,i; printf("enter the number of elements"); scanf("%d",&n); for(i=1;i<=n;i++) enq(); display(); printf("nAfter deleting negative valuesn"); deletenegative(); display(); } void enq() { int val; if(r==MAX-1) { printf("Queue full"); return; } else if(f == -1 && r == -1) f=r=0; else r=r+1; printf("enter the data to be enteredn"); scanf("%d",&val); queue[r]=val; } void deq() { if(f==-1) {
  • 2. printf("underflow"); return; } if(f ==r) f=r=-1; else f=f+1; } void display() { int i; if(f ==-1) printf("queue emptyn"); else { for(i=f; i<=r;i++) printf("%dt",queue[i]); } } void move(int i) { for( ;i<r;i++) queue[i]=queue[i+1]; } void deletenegative() { int i; if(f ==-1) printf("queue emptyn"); else { for(i=f; i<=r;) { if(queue[i] <0) { if(i==f) { deq(); i=i+1; } else {
  • 3. move(i); r=r-1; i=i-1; } } else i=i+1; } } } To see output... See my blog http://enthusiaststudent.blogspot.in/2017/01/removing-negative-values-in-queues-c.html Q) 10 bEnqueue, Dequeue and Display operations in Circular Queue using Arrays #include <stdio.h> #include <stdlib.h> #define MAX 5 int cqueue[MAX], front=-1, rear=-1; void deq(); void enq(); void display(); main() { int n,i; printf("enter the number of elements"); scanf("%d",&n); for(i=1;i<=n;i++) enq(); display(); deq(); display(); } void enq() { int val; if((front == 0 && rear == MAX-1)||(front == rear+1)) { printf("Queue full"); return; } if(front == -1 && rear == -1) front = rear = 0; else if(rear == MAX-1)
  • 4. rear== 0; else rear=rear+1; printf("enter the data to be enteredn"); scanf("%d",&val); cqueue[rear]=val; } void deq() { if(front == -1 && rear == -1) { printf("queue is empty"); return; } printf("ndeleted element=%dn ",cqueue[front]); if(front == rear) front = rear = -1; else if(front == MAX-1) front = 0; else front = front+1; } void display() { int i; if(front == -1 && rear == -1 { printf(“queue is empty”); return; } if(front <= rear) { for(i=front;i<=rear;i++) printf("%dt",cqueue[i]); } else { for(i=front;i<MAX;i++) printf("%dt",cqueue[i]); for(i=0;i<=rear;i++) printf("%dt",cqueue[i]); } } Q11a) Implement Input Restricted and output Restricted DEQUEUE(Double ended Queue) #include <stdio.h> #include <stdlib.h> #define MAX 100 /*DEQUEUE IS ALWAYS IMPLEMENTED USING CIRCULAR QUEUE*/ int dequeue[MAX],front=-1,rear=-1;
  • 5. void deleteFront();//deq(); void deleteRear(); void insertFront(); void insertRear(); //enq() void display(); main() { int opt1,opt2; while(1) { printf("Press 1. Input Restricted 2. Output Restricted 3. display 4. exitn"); scanf("%d",&opt1); switch(opt1) { case 1: do{ printf("Press 1. insertRear 2. deleteFront 3. deleteRear 4. Display() 5. Backn"); scanf("%d",&opt2); switch(opt2) { case 1: insertRear(); break; case 2: deleteFront(); break; case 3: deleteRear(); break; case 4: display(); break; } }while(opt2>=1 && opt2 <= 4); break; case 2: do{ printf("Press 1. deleteFront 2. insertRear 3. insertFront 4. Display 5. Back n"); scanf("%d",&opt2); switch(opt2) { case 1: deleteFront(); break; case 2: insertRear(); break; case 3: insertFront(); break; case 4: display(); break; }
  • 6. }while(opt2>=1 && opt2 <= 4); break; case 3: display(); break; case 4: exit(0); } } } void insertRear() // enq() { int val; if((front == 0 && rear == MAX-1)||(front == rear+1)) { printf("Queue full"); return; } if(front == -1 && rear == -1) front = rear = 0; else if(rear == MAX-1) rear== 0; else rear=rear+1; printf("enter the data to be enteredn"); scanf("%d",&val); dequeue[rear]=val; } void deleteFront() //deq() { if(front == -1 && rear == -1) { printf("queue is empty"); return; } printf("ndeleted element=%dn ",dequeue[front]); if(front == rear) front = rear = -1; else if(front == MAX-1) front = 0; else front = front+1; } void insertFront() { int val; if((front == 0 && rear == MAX-1)||(front == rear+1)) { printf("Queue full"); return; }
  • 7. if(front == -1 && rear == -1) front = rear = 0; else if(front == 0) front = MAX-1; else front=front-1; printf("enter the data to be enteredn"); scanf("%d",&val); dequeue[front]=val; } void deleteRear() { if(front == -1 && rear == -1) { printf("queue is empty"); return; } printf("ndeleted element=%dn ",dequeue[front]); if(front == rear) front = rear = -1; else if(rear == 0) rear = MAX-1; else rear = rear-1; } void display() { int i; if(front == -1 && rear == -1) { printf("queue is empty"); return; } if(front <= rear) { for(i=front;i<=rear;i++) printf("%dt",dequeue[i]); } else { for(i=front;i<MAX;i++) printf("%dt",dequeue[i]); for(i=0;i<=rear;i++) printf("%dt",dequeue[i]); } } Q) 12 Program to implement Ascending Priority queue
  • 8. #include<stdio.h> #include <stdlib.h> struct node { int data,pri; struct node *next; }; struct node *head=NULL,*c,*p; void create(); void display(); void deleteMin(); main() { int n,i; printf("enter the number of elements"); scanf("%d",&n); for(i=1;i<=n;i++) create(); display(); deleteMin(); printf("n after deleting one element, the contents of apq are :n"); display(); } void create() { int v,priority; printf("enter value and priorityn"); scanf("%d%d",&v,&priority); struct node *newnode = (struct node *)malloc(sizeof(struct node)); newnode->data =v; newnode->pri=priority; newnode->next = NULL; if(head == NULL) head = newnode; else if( newnode->pri < head->pri) { newnode->next=head; head=newnode; } else { c=head; while(newnode->pri >= c->pri && c->next != NULL) { p=c; c=c->next; } if(c->next == NULL && newnode->pri >= c->pri) c->next=newnode; else
  • 9. { p->next = newnode; newnode->next=c; } } } void display() { if(head == NULL) printf("list is empty"); else { c=head; while(c != NULL) { printf("%d %d->",c->data,c->pri); c=c->next; } } } void deleteMin() { /* delete the first node as the first node is minimum in ascending priority queue*/ c=head; head=head->next; free(c); } Session-13 A) What is a recursive solution to summing up a list of numbers? First you should note that the sum of [2 13 4 25 66 71 82 91]) is equal to 2 + sum of [ 13 4 25 66 71 82 91] #include <stdio.h> int sum(int *a,int index,int n) { if(index == n) return 0; return(a[index]+sum(a,index+1,n)); } main() { int n,i,a[100]; printf("enter size of arrayn"); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]);
  • 10. printf("array sum =%d",sum(a,0,n)); } Session-13b) . Write a recursive function called elfish? that, given a word, tells us if that word is elfish or not. #include <stdio.h> int elfish(char *s,char *b, int i) { if(b[i]=='0') return 1; int k,flag=0; for(k=0;s[k]!='0';k++) { if(s[k]==b[i]) { flag=1; break; } } if(flag == 1) return elfish(s,b,i+1); else return 0; } main() { char s[100],b[4]="elf"; int result; printf("enter the stringn"); gets(s); result=elfish(s,b,0); if(result ==0) printf("given string is not elfish"); else printf("given string is elfish"); } Session-14a)Write a recursive function to Find the total number of sequences of length n (using H and T) such that no two Hs are next to each other. #include <stdio.h> int total_seq(int n) { if(n == 1) return 2; if(n ==2) return 3; return(total_seq(n-1)+total_seq(n-2));
  • 11. } main() { int n,i; printf("enter n value"); scanf("%d",&n); printf("n Total number of sequences = %d",total_seq(n)); } Session14 b) Given an array of positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. #include<stdio.h> int excl,incl; /*Function to return max sum such that no two elements are adjacent */ int FindMaxSum(int *a, int index, int n) { int temp; if(index == n) return incl; else { temp=incl; if(incl<(excl+a[index])) incl=excl+a[index]; excl = temp; return FindMaxSum(a,index+1,n); } } main() { int n, a[100],i; printf(“enter the number of elementsn”); scanf(“%d”, &n); printf(“enter array elementsn”); for(i=0;i<n;i++) scanf(“%d”, &a[i]); incl = a[0]; excl = 0; printf("nMaximum sum of non- adjacent elements= %d n", FindMaxSum(a,1, n)); } Session-15a) Infix to postfix conversion #include<stdio.h> #include <process.h>
  • 12. #define MAX 50 char stack[MAX]; int top=-1; int getpriority(char); void push(char); char pop(); main() { char infix[50],temp,ch,postfix[50]; int i,j=0; printf("enter the infix expressionn"); scanf("%s",&infix); for(i=0; infix[i] != '0'; i++) { ch=infix[i]; if(ch == '(') push(ch); else if(ch == ')') { while(stack[top]!='(') postfix[j++]=pop(); temp=pop();// popping the ( } else if(isalnum(ch)) postfix[j++]=ch; else { while(getpriority(stack[top])>=getpriority(ch)) postfix[j++]=pop(); push(ch); } } while(top != -1) postfix[j++]=pop(); postfix[j]='0'; puts(postfix); } int getpriority(char ch) { if( ch == '*'|| ch == '/'|| ch == '%') return 2; else if(ch == '+' || ch == '-') return 1; else return 0; } void push(char item) { if(top == MAX-1) {
  • 13. printf("stack is full"); return; } top=top+1; stack[top]=item; } char pop() { if(top == -1) { printf("stack empty"); return; } char temp; temp=stack[top]; top=top-1; return temp; } Evaluating PostFix expression #include<stdio.h> #include <process.h> #include <string.h> #define MAX 50 char stack[MAX]; int top=-1; void push(float); float pop(); void main() { char exp[MAX],temp,ch; int i; float op1,op2,value; printf("Enter an expression : "); gets(exp); for(i=0;i<strlen(exp);i++) { ch=exp[i]; if(isdigit(ch)) push((float)ch-'0'); else { op2=pop(); op1=pop(); switch(ch)
  • 14. { case '+': value=op1+op2; break; case '-': value=op1-op2; break; case '*': value=op1*op2; break; case '/': value=op1/op2; break; case '%': value=(int)op1%(int)op2; break; } push(value); } } printf("n Result=%f",pop()); } void push(float item) { if(top == MAX-1) { printf("stack is full"); return; } top=top+1; stack[top]=item; } float pop() { if(top == -1) { printf("stack empty"); return; } float temp; temp=stack[top]; top=top-1;
  • 15. return temp; } 15b) symbol Balancing Using Stacks #include<stdio.h> #include <process.h> #include <string.h> #define MAX 50 char stack[MAX]; int top=-1; void push(char); char pop(); void main() { char exp[MAX],temp; int i, flag=1; printf("Enter an expression : "); gets(exp); for(i=0;i<strlen(exp);i++) { if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[') push(exp[i]); if(exp[i]==')' || exp[i]=='}' || exp[i]==']') if(top == -1) { flag=0; break; } else { temp=pop(); if(exp[i]==')' && (temp=='{' || temp=='[')) flag=0; if(exp[i]=='}' && (temp=='(' || temp=='[')) flag=0; if(exp[i]==']' && (temp=='(' || temp=='{')) flag=0; } } if(top>=0) flag=0; if(flag==1) printf("n Valid expression"); else printf("n Invalid expression");
  • 16. } void push(char item) { if(top == MAX-1) { printf("stack is full"); return; } top=top+1; stack[top]=item; } char pop() { if(top == -1) { printf("stack empty"); return; } char temp; temp=stack[top]; top=top-1; return temp; } 17a) Use Queue data structure to print binary numbers from 1 to n. 1) Create an empty queue of strings 2) Enqueue the first binary number “1” to queue. 3) Now run a loop for generating and printing n Binary Numbers. a) Dequeue and Print the front of queue. b) Append “0” at the end of front item and enqueue it. c) Append “1” at the end of front item and enqueue it. Input: n=5 Output: 1,10,11,100,101 #include <stdio.h> #include <string.h> #define MAX 20 char queue[MAX][MAX],temp[MAX]; int front=-1, rear=-1; void enq(char *s) { if(rear == MAX-1) { printf("Queye full"); return; } if(front == -1 && rear == -1) front=rear=0; else rear=rear+1; strcpy(queue[rear],s); } char* deq()
  • 17. { if(front == -1) printf("stack is empty"); else { strcpy(temp,queue[front]); if(front == rear) front = rear = -1; else front=front+1; return temp; } } void bin() { char temp2[MAX]; strcpy(temp,deq()); printf("Binary numbers = %sn",temp); strcpy(temp2,temp); strcat(temp,"0"); enq(temp); strcat(temp2,"1"); enq(temp2); } main() { int i; char temp[2]="1"; enq(temp); for(i=1;i<=5;i++) bin(); } 18a) Implementing Stacks using linked list #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *top=NULL,*c,*p; void push(); void pop(); void display(); main() { int opt; while(1) { printf("nPress 1. Pusht 2. Popt3. Display t4. Exit n"); scanf("%d",&opt); switch(opt) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); } }
  • 18. } void push() { int val; printf("nenter a value to insert into stackn"); scanf("%d",&val); struct node * newnode=malloc(sizeof(struct node)); newnode->data=val; newnode->next = NULL; if(top == NULL) top = newnode; else { newnode->next=top; top=newnode; } } void pop() { if(top == NULL) { printf("n Stack is EMPTY..Deletion is not possible"); return; } printf("n Deleted element = %dn",top->data); c=top; top=top->next; free(c); } void display() { if(top == NULL) { printf("stack is empty"); return; } else { for(c=top;c!=NULL;c=c->next) printf("%dt",c->data); } } 18b) Implementing Queues using Linked List #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *f=NULL,*r = NULL,*c; void enq(); void deq(); void display(); main() {
  • 19. int opt; while(1) { printf("n Press 1. Enqt 2. Deqt 3. Display t 4. Exitn"); scanf("%d",&opt); switch(opt) { case 1: enq(); break; case 2: deq(); break; case 3: display(); break; case 4: exit(0); } } } void enq() { int val; printf("enter value to be enqueuedn"); scanf("%d",&val); struct node *newnode = (struct node *)malloc(sizeof(struct node)); newnode->data=val; newnode->next=NULL; if(f == NULL && r == NULL) f=r=newnode; else { r->next=newnode; r= newnode; } } void deq() { if(f == NULL && r == NULL) { printf("queue is empty"); return; } if(f == r) f = r = NULL; else { c=f; f=f->next; free(c); } } void display() { if(f == NULL && r == NULL) { printf("queue is empty"); return; } else {
  • 20. c=f; while(c != NULL) { printf("%dt",c->data); c=c->next; } } } Applications of Stack: Reversing a list Parentheses checker Conversion of an infix expression into a postfix expression Evaluation of a postfix expression Conversion of an infix expression into a prefix expression Evaluation of a prefix expression Recursion Tower of Hanoi Applications of Queue:  Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur.  Computer systems must often provide a “holding area” for messages between two processes, two programs, or even two systems. This holding area is usually called a “buffer” and is often implemented as a queue.  .In real life, Call Center phone systems will use Queues, to hold people calling them in an order, until a service representative is free. We wait in queues to buy pizza, to enter movie theaters etc, queue data structure help us simulate and analyze such real world queues.  Handling of interrupts in real-time systems. The interrupts are handled in the same order as they arrive, First come first served. Q)Addition of Two polynomials using linked list algorithm Let p and q be the two polynomials represented by the linked list. 1. while p and q are not null, repeat step 2. 2. If exponents of the two terms are equal then if the terms do not cancel then insert the sum of the terms into the sum Polynomial Advance p Advance q Else if the exponent of the first polynomial> exponent of second Then insert the term from first polynomial into sum polynomial Advance p Else insert the term from second polynomial into sum polynomial Advance q 3. copy the remaining terms from the non empty polynomial into the sum polynomial.