-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_010.cpp
More file actions
67 lines (59 loc) · 1.32 KB
/
problem_010.cpp
File metadata and controls
67 lines (59 loc) · 1.32 KB
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
//Write a program to implement a linkedlist,
//with the option to insert a node into it at the user given position
#include<iostream>
using namespace std;
struct Node{
int data;
struct Node* next;
};
void CreateList(struct Node* *root,int value){
struct Node* ptr=*root;
struct Node* temp= new Node();
temp->data=value;
temp->next=NULL;
if(*root == NULL){
*root=temp;
}else{
while(ptr->next !=NULL){
ptr=ptr->next;
}
ptr->next=temp;
}
}
void insertNode(struct Node* *root,int value,int pos){
struct Node* ptr=*root;
struct Node* temp= new Node();
temp->data=value;
if(*root == NULL){
*root=temp;
temp->next=NULL;
}else{
int i=1;
while(i<pos-1){
ptr=ptr->next;
i++;
}
struct Node* afterNode=ptr->next;
ptr->next=temp;
temp->next=afterNode;
}
}
void display(struct Node* root){
struct Node* ptr=root;
while(ptr!=NULL){
cout<<ptr->data<<"->";
ptr=ptr->next;
}
cout<<"Null"<<endl;
}
int main(){
struct Node* root=NULL;
CreateList(&root,12);
CreateList(&root,15);
CreateList(&root,17);
CreateList(&root,22);
display(root);
insertNode(&root,45,3);
display(root);
return 0;
}