Skip to content
Open
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
44 changes: 20 additions & 24 deletions data structures/cpp/Doubly_linked_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ class node
int data;
struct node *prev;
struct node *next;
node(int d1, node* p = nullptr, node* n = nullptr) :
data(d1),
prev(p),
next(n) {}
};
//functionto create ll
struct node *create_dll(struct node *head)
{
struct node *newnode = new struct node;
newnode->next = NULL;
newnode->prev = NULL;
cin >> newnode->data;
tail = newnode;
if (head == NULL)
{
int input;
cin >> input;
struct node *newnode = new struct node(input);
if (head == nullptr) {
return newnode;
}
else
{
struct node *p = head;
while (p->next != NULL)
{
} else {
node *p = head;
while (p->next != nullptr) {
p = p->next;
}
newnode->prev = p;
Expand All @@ -36,21 +34,19 @@ struct node *create_dll(struct node *head)
//function to insert node at head
struct node *insert_at_beg_in_dll(struct node *head)
{
struct node *newnode = new struct node;
newnode->next = NULL;
newnode->prev = NULL;
cin >> newnode->data;
if (head == NULL)
{
int input_data;
cin >> input_data;

// Creating a new node with input data using the constructor
node *newnode = new node(input_data);

if (head == nullptr) {
return newnode;
}
else
{
} else {
head->prev = newnode;
newnode->next = head;
head = newnode;
return head;
}
}
//function to to insert element at end
struct node *insert_at_end_in_dll(struct node *head)
Expand Down Expand Up @@ -140,4 +136,4 @@ int main()
cin >> t;
}
return 0;
}
}