-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked list Deleting node at nth position.cpp
86 lines (72 loc) · 1.35 KB
/
Linked list Deleting node at nth position.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;
typedef struct node{
int data;
node* next;
};
node *head;
void insert(int data)
{
node *temp1=new node();
if(head==NULL)
{
temp1->data=data;
temp1->next=NULL;
head=temp1;
}
else
{
node* temp2=new node();
temp2=head;
while(temp2->next!=NULL)
{
temp2=temp2->next;
}
temp2->next=temp1;
temp1->data=data;
temp1->next=NULL;
}
}
void delet(int n)
{
node *temp1=new node();
temp1=head;
if(n==1)
{
head=temp1->next;
free(temp1);
return;
}
for(int i=1;i<n-1;i++)
temp1=temp1->next;
node *temp2=new node();
temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);
}
void display()
{
node *temp;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
head=NULL;
int a;
insert(4);
insert(6);
insert(98);
insert(45);
display();
cout<<endl;
cout<<"enter the position for deleting node ";
cin>>a;
delet(a);
display();
return 0;
}