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

By | July 25, 2014

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-021
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/021/Assign/13
Assignment 2013

 

Write an algorithm for the implementation of Circular Linked Lists.

 

#include<stdio.h>
struct node
{
int data;
struct node *next;
};
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;
return temp;
}

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

Program Code:

[codesyntax lang=”c”]

#include<stdio.h>
struct node
{
int data;
struct node *next;
};
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;
return temp;
}

void insert(int x)
{
node *temp,*start;
start=root;
if(root==NULL)
{ root=create_node(x);root->next=root;}
else
{
while(start->next!=root)
{
start=start->next;
} start->next=create_node(x);
start->next->next=root;
}
}
void display_list(node *start)
{
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]

 

Screen Shots:

C_program_Cir_Link_List

 

C_program_Cir_Link_List_Out

Leave a Reply