Sunday, January 04, 2015

Working With Doubly Circular Linked List

Posted By: BackBenchers World - Sunday, January 04, 2015

Share

& Comment

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
struct node *lpt;
int info;
struct node *rpt;
}*first;

void create()
{
struct node *ptr, *cpt;
char ch;

ptr=(struct node*)malloc(sizeof(struct node));    //Allocating memory to ptr

printf("Enter the information of first node: ");
scanf("%d",&ptr->info);

first=ptr;                                       //Initializing 'first'

do
{
cpt=(struct node*)malloc(sizeof(struct node));    //Allocating memory to cpt

printf("\nEnter the information of next node: ");
scanf("%d",&(cpt->info));

ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;

printf("\nPress <Y> for more nodes\n");
ch=getch();
}
while(ch=='y'||ch=='Y');

ptr->rpt=first;
first->lpt=ptr;
}

void ftraverse()
{
struct node *ptr;
ptr=first;
if(ptr==NULL) printf("List is empty");

else
{
printf("\nThe Elements in the list are: ");
while(ptr->rpt!=first)
{
printf("\t%d",ptr->info);
ptr=ptr->rpt;
}
printf("\t%d",ptr->info);
}
}

void rtraverse()
{
struct node *ptr;
ptr=first;

if(ptr==NULL) printf("List is empty");

else
{
ptr=ptr->lpt;

printf("\nThe Elements in the list are: ");
while(ptr!=first)
{
printf("\t%d",ptr->info);
ptr=ptr->lpt;
}
printf("\t%d",ptr->info);

}
}

void binsert()
{
struct node *ptr, *cpt;
cpt=first->lpt;

ptr=(struct node*)malloc(sizeof(struct node));    //Allocating memory to ptr

printf("Enter the information of node: ");
scanf("%d",&ptr->info);

ptr->lpt=cpt;
ptr->rpt=first;
first->lpt=ptr;
cpt->rpt=ptr;
first=ptr;
ftraverse();
}

void einsert()
{
struct node *ptr, *cpt;
cpt=first->lpt;

ptr=(struct node*)malloc(sizeof(struct node));    //Allocating memory to ptr

printf("Enter the information of node: ");
scanf("%d",&ptr->info);

cpt->rpt=ptr;
ptr->lpt=cpt;
cpt=ptr;
cpt->rpt=first;
first->lpt=cpt;
ftraverse();
}

void dinsert()
{
struct node *ptr, *cpt, *tpt;
int data;
cpt=first;

printf("Enter the node information after which you want to insert: ");
scanf("%d",&data);

while(cpt->info!=data)
{
cpt=cpt->rpt;
}
tpt=cpt->rpt;

ptr=(struct node*)malloc(sizeof(struct node));    //Allocating memory to ptr

printf("Enter information of node to be inserted: ");
scanf("%d",&ptr->info);

cpt->rpt=ptr;
ptr->lpt=cpt;
ptr->rpt=tpt;
tpt->lpt=ptr;
ftraverse();

}

void bdel()
{
printf("Try this yourself");
}

void edel()
{
printf("Try this yourself");
}

void ddel()
{
printf("Try this yourself");
}


void main()
{
clrscr();
char ch=1;
char choice;
printf("Working with Data Strucrure\n\n");
printf("Menu\n\n1. Create\n2. Insert at Beginning\n3. Insert After Given Info Node\n");
printf("4. Insert at End\n5. Delete Node From Beginning\n6. Delete Given Info Node\n");
printf("7. Delete Node From End\n8. Farward Traverse\n9. Reverse Traverse\n0. Exit");

while(ch)
{
printf("\n\nEnter your choice: \n");
choice=getch();

switch(choice)
{
case '1': create();
break;

case '2': binsert();
break;

case '3': dinsert();
break;

case '4': einsert();
break;

case '5': bdel();
break;

case '6': ddel();
break;

case '7': edel();
break;

case '8': ftraverse();
break;

case '9': rtraverse();
break;

case '0': return;

default: printf("Wrong choice");
}
}
}

About BackBenchers World

Techism is an online Publication that complies Bizarre, Odd, Strange, Out of box facts about the stuff going around in the world which you may find hard to believe and understand. The Main Purpose of this site is to bring reality with a taste of entertainment

0 comments:

Post a Comment

Copyright © 2013 TechDotHunter™ is a registered trademark.

Designed by Templateism. Hosted on Blogger Platform.