Program to implement a cyclic list-based queue.

#include <stdio.h> #include <stdlib.h> typedef int item_t; typedef struct qu_n_t {item_t item; struct qu_n_t *next; } qu_node_t; typedef struct {qu_node_t *remove; qu_node_t *insert; } queue_t; typedef qu_node_t node_t; #define BLOCKSIZE 256 node_t *currentblock = NULL; int size_left; node_t *free_list = NULL; node_t *get_node() { node_t *tmp; if( free_list != NULL ) { tmp = free_list; […]

Continue reading

Program to implement list-based queue.

#include <stdio.h> #include <stdlib.h> typedef int item_t; typedef struct qu_n_t {item_t item; struct qu_n_t *next; } qu_node_t; typedef struct {qu_node_t *remove; qu_node_t *insert; } queue_t; typedef qu_node_t node_t; #define BLOCKSIZE 256 node_t *currentblock = NULL; int size_left; node_t *free_list = NULL; node_t *get_node() { node_t *tmp; if( free_list != NULL ) { tmp = free_list; […]

Continue reading

Program to demonstrate a block-list-based stack.

#include <stdio.h> #include <stdlib.h> typedef int item_t; typedef struct st_t { item_t *base; item_t *top; int size; struct st_t *previous;} stack_t; stack_t *create_stack(int size) { stack_t *st; st = (stack_t *) malloc( sizeof(stack_t) ); st->base = (item_t *) malloc( size * sizeof(item_t) ); st->size = size; st->top = st->base; st->previous = NULL; return( st ); […]

Continue reading

Program to demonstrate an array based stack.

#include <stdio.h> #include <stdlib.h> typedef int item_t; typedef struct {item_t *base; item_t *top; int size;} stack_t; stack_t *create_stack(int size) { stack_t *st; st = (stack_t *) malloc( sizeof(stack_t) ); st->base = (item_t *) malloc( size * sizeof(item_t) ); st->size = size; st->top = st->base; return( st ); } int stack_empty(stack_t *st) { return( st->base == […]

Continue reading

Program to implement operation on stack using the class.

#include <stdio.h> #include <stdlib.h> typedef int item_t; typedef struct {item_t *base; item_t *top; int size;} stack_t; stack_t *create_stack(int size) { stack_t *st; st = (stack_t *) malloc( sizeof(stack_t) ); st->base = (item_t *) malloc( size * sizeof(item_t) ); st->size = size; st->top = st->base; return( st ); } int stack_empty(stack_t *st) { return( st->base == […]

Continue reading

Program to sort list of N elements using Merge sort.

#include<stdio.h> #include<conio.h> void main() { int a[20],i,j,n,temp,p=2,first,last,count=0; clrscr(), printf(“enter the number of elements:”); scanf(“%d”,&n); printf(“enter the number:”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); while(p<(n+1)) { first=0;last=p; while(last<(n+1)) { for(i=first;i<last;i++) for(j=i+1;j<last;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } first=last+1; last=first+p-1; } printf(“\n elements after %d iterations are:”,p); for(i=0;i<n;i++) printf(” %d”,a[i]); p=p*2; if(p>n) { p=n; count++; } if(count==2) break; } getch(); } […]

Continue reading

Program to Convert infix to postfix.

#include<stdio.h> #include<conio.h> #include<string.h> char symb,temp; int i=0,p,pos=0,top=-1,length; char infix[20],postfix[20],s[20]; main() { clrscr(); scanf(“%s”,infix); infix_to_postfix(infix,postfix); printf(“postfix exp=%s”,postfix); getch(); return; } infix_to_postfix(char infix[],char postfix[]) { length=strlen(infix); push(‘#’); while(i<length) { symb=infix[i]; switch(symb) { case'(‘:push(symb); break; case’)’: temp=pop(); while(temp!='(‘) { postfix[pos]=temp; pos++; temp=pop(); } break; case’+’: case’-‘: case’*’: case’/’: case’^’: case’$’: while(prcd(s[top])>=prcd(symb)) { temp=pop(); postfix[pos]=temp; pos++; } push(symb); break; […]

Continue reading

Program to Sort for an element using Bubble Sort.

#include<stdio.h> #include<conio.h> void main() { int n,a[15],i,j,temp; clrscr(); printf(“Enter the number of elements”); scanf(“%d”,&n); printf(“\nEnter the elements”); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } for(j=0;j<n;j++) { for(i=0;i<n-j;i++) { if(a[i]>=a[i+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } printf(“\nSorted elements:”); for(i=0;i<n;i++) { printf(“\n%d”,a[i]); } getch(); } Output: Enter the number of elements 5 Enter the elements 40 50 30 20 […]

Continue reading