Pages

RevenueHits

Search ~~~~

Tuesday, June 28, 2011

Circular Queue implementation

Circular Queue implementation using C programming.


#include<stdio.h>
int queue[10];
int lowerBound=0;
int upperBound=9;
int front=-1;
int rear=-1;
int size=0;


void addToQueue(int num)                   
{
if(size==(upperBound-lowerBound)+1) return; // queue Full
if(rear<upperBound)
{
rear++;
}
else
{
rear=lowerBound;
}


queue[rear]=num;
size++;
if(front==-1)front=0;
}


int removeFromQueue()
{
int num;
if(size==0)return 0; //queue Empty
num=queue[front];
if(front<upperBound)
{
front++;
}
else
{
front=lowerBound;
}
size--;
return num;


}
int isQueueFull()
{
return size==(upperBound-lowerBound)+1;
}


int isQueueEmpty()
{
return size==0;
}


void main()
{
int ch,num;
while(1)
{
printf("1. Add To Queue\n");
printf("2. Remove From Queue\n");
printf("3. Enter your choice");
scanf("%d",&ch);
if(ch==1)
{
if(isQueueFull())
{
printf("Queue is full\n");
}
else
{
printf("Enter number to add to queue");
scanf("%d",&num);
if(num==0)
{
printf("Cannot add zero to queue\n");
}
else
{
addToQueue(num);
printf("%d added to queue \n",num);
}
}
}
if(ch==2)
{
if(isQueueEmpty())
{
printf("Queue is empty\n");
}
else
{
num=removeFromQueue();
printf("%d removed from queue\n",num);
}
}
if(ch==3)
{
break;
}
}
}

Queue using Array


An example of queue using Array

#include<stdio.h>
int queue[10];
int lowerBound=0;
int upperBound=9;
int front=-1;
int rear=-1;
void addToQueue(int num)
{
if(rear==upperBound) return; // queue Full
rear++;
queue[rear]=num;
if(front==-1)front=0;
}
int removeFromQueue()
{
int num,i;
if(rear==-1)return 0; //queue Empty
num=queue[front];
i=0;
while(i<rear)
{
queue[i]=queue[i+1];
i++;
}
rear--;
if(rear==-1)front=-1;
return num;
}

int isQueueFull()
{
return rear==upperBound;
}

int isQueueEmpty()
{
return rear==-1;
}

void main()
{
int ch,num;
while(1)
{
printf("1. Add To Queue\n");
printf("2. Remove From Queue\n");
printf("3. Enter your choice");
scanf("%d",&ch);
if(ch==1)
{
if(isQueueFull())
{
printf("Queue is full\n");
}
else
{
printf("Enter number to add to queue");
scanf("%d",&num);
if(num==0)
{
printf("Cannot add zero to queue\n");
}
else
{
addToQueue(num);
printf("%d added to queue \n",num);
}
}
}
if(ch==2)
{
if(isQueueEmpty())
{
printf("Queue is empty\n");
}
else
{
num=removeFromQueue();
printf("%d removed from queue\n",num);
}
}
if(ch==3)
{
break;
}
}
}

Doubly Linked list


Doubly Linked list......................Using C programming.











struct Node
{
int num;
struct Node *next;
struct Node *prev;

};
struct Node *start=NULL;
struct Node *end=NULL;

void addAtEnd(int num)
{
struct Node *t;
t=(struct Node *)malloc(sizeof(struct Node));
t->num=num;
t->next=NULL;
t->prev=NULL ;
if(start==NULL)
{
start=t;
end=t;
}
else
{
end->next=t;
t->prev=end;
end=t;
}
}

void insertAtTop(int num)
{
struct Node *t;
t=(struct Node*)malloc(sizeof(struct Node));
t->num=num;
t->next=NULL;
t->prev=NULL;

if(start==NULL)
{
start=t;
end=t;
}
else
{
t->next=start;
start->next=t;
start=t;
}
}

