diff --git a/README.md b/README.md index 575fa7c..eaa688b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # basic_datastructure_programs -# This repo consists of all programs related to basic data structures. \ No newline at end of file +# This repo consists of programs related to basic data structures. diff --git a/circular_linkedlist.cpp b/circular_linkedlist.cpp index 642adb9..cbd4aa4 100644 --- a/circular_linkedlist.cpp +++ b/circular_linkedlist.cpp @@ -1,54 +1,78 @@ #include #include -//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) { @@ -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; }