/*Double linked list*/
#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define pf printf
#define sc scanf
using namespace std;
/*Creating node*/
struct node
{
struct node *prev;//In that section it will contain previous node address
int x;
int y;
struct node *next;
};
typedef struct node type;
type *head = (type*)malloc(sizeof(type));
type *start = head;
type *last = NULL;
//head->prev = (type*)NULL; //This is the ending node. Before that there will be no node
/*Input function*/
void takeIn()
{
int n;
pf("How many nodes you want to take in: ");
sc("%d",&n);
head->prev = NULL;
for(int i=0; i<n; i++)
{
sc("%d %d",&head->x,&head->y);
head ->next = (type*)malloc(sizeof(type));
type *temp = head;// Storing current node address
head = head->next;
head-> prev = temp; // in the previous node address storing the temp.temp was the current node address.now it is
//previous node address.
}
last = head->prev; // we are storing the last node address to the global variable so that we can use anytime we want.
head->next = NULL;
}
void printUlta()
{
type *l = last;
printf("\n");
while(l->prev)
{
printf("%d %d\n",l->x,l->y);
l = l->prev;
// break;
}
printf("%d %d\n",l->x,l->y);
}
void insert_first()
{
type *st = start;
type *newNode = (type*)malloc(sizeof(type));
pf("Enter your first node: ");
sc("%d %d",&newNode->x,&newNode->y);
type *temp = st->next;
newNode->prev = NULL;
newNode -> next = st;
st->prev = newNode;
start = newNode;
}
void insert_last()
{
type *lst = last;
type *newNode = (type*)malloc(sizeof(type));
pf("Enter Your last node: ");
sc("%d %d",&newNode->x,&newNode->y);// Taking input to the last node
newNode->next = NULL; // Making the last node last address null
newNode->prev = lst; // Making previous address as the last address
lst -> next = newNode;// Linking with the newNode. Putting that new address to the last node
last = newNode;// updating the last address to the last node
}
type *srch(int itm)
{
type *st = start;
type *noItem = NULL;
if(st->x == itm)
{
return st;
}
while(st->next)
{
if(st->next->x == itm)
{
return st->next;// Returning the current node
}
st = st->next;
}
return noItem;
}
void insert_middle()
{
int n;
pf("Enter the middle node : ");
type *newNode = (type*)malloc(sizeof(type));
sc("%d %d",&newNode->x,&newNode->y);
pf("Enter the value where you want to put that value: ");
sc("%d",&n);
type *add = (type*)srch(n);
newNode ->prev = add->prev;
newNode ->next = add;
type *temp = add->prev;
add->prev = newNode;
}
void delt()
{
int n;
pf("Enter the value which node you want to delete? :\n");
sc("%d",&n);
type *add = (type*)srch(n);
type *temp = add;
type *pn = (type*)add->prev;
type *nn = (type*)add->next;
if(add->next == NULL)
{
pn->next = NULL;
free(add);
}
else if(add->prev == NULL)
{
start->next = nn;
free(add);
}
else
{
pn->next = nn;
nn ->prev = pn;
free(add);
}
}
int main()
{
takeIn();
// printUlta();
// insert_first();
// insert_last();
// insert_middle();
printUlta();
while(1)
{
delt();
printUlta();
}
return 0;
}