Skip to content

Added function for deleting node in circular linked list #18

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 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# basic_datastructure_programs
# This repo consists of all programs related to basic data structures.
# This repo consists of programs related to basic data structures.
90 changes: 60 additions & 30 deletions circular_linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,78 @@
#include<stdio.h>
#include<malloc.h>
//Declaring a node.
//Declaring a Node.
struct Node
{
int data; //data part of node.
struct Node *link; //pointer to next node.
int data; //data part of Node.
struct Node *link; //pointer to next Node.
};

struct Node *header; //First Node of list.(global)


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 *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)
{
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;
header=temp;
}
temp->link=header;

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;

}

//DELETES A NODE POINTED BY POINTER P
int deletep(struct Node *p)
{
if(header == NULL)return -2;
Node* temp = header;
if(p == header)
{
int x = header->data;
header = NULL;
return x;
}

while(temp->link != p )
{
temp = temp->link;
}

temp->link = p->link;
p->link = NULL;
int y = p->data;
p = NULL;
return y;

}

//display of linkedlist
void displaylist()
{
struct Node* temp=header;
printf("\nElements Of List :");
printf("%d",temp->data);
printf("%d ",temp->data);
temp=temp->link;
while(temp!=header)
{
Expand All @@ -68,5 +92,11 @@ int main()
scanf("%d",&num);
createlist(num);
displaylist();
printf("\n");
//deletes third node
Node* temp = header->link->link;
int x = deletep(temp);
printf("\n%d deleteed\n",x);
displaylist();
return 0;
}