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: