A C program and algorithm to implement Circular Doubly Linked List – IGNOU MCA Assignment 2014 – 15

By | July 27, 2014

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-021
Course Title : Data and File Structures
Assignment Number : MCA(2)/021/Assign/2014-15
Maximum Marks : 100
Weightage : 25%

 

Write an algorithm for the implementation of Circular Doubly Linked Lists.

 

#include<stdio.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
typedef struct node node; //with this Use “node” instead of “struct node”
node *root=NULL; //Global variable “root” pointer
node *create_node(int); //Declaration
void insert(int); //Declaration
void display_list(node *); //Declaration
void display_list_rev(node *);

void main()
{
clrscr();
display_list(root);
insert(10);
insert(20);
insert(30);
insert(40);
display_list(root);
display_list_rev(root);
getch();
}

node *create_node(int x)
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=x;
temp->next=NULL;
temp->prev=NULL;
return temp;
}

void insert(int x)
{
node *start;
start=root;
if(root==NULL)
{ root=create_node(x);root->next=root;root->prev=root;}
else
{
while(start->next!=root)
{
start=start->next;
} start->next=create_node(x);
start->next->next=root;
start->next->prev=start;
}
}
void display_list(node *start)
{
printf(“\nLink List :-\n”);
if(start==NULL)
{ printf(“List is Empty!\n”); }
else
{
while(start->next!=root)
{ printf(“%d->”,start->data);
start=start->next;
}printf(“%d->”,start->data);
}
}

void display_list_rev(node *start)
{
printf(“\n\nLink List (Reverse):-\n”);
if(start==NULL)
{ printf(“List is Empty!\n”); }
else
{ while(start->next!=root)
{
start=start->next;
}
while(start->prev!=root)
{ printf(“%d->”,start->data);
start=start->prev;
}printf(“%d->”,start->data);
start=start->prev;
printf(“%d->”,start->data);
}
}

Program Code:

#include<stdio.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
typedef struct node node; //with this Use "node" instead of "struct node"
node *root=NULL; //Global variable "root" pointer
node *create_node(int); //Declaration
void insert(int); //Declaration
void display_list(node *); //Declaration
void display_list_rev(node *);
void main()
{
clrscr();
display_list(root);
insert(10);
insert(20);
insert(30);
insert(40);
display_list(root);
display_list_rev(root);
getch();
}
node *create_node(int x)
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=x;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
void insert(int x)
{
node *start;
start=root;
if(root==NULL)
{ root=create_node(x);root->next=root;root->prev=root;}
else
{
while(start->next!=root)
{
start=start->next;
} start->next=create_node(x);
start->next->next=root;
start->next->prev=start;
}
}
void display_list(node *start)
{
printf("\nLink List :-\n");
if(start==NULL)
{ printf("List is Empty!\n"); }
else
{
while(start->next!=root)
{ printf("%d->",start->data);
start=start->next;
}printf("%d->",start->data);
}
}
void display_list_rev(node *start)
{
printf("\n\nLink List (Reverse):-\n");
if(start==NULL)
{ printf("List is Empty!\n"); }
else
{ while(start->next!=root)
{
start=start->next;
}
while(start->prev!=root)
{ printf("%d->",start->data);
start=start->prev;
}printf("%d->",start->data);
start=start->prev;
printf("%d->",start->data);
}
}

Screen Shots:

C_program_Cir_Doub_Link_List

C_program_Cir_Doub_Link_List_Out

Leave a Reply