-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_linked.pl
executable file
·110 lines (96 loc) · 2.26 KB
/
p_linked.pl
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
#!/usr/bin/env perl
use strict;
#use warnings;
use 5.010;
use Sysadm::Install qw (:all);
use Data::Printer;
use constant NEXT => 0;
use constant VAL => 1;
my $list = undef;
while (1) {
my $op_mode = pick("Linked List Operations", ["Insert", "Display", "Size",
"Delete", "Addall", "Exit"], 1);
if ( $op_mode eq "Insert" ) {
say "Enter the number to insert : ";
chomp (my $num = <STDIN>);
insert($num);
}
elsif ( $op_mode eq "Display" ) {
display();
}
elsif ( $op_mode eq "Size" ) {
size();
}
elsif ( $op_mode eq "Delete" ) {
say "Enter the number to delete : ";
chomp (my $num = <STDIN>);
del_num($num);
}
elsif ( $op_mode eq "Addall" ) {
addall();
}
elsif ( $op_mode eq "Exit" ) {
exit;
}
}
sub insert {
my $num = shift;
$list = [ $list, $num ];
say "Success insert to $num";
}
sub display {
say "List is Empty" if ( $list eq "" );
my $t_list = $list;
while ( $t_list ne "" ) {
say "Element(s) in the list are : $t_list->[VAL]";
$t_list = $t_list->[NEXT];
}
}
sub size {
my $cnt = 0;
my $t_list = $list;
while ( $t_list ) {
$t_list = $t_list->[NEXT];
$cnt++;
}
say "Size of ther list is $cnt";
}
sub del_num {
my $num = shift;
my $t_list = $list;
my $p_list;
my $flag = 'off';
while ( $t_list ) {
if ( $t_list->[VAL] == $num ) {
$flag = 'on';
if ( $t_list->[NEXT] eq "" ) {
shift @{$p_list};
unshift @{$p_list}, undef;
say "(Last number)$num deleted successfully";
}
else {
$p_list->[NEXT] = $t_list->[NEXT];
say "$num deleted successfully";
}
}
$p_list = $t_list;
$t_list = $t_list->[NEXT];
}
say "Not found $num" if ( $flag eq 'off' );
}
sub addall {
my $t_list = $list;
my $sum;
while ( $t_list ) {
my $num = $t_list->[VAL];
$sum += $num;
$t_list = $t_list->[NEXT];
}
say "Add all number is $sum";
}
=pod
foreach my $num ( 1..5 ) {
my $node = { data => $num };
$$tail = $node;
$tail = \$node->{data};
}