Sie sind auf Seite 1von 20

QUEUE A queue (pronounced /kju/) is a particular kind of collection in which the entities in the collection are kept in order

and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that whenever an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure

14.3.1.Queues
1. A queue is a list with insertions at one end and deletions at the other end. 2. A queue exhibits the FIFO (first in first out) property.

14.3.2.Array Implementation of a Stack


#include <stdio.h> #include <stdlib.h> #define MAX 10 void insert(int queue[], int *rear, int value) { if(*rear < MAX-1) { *rear= *rear +1; queue[*rear] = value; } else { printf("The queue is full can not insert a value\n"); exit(0); } } void delete(int queue[], int *front, int rear, int * value) { if(*front == rear) { printf("The queue is empty can not delete a value\n"); exit(0); } *front = *front + 1; *value = queue[*front]; } void main() { int queue[MAX];

int front,rear; int n,value; front = rear = -1; insert(queue,&rear,1); insert(queue,&rear,2); delete(queue,&front,rear,&value); printf("The value deleted is %d\n",value);

14.3.3.A queue based on the linked list


# include <stdio.h> # include <stdlib.h> struct node { int data; struct node *link; }; void insert(struct node **front, struct node **rear, int value) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); if(temp==NULL) { printf("No Memory available Error\n"); exit(0); } temp->data = value; temp->link=NULL; if(*rear == NULL) { *rear = temp; *front = *rear; } else { (*rear)->link = temp; *rear = temp; } } void delete(struct node **front, struct node **rear, int *value) { struct node *temp; if((*front == *rear) && (*rear == NULL)) { printf(" The queue is empty cannot delete Error\n"); exit(0); } *value = (*front)->data; temp = *front; *front = (*front)->link;

if(*rear == temp) *rear = (*rear)->link; free(temp);

void main() { struct node *front=NULL,*rear = NULL; int n,value; insert(&front,&rear,1); insert(&front,&rear,2); insert(&front,&rear,3); insert(&front,&rear,4); delete(&front,&rear,&value); printf("The value deleted is %d\n",value); delete(&front,&rear,&value); printf("The value deleted is %d\n",value); delete(&front,&rear,&value); printf("The value deleted is %d\n",value); }

14.4.1.Circular queues
#include <stdio.h> #include <stdlib.h> #define MAX 10 void insert(int queue[], int *rear, int front, int value) { *rear= (*rear +1) % MAX; if(*rear == front) { printf("The queue is full can not insert a value\n"); exit(0); } queue[*rear] = value; } void delete(int queue[], int *front, int rear, int * value) { if(*front == rear) { printf("The queue is empty can not delete a value\n"); exit(0); } *front = (*front + 1) % MAX; *value = queue[*front]; }

void main() { int queue[MAX]; int front,rear; int n,value; front=0; rear=0; insert(queue,&rear,front,1); insert(queue,&rear,front,2); insert(queue,&rear,front,3); insert(queue,&rear,front,4); delete(queue,&front,rear,&value); printf("The value deleted is %d\n",value); delete(queue,&front,rear,&value); printf("The value deleted is %d\n",value); delete(queue,&front,rear,&value); printf("The value deleted is %d\n",value); } #include <stdio.h> int main(){ int a[100],i,j; printf("To DQueue Enter -1\n"); for(i=0;;){ printf("NQueue "); scanf("%d",&a[i]); if(a[i]==0) break; if(a[i]==-1){ a[i]=0; if(i==0){ printf("Wrong\n"); continue; } printf("DQueue = %d\n",a[0]); for(j=0;j<i;j++)

a[j]=a[j+1]; i--; } else i++; } for(j=0;j<i;j++) printf("%d ",a[j]); printf("\n"); getchar(); return 0; }

Bubble sort using link list Bubble sort [linked list] #include <stdio.h> #include <stdlib.h> #define MAX 10 struct lnode { int data; struct lnode *next; } *head, *visit; /* add a new entry to the linked list */ void llist_add(struct lnode **q, int num); /* preform a bubble sort on the linked list */ void llist_bubble_sort(void); /* print the entire linked list */ void llist_print(void); int main(void) { /* linked list */ struct lnode *newnode = NULL; int i = 0; /* a general counter */ /* load some random values into the linked list */ for(i = 0; i < MAX; i++) { llist_add(&newnode, (rand() % 100)); } head = newnode; printf("Before bubble sort:\n");

