Code for inserting elements in doubly linked list (#67)
* Code for inserting elements in doubly linked list * Circular linked list addedpull/71/head
parent
d28751e92e
commit
98d54a67d1
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue