Skip to content

Refactored circular_linkedlist.cpp #21

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.vscode/
*
!/**/
!*.*
*.in
*.out
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# basic_datastructure_programs
# This repo consists of all programs related to basic data structures.
# Basic Datastructure Programns

This repo consists of some programs related to basic data structures.
126 changes: 66 additions & 60 deletions circular_linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,78 @@
#include<stdio.h>
#include<malloc.h>
#include <iostream>

using namespace std;

//Declaring a node.
struct Node
{
int data; //data part of node.
struct Node *link; //pointer to next node.
struct Node {
int data; //data part of node.
struct Node* next; //pointer to next node.
};

struct Node *header; //First Node of list.(global)
struct Node* createNode(int value) {
struct Node* node;
node = (struct Node*)malloc(sizeof(struct Node)); //allocating memory to node.

if (node == NULL) {
cout << "Overflow" << endl;
; //if there is no space in memory
} else {
node->data = value;
node->next = NULL;
}

void createlist(int n)
{
struct Node *temp=header;
temp=(struct Node*)malloc(sizeof(struct Node)); //allocating memory to first node.
if(temp==NULL)
printf("Overflow"); //if there is no space in memory
temp->link=NULL;
int i=1;
printf("Enter The Value Of Node%d: ",i);
scanf("%d",&temp->data);
if(header==NULL)
{
temp->link=NULL;
header=temp;
}
i=i+1;
while(i<=n)
{
struct Node* temp1=temp;
temp=(struct Node*)malloc(sizeof(struct Node)); //allocating memory to ith node.
if(temp==NULL)
printf("Overflow");
printf("Enter The Value for Node%d: ",i);
scanf("%d",&temp->data);
temp1->link=temp;
temp->link=NULL;
i=i+1;
}
temp->link=header;

}
return node;
}

struct Node* createList() {
struct Node* header = (struct Node*)malloc(sizeof(struct Node)); //allocating memory to node.
if (header == NULL) {
cout << "Overflow" << endl; //if there is no space in memory
} else {
header->data = 0;
header->next = NULL;
}

//display of linkedlist
void displaylist()
{
struct Node* temp=header;
printf("\nElements Of List :");
printf("%d",temp->data);
temp=temp->link;
while(temp!=header)
{
printf("%d ",temp->data);
temp=temp->link;
}

}
return header;
}

void insert(struct Node*& list, int value) {
struct Node* v = list;

struct Node* n = createNode(value);
n->next = list;

if (list->next != NULL) { //list is empty

while (v->next != list) {
v = v->next;
cout << v->data << endl;
}
v->next = n;
} else {
n->next = n;
list = n;
}
}

//display of linkedlist
void displaylist(struct Node* header) {
struct Node* v = header;
do {
cout << v->data << " ";
v = v->next;
} while (v != header);
}

//Main Body
int main()
{
int num;
printf("Enter The Number Of Nodes For Linkedlist: ");
scanf("%d",&num);
createlist(num);
displaylist();
return 0;
int main() {
struct Node* list = createList();

insert(list, 3);
insert(list, 1);
insert(list, 7);
insert(list, 2);
insert(list, 9);

displaylist(list);
return 0;
}