Write an algorithm for the implementation of a Doubly Linked List – IGNOU MCA Assignment 2017 – 18

By | December 22, 2017

MASTER OF COMPUTER APPLICATIONS

Course Code : MCS-021
Course Title : Data and File Structures
Assignment Number : MCA(2)/021/Assignment/17-18
Maximum Marks : 100
Weightage : 25%

Write an algorithm for the implementation of a Doubly Linked List – IGNOU MCA Assignment 2017 – 18

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 main()
{
clrscr();
display_list(root);
insert(10);
insert(20);
insert(30);
insert(40);
display_list(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);
}
}

 

[codesyntax lang=”php”]

#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 main()
{
clrscr();
display_list(root);
insert(10);
insert(20);
insert(30);
insert(40);
display_list(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);
}
}

[/codesyntax]

 

SCREENSHOTS:

C_program_Doubly_Linked_List

C_program_Doubly_Linked_List_Output