llist_print(); printf("After bubble sort:\n"); llist_bubble_sort(); llist_print(); return 0; } /* adds a node at the end of a linked list */ void llist_add(struct lnode **q, int num) { struct lnode *tmp; tmp = *q; /* if the list is empty, create first node */ if(*q == NULL) { *q = malloc(sizeof(struct lnode)); tmp = *q; } else { /* go to last node */ while(tmp->next != NULL) tmp = tmp->next; /* add node at the end */ tmp->next = malloc(sizeof(struct lnode)); tmp = tmp->next; } /* assign data to the last node */ tmp->data = num; tmp->next = NULL; } /* print the entire linked list */ void llist_print(void) { visit = head; while(visit != NULL) { printf("%d ", visit->data); visit = visit->next; } printf("\n"); } /* preform a bubble sort on the linked list */ void llist_bubble_sort(void) {

struct lnode *a = NULL; struct lnode *b = NULL; struct lnode *c = NULL; struct lnode *e = NULL; struct lnode *tmp = NULL; /* // the `c' node precedes the `a' and `e' node // pointing up the node to which the comparisons // are being made. */ while(e != head->next) { c = a = head; b = a->next; while(a != e) { if(a->data > b->data) { if(a == head) { tmp = b -> next; b->next = a; a->next = tmp; head = b; c = b; } else { tmp = b->next; b->next = a; a->next = tmp; c->next = b; c = b; } } else { c = a; a = a->next; } b = a->next; if(b == e) e = a; } } } Write a program to find permutation #include<stdio.h> #include<stdlib.h> int lev=-1,n,val[50],a[50]; void main()

{ int i,j; clrscr(); printf("Enter howmany numbers "); scanf("%d",&n); for(i=0;i<n;i++) { val[i]=0; j=i+1; scanf("%d",&a[j]); } visit(0); getch(); } visit(int k) { int i; val[k]=++lev; if(lev==n) { for(i=0;i<n;i++) printf("%2d",a[val[i]]); printf(" "); } for(i=0;i<n;i++) if(val[i]==0) visit(i); lev--; val[k]=0; } Infix To Prefix Conversion #include<stdio.h> #include<conio.h> #include<string.h> #define MAX 15 #define true 1 #define false 0 /*Structure Decvlaration*/ typedef struct { char data[MAX]; char top;

}STK; /*Function Declarations*/ void input(char str[]); void intopre(char str1[],char pre[]); void intopost(char str1[],char post[]); int isoperand(char sym); int prcd(char sym); void push(STK *s1,char elem); int pop(STK *s1); int empty(STK *s2); int full(STK *s2); void dis(char str[]); void main() { STK s; int cs,ans; char str[MAX],pre[MAX],post[MAX]; clrscr(); do /*Using Do-while Loop*/ { clrscr(); printf(" -----Program for Expressions-----"); printf(" Input The String:"); printf(" MENU: "); printf("1.Infix to Prefix "); printf("2.Infix to Postfix"); printf(" 3.Exit"); cs=getche(); switch(cs) /*Using Switch Case*/ { case 1: intopre(str,pre); break; case 2: intopost(str,post); break; case 3:

break; default: printf(" Enter a Valid Choise!"); /*Default Case*/ break; } printf(" Do you wish to Continue?(y/n)"); ans=getche(); }while(ans=='y'||ans=='Y'); /*Condition for Do-while loop*/ getch(); } /*To Input String*/ void input(char str) { printf("Enter the Infix String:"); scanf("%s",str); } /*To Covert Infix To Prefix*/ void intopre(STK s1,char str1[],char pre[]) { int len,flag; len=strlen(str1); int check=0,cnt=len-1,pos=0; char elem; while(cnt>=0) /*while condition*/ { flag=0; if(isoperand(str1[cnt])) /*Checking for Operand*/ { printf("%c",str1[cnt]); cnt--; pos++; } else { check=prcd(str1[cnt]); while(check==false) { pre[pos]=str1[cnt]; flag=1; pos++;

cnt--; } if(flag==0) { elem=pop(&s1); printf("%c",elem); } } } } /*To Convert Infix To Postfix*/ void intopost(STK s1,char str1[],char post[]) { int len; len=strlen(str1); int check=0,cnt=len-1,pos=0; } /*To Check For Operand*/ int isoperand(char sym) { if('A'<sym<'Z'||'a'<sym<'z') return(true); return(false); } /*To Check The Precedence*/ int prcd(char sym) { } /*To Display String*/ void dis(char str[]) { } /*Push Function Definition*/ void push(STK *s1,char elem) { if(!full(s1))

{ s1->top++; /*Incrementing top*/ s1->data[s1->top]=elem; /*Storing element*/ } else printf(" Stack is Full!"); } /*Full Function Definition*/ int full(STK *s2) { if(s2->top==MAX) /*Condition for Full*/ return(true); return(false); } /*Pop Function Definition*/ int pop(STK *s1) { char elem; if(!empty(s1)) { elem=s1->data[s1->top]; /*Storing top stack element in elem*/ s1->top--; /*Decrementing top*/ return(elem); } return(false); } /*Empty Function Definition*/ int empty(STK *s2) { if(s2->top==-1) /*Condition For Empty*/ return(true); return(false); }

