Skip to content

Commit bc987d0

Browse files
Insert Into Tail
1 parent f9fbacc commit bc987d0

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

input.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1
2+
100

insert_at_tail.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Node
5+
{
6+
public:
7+
int val;
8+
Node *next;
9+
Node(int val)
10+
{
11+
this->val = val;
12+
this->next = NULL;
13+
}
14+
};
15+
16+
void print_linked_list(Node *head)
17+
{
18+
Node *tmp = head;
19+
cout << endl
20+
<< "Your linked list: ";
21+
while (tmp != NULL)
22+
{
23+
cout << tmp->val << " ";
24+
tmp = tmp->next;
25+
}
26+
}
27+
28+
void insert_at_tail(Node *&head, int v)
29+
{
30+
Node *newNode = new Node(v);
31+
if (head == NULL)
32+
{
33+
head = newNode;
34+
return;
35+
}
36+
Node *tmp = head;
37+
while (tmp->next != NULL)
38+
{
39+
tmp = tmp->next;
40+
}
41+
42+
// tmp ekhon last node e
43+
tmp->next = newNode;
44+
// cout << tmp->val << endl;
45+
}
46+
47+
int main()
48+
{
49+
Node *head = NULL;
50+
while (true)
51+
{
52+
cout << endl;
53+
cout << "Option 1: Insert at Tail" << endl;
54+
cout << "Option 2: Print Linked List" << endl;
55+
cout << "Option 3: Terminate" << endl;
56+
int op;
57+
cin >> op;
58+
if (op == 1)
59+
{
60+
cout << "Please enter a value" << endl;
61+
int v;
62+
cin >> v;
63+
insert_at_tail(head, v);
64+
}
65+
else if (op == 2)
66+
{
67+
print_linked_list(head);
68+
}
69+
else if (op == 3)
70+
{
71+
break;
72+
}
73+
}
74+
75+
return 0;
76+
}

insert_at_tail.exe

52.9 KB
Binary file not shown.

output.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
0x61ff08
2-
0x61ff08
1+
Option 1: Insert at Tail

0 commit comments

Comments
 (0)