void insertAtPosition(int num,int pos)
{
struct Node *p1;
int x;
struct Node *t;
t=(struct Node *)malloc(sizeof(struct Node));
t->num=num;
t->next=NULL;
t->prev=NULL;
x=1;
p1=start;

while(x<pos && p1!=NULL)
{
p1=p1->next;
x++;
}
if(p1==NULL)
{
if(p1==start)
{
start=t;
end=t;
}
else
{
end->next=t;
t->prev=end;
end=t;
}
}
else
{
if(p1==start)
{
t->next=start;
start->prev=t;
start=t;
}
else
{
t->next=p1;
t->prev=p1->next;
p1->prev->next=t;
p1->prev=t;
}
}
}
void removeFromPosition(int pos)
{
struct Node *p1;
int x;
if(start==NULL)
{
printf("Invalid Position\n");
return;
}
x=1;
p1=start;
while(x<pos && p1!=NULL)
{
p1=p1->next;
x++;
}
if(p1==NULL)
{
printf("Invalid Position\n");
return;
}
if(p1==start && p1==end)
{
start=NULL;
end=NULL;
}
else
{
if(p1==start)
{
start=start->next;
start->prev=NULL;
}
else
{
if(p1==end)
{
end=end->prev;
end->next=NULL;
}
else
{
p1->prev->next=p1->next;
p1->next->prev=p1->prev;
}
}
}
free(p1);
}
void traverseTopToBottom()
{
struct Node *t;
t=start;
while(t!=NULL)
{
printf("%d\n",t->num);
t=t->next;
}
}
void traverseBottomToTop()
{
struct Node *t;
t=end;
while(t!=NULL)
{
printf("%d\n",t->num);
t=t->prev;
}
}
void main()
{
int ch, num, pos;
while(1)
{
printf("1. Add a node at end\n");
printf("2. Insert a node at top\n");
printf("3. Insert a node at a position\n");
printf("4. Remove a node from a position \n");
printf("5. Traverse -Top To Bottom\n");
printf("6. Traverse - Bottom To Top\n");
printf("7. Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);

if(ch==1)
{
printf("Enter a number to add at end:");
scanf("%d",&num);
addAtEnd(num);
}
if(ch==2)
{
printf("Enter a number to insert at top");
scanf("%d",&num);
insertAtTop(num);
}
if(ch==3)
{
printf("Enter a number to insert:");
scanf("%d",&num);

printf("Enter the position where you want to insert:");
scanf("%d",&pos);
insertAtPosition(num,pos);
}
if(ch==4)
{
printf("Enter positionj of the node to remove:");
scanf("%d",&pos);
removeFromPosition(pos);

}
if(ch==5)
{
traverseTopToBottom();
}
if(ch==6)
{
traverseBottomToTop();
}
if(ch==7)
{
break;
}
}
}

Singly Link List Implementation --- C code


Singly Link List Implementation through ~ C programming





struct Node
{
int num;
struct Node *next;
};

struct Node *start=NULL ; 
void addAtEnd(int num)
{
struct Node *t,*j;
t=(struct Node *)malloc(sizeof(struct Node));
t->num=num;
t->next=NULL; 
if(start==NULL)
{
start=t;
}
else
{
j=start;
while(j->next!=NULL)
{
j=j->next;
}
j->next=t;
}
}
void insertAtTop(int num)
{
struct Node *t;
t=(struct Node *)malloc(sizeof(struct Node));
t->num=num;
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
t->next=start;
start=t;
}
}
void insertAtPosition(int num, int pos)
{
struct Node *p1,*p2;
int x;
struct Node *t;
t=(struct Node *)malloc(sizeof(struct Node));
t->num=num;
t->next=NULL;
x=1;
p1=start;
while(x<pos && p1!=NULL)
{
p2=p1;
p1=p1->next;
x++;
}
if(p1==NULL)
{
if(start==NULL)
{
start=t;
}
else
{
p2->next=t;
}
}
else
{
if(p1==start)
{
t->next=start;
start=t;
}
else
{
t->next=p1;
p2->next=t;
}
}
}
void removeFromPosition(int pos)
{
struct Node *p1,*p2;
int x;
if(start==NULL)
{
printf("Invalid Position");
return;
}
x=1;
p1=start;
while(x<pos && p1!=NULL)
{
p2=p1;
p1=p1->next;
x++;
}
if(p1==NULL)
{
printf("Invalid position\n");
return;
}
if(p1==start)
{
start=start->next;
}
else
{
p2->next=p1->next;
}
free(p1);
}
void traverseTopToBottom()
{
struct Node *t;
t=start;
while(t!=NULL)
{
printf("%d\n",t->num);
t=t->next;
}
}
void traverseBottomToTop(struct Node *t)
{
if(t==NULL)
{
return;
}
traverseBottomToTop(t->next);
printf("%d\n",t->num);
}
void main()
{
int ch,num,pos;
while(1)
{
printf("\nEnter yours choice\n");
printf("\n1. Add a node at end \n");
printf("\n2. Insert a node at top \n");
printf("\n3. Insert a node at a position\n");
printf("\n4. Remove a node from a position\n");
printf("\n5. Traverse- Top To Bottom\n");
printf("\n6. Traverse- Bottom To Top\n");
printf("\n7. Exit\n");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter a number to add at end:");
scanf("%d",&num);
addAtEnd(num);
}
if(ch==2)
{
printf("Enter a no. to insert at top:");
scanf("%d",&num);
insertAtTop(num);
}
if(ch==3)
{
printf("Enter a no. to insert:");
scanf("%d",&num);
printf("Enter the position: ");
scanf("%d",&pos);
insertAtPosition(num,pos);
}
if(ch==4)
{
printf("Enter position of the node to remove:");
scanf("%d",&pos);
removeFromPosition(pos);
}
if(ch==5)
{
traverseTopToBottom();
}
if(ch==6)
{
traverseBottomToTop(start);
}
if(ch==7)
{
break;
}
}
}