/* Program to Demonstrate Circular single linked list */
#include <stdio.h>
#include <stdlib.h>
/* Circular single linked list */
struct clist
{
int data;
struct clist *next;
};
typedef struct clist *CL;
#include <stdlib.h>
/* Circular single linked list */
struct clist
{
int data;
struct clist *next;
};
typedef struct clist *CL;
/* inserting elements into Circular list just after first(head) node */
void insert(CL *head)
{
CL temp;
int e;
temp=malloc(sizeof(struct clist));
if(temp == NULL){
printf("Can't allocate memory\n");
return;
}
printf("Enter element\n");
scanf("%d", &e);
temp->data = e;
if(*head == NULL){
*head = temp;
(*head)->next = temp;
} else {
temp->next = (*head)->next;
(*head)->next =temp;
*head =temp;
}
}
/* Displaying elements in list */
void display(CL head)
{
CL temp;
if(head == NULL) {
printf("Empty list \n");
return;
}
temp = head;
printf("Elements are \n");
do {
/* it circular list after head->next is other end of the list*/
printf("%d\n", temp->data);
temp=temp->next;
} while (temp != head);
}
/* deleting the element right after the root node from circular
* single linked list
*/
void del(CL *head)
{
CL temp;
if(*head == NULL){
printf("Empty list ..\n");
return;
}
temp = (*head)->next;
printf("Element deleted %d", temp->data);
if( temp != *head){
(*head)->next = temp->next;
free(temp);
} else {
free(*head);
*head = NULL;
}
}
/* for main program you could refer below post to test above program */
Comments
Post a Comment