-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.cpp
79 lines (73 loc) · 1.21 KB
/
linked_list.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
//Implement Linked List
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
struct node
{
int data;
struct node* nptr;
};
struct node* hptr=NULL;
void insertNode(int pos, int x)
{
struct node *temp=new node;
if(temp==NULL)
cout<<"Insert not possible\n";
temp->data=x;
if(pos==1)
{
temp->nptr=hptr;
hptr=temp;
}
else
{
int i=1;
struct node* thptr=hptr;
while(i<pos-1)
{
thptr=thptr->nptr;
i++;
}
temp->nptr=thptr->nptr;
thptr->nptr=temp;
}
}
void deleteNode(int pos)
{
struct node *temp=hptr;
int i=1;
if(temp==NULL)
cout<<"Delete not possible\n";
if(pos==1)
hptr=hptr->nptr;
else
{
while(i<pos-1)
{
temp=temp->nptr;
i++;
}
temp->nptr=(temp->nptr)->nptr;
}
}
void print()
{
struct node* temp=hptr;
while(temp!=NULL)
{
cout<<temp->data<<"\n";
temp=temp->nptr;
}
}
int main()
{
insertNode(1,10);
insertNode(2,20);
insertNode(3,30);
insertNode(4,40);
insertNode(5,50);
deleteNode(3);
print();
return 0;
}