Skip to content

Instantly share code, notes, and snippets.

@muhammedeminoglu
Created May 20, 2017 19:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save muhammedeminoglu/655f4538eabac253965f998ef94ee168 to your computer and use it in GitHub Desktop.
Save muhammedeminoglu/655f4538eabac253965f998ef94ee168 to your computer and use it in GitHub Desktop.
Queue implementatih on with Linked Lists
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node{
char name[20];
char secondName[20];
int age;
struct node *next;
};
struct node* head = NULL;
struct node* tail = NULL;
struct node* temp;
char firstName[20], secondName[20];
int age;
struct node* createPerson(char name[], char secondName[], int age)
{
struct node* newPerson = (struct node*)malloc(sizeof(struct node));
strcpy(newPerson->name, name);
strcpy(newPerson->secondName, secondName);
newPerson->age = age;
newPerson->next = NULL;
return newPerson;
};
void enQueuePerson(char name[], char secondName[], int age)
{
struct node* person = createPerson(name, secondName, age);
if(head == NULL && tail == NULL)
{
head = person;
tail = person;
}
else
{
tail->next = person;
tail = person;
}
}
void deQueue()
{
temp = head;
if(head == NULL)
{
printf("\nQueue is empty, pls Enqueue");
return;
}
if(head == tail)
{
head = NULL;
tail = NULL;
return;
}
head = temp->next;
free(temp);
}
struct node* whoNext()
{
if(head == NULL)
{
printf("\nThere is no item in queue...");
return 0;
}
return head;
}
void printQueue()
{
int i = 1;
if(head == NULL)
{
return;
}
temp = head;
while(temp->next != NULL)
{
printf("\n%d. Position => %s %s %d ", i, temp->name, temp->secondName, temp->age);
temp = temp->next;
i++;
}
printf("\n%d. Position => %s %s %d ", i, temp->name, temp->secondName, temp->age);
}
void menu()
{
int choise;
while( 1 == 1 )
{
printf("\n 1- Enqueue ... ");
printf("\n 2- Dequeue ... ");
printf("\n 3- Who's next? ");
printf("\nmake your choise ");
scanf("%d", &choise);
selection(choise);
}
}
void selection(int chosen)
{
switch(chosen)
{
case 1:
printf("\n Enter First Name ... ");
scanf("%s", &firstName);
printf("\n Enter Second Name ... ");
scanf("%s", &secondName);
printf("\n Enter Age ... ");
scanf("%d", &age);
enQueuePerson(firstName, secondName, age);
printQueue();
break;
case 2:
deQueue();
printQueue();
break;
case 3:
temp = whoNext();
printf("\n ****************** \n");
if(temp != NULL)
{
printf("%s %s %d", temp->name, temp->secondName, temp->age);
}
break;
}
}
int main()
{
menu();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment