Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DoublyLL.cpp #534

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
100 changes: 61 additions & 39 deletions c++/DoublyLL.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,82 @@
#include <iostream>
using namespace std;

// Definition of Node class for Doubly Linked List
class Node
{
public:
Node *previous;
int data;
Node *next;
};
Node *Insert(int pos,int x, Node *head)

// Function to insert a node at a given position
Node *Insert(int pos, int x, Node *head)
{
Node *t,*p;
if(pos ==0)
{
t=new Node;
t->data=x;
t->previous=NULL;
t->next=head;
if(head !=NULL)
head->previous=t;
head=t;
}
else
{
p=head;
for(int i=0;i<pos-1;i++)
p=p->next;
t=new Node;
t->data=x;
t->previous=p;
t->next=p->next;
if(p->next)
p->next->previous=t;
p->next=t;
}
return head;
Node *t, *p;
// Insert at the beginning (position 0)
if (pos == 0)
{
t = new Node;
t->data = x;
t->previous = NULL; // New node's previous is NULL
t->next = head; // New node points to current head

if (head != NULL)
head->previous = t; // Update the old head's previous pointer to the new node

head = t; // Update head to the new node
}
// Insert at any position other than 0
else
{
p = head;
for (int i = 0; i < pos - 1; i++)
p = p->next; // Traverse to the node before the insertion point

t = new Node;
t->data = x;
t->previous = p; // New node's previous points to current node
t->next = p->next; // New node's next points to current node's next

if (p->next)
p->next->previous = t; // Update the next node's previous pointer to the new node

p->next = t; // Update current node's next to the new node
}
return head; // Return updated head pointer
}

// Function to display the doubly linked list
void display(Node *head)
{
Node *p=head;
while(p!=NULL)
Node *p = head;
while (p != NULL)
{
cout<<p->data<<" ";
p=p->next;
cout << p->data << " ";
p = p->next; // Move to the next node
}
cout << endl; // Print newline after the list is displayed
}

int main()
{
Node *first=NULL;
int n,x,pos;
cout<<"Enter the number of elements in the list: ";
cin>>n;
for(int i=0;i<n;i++)
Node *first = NULL; // Initialize the head of the linked list
int n, x, pos;

// Input the number of elements to insert
cout << "Enter the number of elements in the list: ";
cin >> n;

// Insert elements into the list
for (int i = 0; i < n; i++)
{
cin>>x;
first=Insert(i,x,first);
cin >> x; // Input value to insert
first = Insert(i, x, first); // Insert at position i
}

// Display the resulting list
display(first);
return 0;
}

return 0;
}