Skip to content
Open
Show file tree
Hide file tree
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
Binary file added Linkedlist🤐/a.out
Binary file not shown.
149 changes: 149 additions & 0 deletions Linkedlist🤐/db_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include <iostream>
#include <malloc.h>
using namespace std;
int count = 0;
//Only Create functions are created, deletion will be added soon ...
struct dblink
{
int data;
struct dblink *next, *prev;
} *node, *head, *last, *temp;

void create_at_begin()
{
node = (struct dblink *)malloc(sizeof(struct dblink));
cout << "Enter data : ";
cin >> node->data;
node->next = node->prev = NULL;
if (head == NULL)
head = last = node;
else
{
node->next = head;
head->prev = node;
head = node;
}
}

void create_at_last()
{
node = (struct dblink *)malloc(sizeof(struct dblink));
cout << "Enter data : ";
cin >> node->data;
node->next = node->prev = NULL;
if (head == NULL)
head = last = node;
else
{
last->next = node;
node->prev = last;
last = node;
}
}

void counti()
{
count = 0;
temp = head;
while (temp != NULL)
{
temp = temp->next;
count++;
}
}

void create_at_mid()
{
counti();
int pos;
cout << "Enter position : ";
cin >> pos;
if (pos == 1)
{
create_at_begin();
return;
}
else if (pos == count - 1)
{
create_at_last();
return;
}
else if (pos > count && pos < 0)
{
cout << "Invalid position\n";
return;
}
else
{
node = (struct dblink *)malloc(sizeof(struct dblink));
cout << "Enter data : ";
cin >> node->data;
node->next = node->prev = NULL;
if (head == NULL)
{
head = last = node;
}
else
{
int p = 1;
temp = head;
while (p < pos)
{
p++;
temp = temp->next;
}
// node->next=temp->next;
// temp->next=node;
// node->prev=temp;
// temp->next->next->prev=node;
temp->next=node;
temp->next->prev=node;

node->next=temp->next->next;
node->prev=temp;
}
}
}
void display()
{
temp = head;
cout << "From Head\n";
while (temp != NULL)
{
cout << "Data is " << temp->data << endl;
temp = temp->next;
}
cout << "From Last \n";
temp = last;
while (temp != NULL)
{
cout << "Data is " << temp->data << endl;
temp = temp->prev;
}
}

int main()
{
int ch;
cout << "1.Begin\n2.Last\n3.Mid\n4.Display\nEnter choice : ";
cin >> ch;
switch (ch)
{
case 1:
create_at_begin();
main();
break;
case 2:
create_at_last();
main();
break;
case 3:
create_at_mid();
main();
break;
case 4:
display();
main();
break;
}
}
Binary file added Linkedlist🤐/singly_linked_list
Binary file not shown.
146 changes: 146 additions & 0 deletions Linkedlist🤐/singly_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include<iostream>
#include<malloc.h>
using namespace std;
int count;
int pos;
//I am more comforbtable with struct but I am using Class for practice
class Node{
public:
int data;
Node *next;
}*node,*last,*temp,*head,*previous;

void create_at_begin()
{
node = (Node *) malloc (sizeof(Node));
cout<<"Enter Data ";
cin >> node->data;
node->next = NULL;
if(head == NULL)
head=last=node;
else{
node->next = head;
head = node;
}
}

void create_at_end()
{
node= (Node*) malloc (sizeof(Node));
cout<<"Enter Data at Last.";
cin>>node->data;
node->next=NULL;
if(head==NULL)
head=last=node;
else
{
last->next=node;
last=node;
}
}

void counti(){
temp=head;
while(temp!=NULL){
count++;
temp=temp->next;
}
}

void create_at_mid(){
int pos;
cout << "Enter position to enter : ";
cin >> pos;
node= (Node*) malloc (sizeof(Node));
cout<<"Enter Data at "<<pos<<" : ";
cin>>node->data;
node->next=NULL;
counti();
if(pos==1){
create_at_end();
return;
}
else if (pos==count){
create_at_begin();
return;
}
if (pos > count || pos < 1){
cout << "invalid Position\n";
return;
}
else{
temp = head;
for(int i = 1; i <= pos - 1; i++){
temp = temp->next;
}
node->next = temp->next;
temp->next = node;
}

}

void display()
{
temp=head;
while(temp!=NULL)
{
cout << "Data = "<<temp->data<<endl;
temp=temp->next;
}
}

void delete_from_mid()
{ int ch;
int prev;
cout<<"Enter which node you want to delete: ";
cin>>pos;
temp = head;
while(temp!=NULL)
prev++;
if(prev = pos-1){
previous->next=temp->next;
}
delete temp;
}

void delete_from_front()
{
temp=head;
head = head->next;
delete temp;
}

int main()
{
int ch;
while(1){
cout<<"1. Add at BEGIN."<<endl;
cout<<"2. Add At LAST."<<endl;
cout<<"3. Add At Middle"<<endl;
cout<<"4. Delete."<<endl;
cout<<"5. Display."<<endl;
cout<<"Enter choice: ";
cin>>ch;
switch (ch)
{
case 1:
create_at_begin();
break;
case 2:
create_at_end();
break;
case 3:
create_at_mid();
break;
case 4:
delete_from_mid();
break;
case 5:
display();
break;
default:
cout<<"Incorrect Answer"<<endl;
return 0;
}
}
}
Loading