Linked list queue example


# include <stdio.h> # include <stdlib.h> struct qnode { int data; int prio;

struct qnode *next; }; void quein(struct qnode **, struct qnode **, int , int); int quedel(struct qnode **, struct qnode **, int *, int *); int main(void) { int tab[10] = {2, 8, 3, 5, 4, 9, 6, 7, 1, 0}; struct qnode *first = NULL; struct qnode *last = NULL; int val, prio, i; for(i = 0; i < 10; i++) { val = tab[i], prio = i; printf("Inserting: value: %d with priority: %d\n", prio, val); quein(&first, &last, val, prio); } printf("=-=\n"); for(i = 0; i < 11; i++) { val = tab[i], prio = i; if(quedel(&first, &last, &val, &prio) != -1) printf("Deleting: value: %d with priority: %d\n", prio, val); } return 0; } int quedel(struct qnode **first, struct qnode **last, int *prio, int *val) { struct qnode *tmp = NULL; if((NULL == *last) && (*last == *first)) { fprintf(stderr, "Empty queue.....\n"); return -1; } *val = (*first)->data, *prio = (*first)->prio; tmp = *first, *first = (*first)->next; if(*last == tmp) *last = (*last)->next; free(tmp); return 0; }

void quein(struct qnode **first, struct qnode **last, int prio, int val) { struct qnode *tmp = NULL; struct qnode *tmp1 = NULL; tmp = malloc(sizeof(struct qnode)); tmp->data = val; tmp->prio = prio; tmp->next = NULL; if(*last == NULL) { *last = tmp; *first = *last; } else { if((*first)->prio < prio) { tmp->next = *first; *first = tmp; } else { if((*last)->prio > prio) { (*last)->next = tmp; *last = tmp; } else { tmp1 = *first; while((tmp1->next)->prio >= prio) { tmp1 = tmp1->next; } tmp->next = tmp1->next; tmp1->next = tmp; } } } return; } Program for Circular Queue implementation through Array #include <stdio.h> #include<ctype.h> # define MAXSIZE 200 int cq[MAXSIZE]; int front,rear; void main()

