#include<stdio.h>
#include<stdlib.h>
struct node
{
int x,y;
struct node *p;
};
typedef struct node pt;
pt *head = (pt*)malloc(sizeof(pt));
pt *start = head;
void in(int n)
{
for(int i=0; i<n; i++)
{
scanf("%d %d",&head->x,&head->y);
head->p = (pt*)malloc(sizeof(pt));
head = head->p;
}
head->p= NULL;
}
void out()//printing the hole node.
{
pt *start1 = start;
while(start1->p)
{
printf("%d %d\n",start1->x,start1->y);
start1=start1->p;
}
}
pt *srch(pt *s,int itm,int x) // Send value to the x for x = 0 -> for same node,1 for prev node,2 for next node
{
pt *noItem = NULL;
while(s->p)
{
if(s->p->x == itm)
{
if(x == 1)
{
return s; // sending the searched previous node address
}
else if(x == 2)
{
return s->p->p; // sending the searched next node address
}
else
{
return s->p; // send the searched node address
}
}
s = s->p;
}
return noItem;
}
/*Adding node after last element*/
void addLast()
{
pt *st = start;
pt *newNode = (pt*)malloc(sizeof(pt));
if(newNode == NULL)
{
printf("Stack Overflow\n");
}
while(st->p)
{
st=st->p;
}
st->p = newNode; // linking the new node
printf("Enter two valus for new node at last: ");
scanf("%d %d",&st->x,&st->y); // putting values to the last address
newNode->p = NULL;
}
/*Adding values to the first*/
void addFirst()
{
pt *newNode = (pt*)malloc(sizeof(pt));
if(newNode == NULL)
{
printf("Stack Overflow\n");
}
printf("Enter two values for new node at first: ");
scanf("%d %d",&newNode->x,&newNode->y);
pt *temp = start; // storing the head node
start = newNode; // changing the head node
newNode->p = temp; // pointing the next node
}
void addMiddle()
{
pt *s = start;
pt *newNode = (pt*) malloc(sizeof(pt));
if(newNode == NULL)
{
printf("Stack Overflow\n");
}
printf("Enter two value to insert a node to the middle: ");
scanf("%d %d",&newNode->x, &newNode->y);
int itm;
printf("Enter the value where you want to put the new node: ");
scanf("%d",&itm);
pt *where = srch(s,itm,1);
pt *temp = where->p; //storing the next address and we'll connect that with the newNode
where->p = newNode;
newNode->p = temp;
}
void delete_node()
{
int m,n;
printf("Enter that which node you want to delete: ");
scanf("%d %d",&m,&n);
pt *st = start;
pt *which = srch(st,m,0);
pt *pre = srch(st,m,1);
pre->p = which->p;
}
void stack_display()
{
pt *st = start;
printf("Printing stack in LIFO order: \n");
while(st)
{
while(st->p->p)
{
st = st -> p;
}
printf("%d %d\n",st->x,st->y);
pt *temp = st->p->p;
//free(temp);
st->p = NULL;
if(st == start)
{
break;
}
st = start;
}
//return ad;
}
void stack_push()
{
addLast();// Calling that addLast value cause it will print from the last
}
int main()
{
int x,y;
in(3);
pt *st = start;
// scanf("%d %d",&x,&y);
//pt *add = (pt*)srch(st,x,y);
// addLast();
// addFirst();
//addMiddle();
// out();
//delete_node();
// stack_push();
// stack_display();
return 0;
}