Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (83 loc) · 2.73 KB
/
Copy pathmain.cpp
File metadata and controls
115 lines (83 loc) · 2.73 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
using namespace std;
struct node
{
int data;
struct node* next; //check struct node* head=NULL
};
void printList(struct node* list)
{
int i=0;
while(list!=NULL)
{
i++;
cout<<"Node:"<<i<<" | "<<"List->Data:"<<list->data<<" List(Node Address):"<<list<<" List->Next:"<<list->next<<"\n";
list=list->next;
}
}
struct node* insertAtFrirst(struct node* head,int data)
{ // function return pointer of the new head node
struct node* ptr=(struct node*)malloc(sizeof(struct node));
ptr->data=data;
ptr->next=head;// fill next of ptr with pointer of previous head
return ptr;//ptr is the new head
}
struct node* insertAtEnd(struct node* head,int data)
{
struct node* ptr=(struct node *)malloc(sizeof(struct node));
struct node* p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=ptr;
ptr->next=NULL;
return head;
}
/* insertAtIndex function dosen't work with index 0 use insertAtFirst forthat purpose */
struct node* insertAtIndex(struct node* head,int data,int index)
{
struct node* ptr=(struct node *)malloc(sizeof(struct node));// new node creation
ptr->data=data;
struct node* p=head; //pointer variable to store head pointer
int i=0;
while(i!=index-1)
{
p=p->next;// travesing to required location //updating
i++;
}
ptr->next=p->next;// assigning original next to Ptr(new node)->next
p->next=ptr; // storing address of newely created node at previous node
return head;
}
int main() {
//struct node* head=NULL; //check struct node* next; //node created
//struct node* second=NULL; //check struct node* next; //node created
//struct node* third=NULL; //check struct node* next; //node created
//struct node* fourth=NULL;
struct node *head=(struct node*)malloc(sizeof(struct node)); //pointer of node creation
struct node *second=(struct node*)malloc(sizeof(struct node)); //pointer of node creation
struct node *third=(struct node*)malloc(sizeof(struct node)); //pointer of node creation
struct node *fourth=(struct node*)malloc(sizeof(struct node));
head->data=1;
head->next=second;
second->data=2;
second->next=third;
third->data=3;
third->next=fourth;
fourth->data=4;
fourth->next=NULL;
printList(head); //do not use &head
head=insertAtFrirst(head,69); //strore new head poineter to head
cout<<"\n\nUpdated list:\n";
printList(head);
head=insertAtFrirst(head,72); //strore new head poineter to head
cout<<"\n\nUpdated list:\n";
printList(head);
head=insertAtIndex(head,101,3); //strore new head poineter to head
cout<<"\n\nUpdated list:\n";
printList(head);
head=insertAtEnd(head,999); //strore new head poineter to head
cout<<"\n\nUpdated list:\n";
printList(head);
}