Created
August 4, 2018 10:35
-
-
Save rafiulgits/cbe9d68b1343a3adbb3d541e1187c37a to your computer and use it in GitHub Desktop.
Circular Singly Linked List with C. Application:: Round Robin Scheduler in OS for time sharing method and many more real life application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Circular Singly Linked list | |
* @copyright: Rafiul Islam | |
* | |
*/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
struct List | |
{ | |
int value; | |
struct List *next; | |
}; | |
/* | |
* This program need two List pointer, one for point the head of the list. | |
* Another for link the tail value. Tail pointer will always link head as | |
* its next location for circular traverse. | |
*/ | |
struct List *head, *tail; | |
void add(int item); | |
void pop(); | |
void showTop(); | |
void main() | |
{ | |
head = tail = NULL; //initially both are NULL | |
showTop(); | |
add(45); | |
showTop(); | |
add(78); | |
add(97); | |
showTop(); | |
pop(); | |
showTop(); | |
pop(); | |
showTop(); | |
} | |
void add(int item) | |
{ | |
struct List *member = (struct List*)malloc(sizeof(struct List)); | |
member->value = item; | |
/* | |
* For first time of insertion in list make the member tail of the list. | |
* First member always link itself as its next member for circulation | |
* maintain. Otherwise new member will link its next to head and tail will | |
* link this new member as tail next. Finally head will link this new member. | |
*/ | |
if(head == NULL) | |
{ | |
tail = member; | |
member->next = member; | |
} | |
else | |
{ | |
member->next = head; | |
tail->next = member; | |
} | |
head = member; | |
} | |
void pop() | |
{ | |
/* | |
* Access the last member of the list, change the head link to previous one, | |
* link tail next to updated head and then free the current member. | |
*/ | |
struct List *member = (struct List*)malloc(sizeof(struct List)); | |
member = head; | |
head = member->next; | |
tail->next = head; | |
free(member); | |
} | |
void showTop() | |
{ | |
if(head == NULL) | |
{ | |
puts("No Element !\n-------------------------------"); | |
} | |
else | |
{ | |
printf("Head value: %d\n",head->value); | |
printf("Tail value: %d\n",tail->value); | |
printf("Tail Linked value: %d\n",tail->next->value); | |
puts("-------------------------------------------"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment