/*
Name: Singly Circular Linked List
Description: Includes insertion deletion modules
*/
#include<stdio.h>
#include<conio.h>
struct linklist
{
int data;
struct linklist *next;
};
typedef struct linklist node;
node *create_node(int val)
{
node *p;
p=(node*)malloc(sizeof(node));
p->data=val;
return p;
}
void create_list_b(int val,node **head,node **tail)//Adding contents at the beginning
{
node *p=create_node(val);
if(*head==NULL)
(*head)=(*tail)=p;
else
{
p->next=(*head);
(*head)=p;
}
(*tail)->next=(*head);
}
void create_list_e(int val,node **head,node **tail)//Adding contents at the End
{
node *p=create_node(val);
if(*head==NULL)
{
(*head)=(*tail)=p;
(*tail)->next=(*head);
}
else
{
p->next=(*tail)->next;
(*tail)->next=p;
(*tail)=p;
}
}
void print(node *head,node *tail)
{
if(head==NULL)
printf("No elements in the list\n");
else
{
while(head!=tail)
{
printf("%d \t",head->data);
head=head->next;
}
printf("%d\n",head->data);
}
}
void del_end(node **head,node **tail)
{
node *start;
if(*head==NULL)
printf("Underflow\n");
else
{
printf("Deleting : %d\n",(*tail)->data);
if((*head)==(*tail))
{
free((*head));
*head=*tail=NULL;
}
else
{
start=*head;
while(start->next!=(*tail))
start=start->next;
start->next=(*tail)->next;
free((*tail));
(*tail)=start;
}
}
}
void del_beg(node **head,node **tail)
{
node *start;
if(*head==NULL)
printf("Underflow\n");
else
{
printf("Deleting : %d\n",(*head)->data);
if((*head)==(*tail))
{
free((*head));
*head=*tail=NULL;
}
else
{
start=*head;
(*head)=(*head)->next;
free(start);
(*tail)->next=(*head);
}
}
}
int main()
{
node *head=NULL,*tail=NULL;
create_list_b(3,&head,&tail);
create_list_b(2,&head,&tail);
create_list_b(1,&head,&tail);
create_list_e(0,&head,&tail);
create_list_e(4,&head,&tail);
print(head,tail);
del_end(&head,&tail);
print(head,tail);
del_end(&head,&tail);
print(head,tail);
del_beg(&head,&tail);
print(head,tail);
del_beg(&head,&tail);
print(head,tail);
getche();
}
0 comments:
Post a Comment