Author Topic: Simple C Program for Queue using  (Read 4770 times)

bbasujon

  • Administrator
  • VIP Member
  • *****
  • Posts: 1827
  • I want to show my performance at any where
    • View Profile
    • Higher Education
Simple C Program for Queue using
« on: January 28, 2012, 08:59:33 AM »
Queue Program using Linked List in C

#include<malloc.h>
#include<stdio.h>
struct node{
int value;
struct node *next;
};

void Init(struct node *n){
n->next=NULL;
}
void Enqueue(struct node *root,int value){
struct node *j=(struct node*)malloc(sizeof(struct node));
j->value=value;
j->next=NULL;
struct node *temp ;
temp=root;
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=j;
printf(“Value Enqueued is : %dn”,value);

}
void Dequeue(struct node *root)
{
if(root->next==NULL)
{
printf(“No Element to Dequeuen”);
}
else
{
struct node *temp;
temp=root->next;
root->next=temp->next;
printf(“Value Dequeued is : %dn”,temp->value);
free(temp);
}
}

void main()
{
struct node sample_queue;
Init(&sample_queue);
Enqueue(&sample_queue,10);
Enqueue(&sample_queue,50);
Enqueue(&sample_queue,570);
Enqueue(&sample_queue,5710);
Dequeue(&sample_queue);
Dequeue(&sample_queue);
Dequeue(&sample_queue);
}
Acquire the knowledge and share the knowledge so that knowing,learning then sharing - all are the collection