{ void add(int,int [],int,int,int); int del(int [],int ,int ,int ); int will=1,i,num; front = 1; rear = 1; clrscr(); printf(" Program for Circular Queue demonstration through array "); while(will ==1) { printf(" MAIN MENU: 1.Add element to Circular Queue 2.Delete element from the Circular Queue "); scanf("%d",&will); switch(will) { case 1: printf(" Enter the data... "); scanf("%d",&num); add(num,cq,MAXSIZE,front,rear); break; case 2: i=del(cq,MAXSIZE,front,rear); printf(" Value returned from delete function is %d ",i); break; default: printf(" Invalid Choice . "); } printf(" Do you want to do more operations on Circular Queue ( 1 for yes, any other key to exit) "); scanf("%d" , &will); } //end of outer while } //end of main

void add(int item,int q[],int MAX,int front,int rear) { rear++; rear= (rear%MAX); if(front ==rear) { printf(" CIRCULAR QUEUE FULL "); return; } else { cq[rear]=item; printf(" Rear = %d Front = %d ",rear,front); } } int del(int q[],int MAX,int front,int rear) { int a; if(front == rear) { printf(" CIRCULAR STACK EMPTY "); return (0); } else { front++; front = front%MAX; a=cq[front]; return(a); printf(" Rear = %d Front = %d ",rear,front); } }
in t ReverseSt r i ngchar *); ( main()

{ char *Str; printf("enter any st r i ng); " \n gets(Str); ReverseString(Str); getch(); } int ReverseString(char *rev) { int len = 0; char p; while(*rev!='\0') { len++; rev++; } rev--; while(len>0) { p = *rev; putchar(p); rev--; len--; } }

C PROGRAM FOR INSERTION #include<stdio.h> #include<conio.h> void insert(int); int delet(int); void display(void); int queue[3]; int rear=-1; int front=-1; void main() { int n=3; char op; clrscr(); do { printf("\nOptions"); printf("\nPress i for insertion"); printf("\nPress d for deletion"); printf("\nPress p for display"); printf("\nPress e for exit"); op=getche();

switch(op) { case 'i':insert(n);break; case 'd':delet(n);break; case 'p':display(); break; default:printf("\nWrong operator"); } } while(op!='e'); getch(); } /////////////////////////////////////////////////// void insert(int n) { int item; if((front==0&&rear==n)||(front==rear+1)) { printf("\nQueue over flow"); return; } if(front==-1) { front=0; rear=0; } else if(rear==n) rear=0; else rear=rear+1; printf("\nEnter the item to be inserted"); scanf("%d",&item); queue[rear]=item; } //////////////////////////////////////// int delet(int n) { int item; if(front==-1) { printf("\nQueue is empty"); return; } printf("\nYou have deleted %d",queue[front]); queue[front]=0; if(front==rear) { front=-1; rear=-1; } else if(front==n) front=0; else front=front+1; return; } /////////////////////////////////

void display(void) { int i; printf("\nDisplaying Queue\n"); for(i=0;i<3;i++) printf("\n%d",queue[i]); }

C implementation:int max; /*the maximum limit has already been set*/ static int queue[max]; int front=0, rear=-1; /*queue is initially empty*/ .. .. main( ) { .. .. addval(23); .. addval(45); .. addval(47); .. } addval( int new) { if(rear == max-1) /*step-1*/ rear = 0; /*step-2*/ else rear = rear + 1; /*step-3*/ if( front == rear && queue[front] != NULL) /*step-4*/ printf("Queue Overflow"); /*step-a*/ else queue[rear] = new; /*step-b*/ }

reverse of sentence
#include <stdio.h> char* reverse_word(char* s) { char *str = s; int len = string_len(s); int i=0; int count=0; reverse_substring(str,0,pos_lastChar(str)); while(i<=len){ if(str[i] ==' ' || i==len){ reverse_substring(str,i-count,i-1); count=0; } if(str[i]!=' '){ count++; } i++; } return str; }

int pos_lastChar(char* s) { int i; for(i=string_len(s); i>0;i--){ if (s[i]!=' ' && s[i]!='\0'){ return i; } } } reverse_substring(char* s, int start, int end) { char temp; int i,j; for(i=start, j=end; i<j; i++,j--) { temp = s[i]; s[i]=s[j]; s[j]=temp; } } int string_len(char* str) { int x=0; while(*str++){ x++; } return (x); } int main(int argc, char **argv) { char str[] = "this is very beautiful "; printf("Length of string: %d\n",string_len(str)); printf("[%s]\n", reverse_word(str)); } PERMUTATION PROGRAM

#include <stdio.h> #define SIZE 3 int main(char *argv[],int argc) { char list[3]={'a','b','c'}; int i,j,k; for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) for(k=0;k<SIZE;k++) if(i!=j && j!=k && i!=k) printf("%c%c%c\n",list,list[j],list[k]); return(0);

Das könnte Ihnen auch gefallen