Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create CircularLinked.c #32

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions DataStructures/CircularLinked.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Implementation of Circular Singly Linked List
#include<stdio.h>
#include<stdlib.h>

struct node {
int info;
struct node *next;
};

typedef struct node* nodeptr;

nodeptr list1=NULL;
nodeptr list2=NULL;

nodeptr getnode() {

nodeptr p=(nodeptr)malloc(sizeof(struct node));
if(p==NULL) {
printf("Could not create node\n");
}
else {
p->next=NULL;
return p;
}
}

void insempty(nodeptr *list,int x) {
nodeptr p=getnode();
p->info=x;
*list=p;
p->next=p; // first node should point itself on creation
}

void insbegin(nodeptr *list,int x) {

if(*list==NULL) {
insempty(list,x);
}
else {
nodeptr p=getnode();
p->info=x;
p->next=(*list)->next;
(*list)->next=p;
}
}

void insend(nodeptr *list,int x) {

if(*list==NULL) {
insempty(list,x);
}
else {
nodeptr p=getnode();
p->info=x;
p->next=(*list)->next;
(*list)->next=p;
(*list)=p;
}
}

void view(nodeptr list) {

if(list==NULL) {
printf("List is Empty\n");
return;
}

nodeptr p=list->next;
do {
printf("%d ",p->info);
p=p->next;
}while(p!=list->next);
printf("\n");
}


int main() {
insend(&list1,5);
insbegin(&list1,4);
insbegin(&list1,3);
insbegin(&list1,2);
insbegin(&list1,1);
insend(&list1,6);
view(list1);
insbegin(&list2,10);
insbegin(&list2,9);
insbegin(&list2,8);
insbegin(&list2,7);
view(list2);
return 0;
}