Skip to content

Instantly share code, notes, and snippets.

@murikadan
Created August 26, 2012 11:11
Show Gist options
  • Save murikadan/3477627 to your computer and use it in GitHub Desktop.
Save murikadan/3477627 to your computer and use it in GitHub Desktop.
Linked List implementation of quque
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node* next;
};
void enqueue(struct node**,struct node **,int);
void dequeue(struct node**,struct node **);
void traverse(struct node*,struct node*);
void free_queue(struct node*);
int main()
{
int key;
struct node *front=NULL,*rear=NULL;
FILE *fp;
fp=fopen("input","r");
fscanf(fp,"%d",&key);
while(!feof(fp))
{
enqueue(&front,&rear,key);
fscanf(fp,"%d",&key);
}
traverse(front,rear);
dequeue(&front,&rear);
dequeue(&front,&rear);
traverse(front,rear);
free_queue(front);
return 0;
}
void enqueue(struct node** front,struct node** rear,int num)
{
struct node* newnode=malloc(sizeof(struct node));
newnode->val=num;
newnode->next=NULL;
if(*front==NULL)
*front=*rear=newnode;
else
{
(*rear)->next=newnode;
(*rear)=newnode;
}
}
void dequeue(struct node** front,struct node** rear)
{
struct node* temp=NULL;
if(*front==*rear)
{
temp=*front;
*front=*rear=NULL;
}
else if(*front!=NULL)
{
temp=*front;
*front=(*front)->next;
}
if(temp!=NULL)
free(temp);
}
void free_queue(struct node* front)
{
while(front!=NULL)
{
free(front);
front=front->next;
}
}
void traverse(struct node* front,struct node* rear)
{
struct node* temp=front;
while(temp!=NULL)
{
printf("%d",temp->val);
if(temp!=rear)
printf("--->");
else
printf("\n");
temp=temp->next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment