-
Notifications
You must be signed in to change notification settings - Fork 6
/
delete_node.cpp
60 lines (60 loc) · 1.4 KB
/
delete_node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node* insert_node(node *start,int x)
{
node *temp=new node;
temp->data=x;
temp->next=NULL;
if(start==NULL)
return temp;
node *ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
return start;
}
void display(node *start)
{
node *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
void getNth(node *start,int n)
{
int c=0;
node *temp=start;
node *temp1=start;
while(temp!=NULL)
{
temp=temp->next;
c++;
}
if(!c<n)
{
for(int i=1;i<c-n+1;i++)
temp1=temp1->next;
cout<<"\nData searched:"<<temp1->data;
}
}
int main()
{
node *sample=NULL;
sample=insert_node(sample,10);
sample=insert_node(sample,20);
sample=insert_node(sample,30);
sample=insert_node(sample,40);
sample=insert_node(sample,50);
display(sample);
getNth(sample,3);
return 0;
}