diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ffb633 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.vscode/ +* +!/**/ +!*.* +*.in +*.out \ No newline at end of file diff --git a/README.md b/README.md index 575fa7c..b879e51 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# basic_datastructure_programs -# This repo consists of all programs related to basic data structures. \ No newline at end of file +# Basic Datastructure Programns + +This repo consists of some programs related to basic data structures. \ No newline at end of file diff --git a/circular_linkedlist.cpp b/circular_linkedlist.cpp index 642adb9..2309097 100644 --- a/circular_linkedlist.cpp +++ b/circular_linkedlist.cpp @@ -1,72 +1,78 @@ -#include -#include +#include + +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; }