Code for inserting elements in doubly linked list (#67)

* Code for inserting elements in doubly linked list

* Circular linked list added
pull/71/head
Vinayak Ravi Joshi 2021-02-09 18:54:16 +05:30 committed by GitHub
parent d28751e92e
commit 98d54a67d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 0 deletions

View File

@ -5,6 +5,10 @@
1. [Singly Linked List](c-or-cpp/singly.cpp)
2. [Reversing Linked List](c-or-cpp/reverse-linkedlist.cpp)
2. [Doubly Linked List](c-or-cpp/doubly.cpp)
3. [Circular Linked List](c-or-cpp/circular.cpp)
### Java
1. [Singly Linked List](java/singly.cpp)

View File

@ -0,0 +1,39 @@
#include <iostream>
using namespace std;
struct Node {
   int data;
   struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata) {
   struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
   struct Node *ptr = head;
   newnode->data = newdata;
   newnode->next = head;
   if (head!= NULL) {
      while (ptr->next != head)
      ptr = ptr->next;
      ptr->next = newnode;
   } else
   newnode->next = newnode;
   head = newnode;
}
void display() {
   struct Node* ptr;
   ptr = head;
   do {
      cout<<ptr->data <<" ";
      ptr = ptr->next;
   } while(ptr != head);
}
int main() {
   insert(3);
   insert(1);
   insert(7);
  insert(3);
   insert(1);
   insert(7);
   cout<<"The circular linked list is: ";
   display();
   return 0;
}

View File

@ -0,0 +1,36 @@
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct Node {
   int data;
   struct Node *prev;
   struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata) {
   struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
   newnode->data = newdata;
   newnode->prev = NULL;
   newnode->next = head;
   if(head != NULL)
   head->prev = newnode ;
   head = newnode;
}
void display() {
   struct Node* ptr;
   ptr = head;
   while(ptr != NULL) {
      cout<< ptr->data <<" ";
      ptr = ptr->next;
   }
}
int main() {
   insert(3);
   insert(1);
   insert(7);
   insert(2);
   insert(9);
   cout<<"The doubly linked list is: ";
   display();
   return 0;
}