Skip to content

Commit 2563b43

Browse files
committed
added insertion at start and end
1 parent cfba5bb commit 2563b43

File tree

2 files changed

+95
-36
lines changed

2 files changed

+95
-36
lines changed

doubly_ll/src/main.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Doubly Linked List Implementation in Rust
22
use text_io::read;
33

4-
#[derive(Debug, Clone)]
4+
#[allow(dead_code)]
5+
#[derive(Clone)]
56
struct DNode {
67
prev: Option<Box<DNode>>,
78
value: i32,
@@ -18,17 +19,45 @@ impl DNode {
1819
}
1920
}
2021

21-
fn main() {
22-
let mut head = DNode::new(0);
22+
fn insert_at_beginning(head: &mut DNode) {
23+
println!("\nEnter the element to be inserted: ");
24+
let inp: i32 = read!();
25+
let mut new_node = DNode::new(inp);
26+
new_node.next = Some(Box::new(head.clone()));
27+
*head = new_node;
28+
println!("The element {} has been inserted!", inp);
29+
}
30+
31+
fn count(head: &mut DNode) -> i32 {
32+
let mut current = head;
33+
let mut count = 0;
34+
while current.next.is_some() {
35+
count += 1;
36+
current = current.next.as_mut().unwrap();
37+
}
38+
count += 1;
39+
count
40+
}
41+
42+
fn push(head: &mut DNode) {
43+
println!("\nEnter the element to be pushed: ");
44+
let inp: i32 = read!();
45+
let mut current = head;
46+
while current.next.is_some() {
47+
current = current.next.as_mut().unwrap();
48+
}
49+
let new_node = DNode::new(inp);
50+
current.next = Some(Box::new(new_node));
51+
println!("Element {} successfully pushed!", inp);
52+
}
2353

54+
fn read_ll(head: &mut DNode) {
2455
let mut choice = 1;
2556
println!("Enter an integer value: ");
26-
let mut inp: i32 = read!();
27-
head.prev = None;
57+
let inp: i32 = read!();
2858
head.value = inp;
2959
head.next = None;
30-
let mut current = &mut head;
31-
let prev = &head;
60+
let mut current = head;
3261
while current.next.is_some() {
3362
current = current.next.as_mut().unwrap();
3463
}
@@ -47,3 +76,23 @@ fn main() {
4776
}
4877
}
4978
}
79+
80+
fn display_ll(head: &mut DNode) {
81+
let mut current = head;
82+
while current.next.is_some() {
83+
print!("{} -> ", current.value);
84+
current = current.next.as_mut().unwrap();
85+
}
86+
print!("{} -> X", current.value);
87+
}
88+
89+
fn main() {
90+
let mut head = DNode::new(0);
91+
read_ll(&mut head);
92+
display_ll(&mut head);
93+
insert_at_beginning(&mut head);
94+
display_ll(&mut head);
95+
push(&mut head);
96+
display_ll(&mut head);
97+
println!("\nNumber of elements in linked list: {} ", count(&mut head));
98+
}

linked_list_proper/src/main.rs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,69 @@
1+
// Singly Linked List Implementation in Rust
12
use text_io::read;
23

34
#[derive(Debug, Clone)]
4-
struct Node {
5-
next: Option<Box<Node>>,
6-
value: String,
5+
struct DNode {
6+
value: i32,
7+
next: Option<Box<DNode>>,
78
}
89

9-
impl Node {
10-
fn new(value: String) -> Self {
11-
Self { value, next: None }
10+
impl DNode {
11+
fn new(value: i32) -> Self {
12+
Self {
13+
value: value,
14+
next: None,
15+
}
1216
}
1317
}
1418

15-
fn main() {
16-
// Variables
17-
let mut choice: i32 = 1;
18-
let mut head = Node::new(String::from(""));
19-
let mut current = &mut head;
20-
// IO Initial Input
21-
println!("Enter a string: ");
22-
let mut str: String = read!();
23-
// Move to the end
19+
fn insert_at_beginning(head: &mut DNode) {
20+
println!("\nEnter the element to be inserted: ");
21+
let inp: i32 = read!();
22+
let mut new_node = DNode::new(inp);
23+
new_node.next = Some(Box::new(head.clone()));
24+
*head = new_node;
25+
println!("The element {} has been inserted!", inp);
26+
}
27+
28+
fn read_ll(head: &mut DNode) {
29+
let mut choice = 1;
30+
println!("Enter an integer value: ");
31+
let inp: i32 = read!();
32+
head.value = inp;
33+
head.next = None;
34+
let mut current = head;
2435
while current.next.is_some() {
2536
current = current.next.as_mut().unwrap();
2637
}
27-
// Set Head Value
28-
current.value = str.clone();
2938
while choice == 1 {
30-
str.clear();
31-
// IO Choice Input
3239
println!("Do you want to add another node? ");
3340
choice = read!();
34-
println!("Choice is {}", choice);
3541
if choice == 0 {
3642
current.next = None;
3743
break;
3844
} else {
39-
// IO Iterative Input
40-
println!("Enter a string: ");
41-
str = read!();
42-
let new_node = Node::new(str.clone());
45+
println!("Enter an integer: ");
46+
let int: i32 = read!();
47+
let new_node = DNode::new(int);
4348
current.next = Some(Box::new(new_node));
4449
current = current.next.as_mut().unwrap();
4550
}
4651
}
47-
48-
// Display nodes
49-
display_nodes(&mut head);
5052
}
5153

52-
fn display_nodes(head: &mut Node) {
54+
fn display_ll(head: &mut DNode) {
5355
let mut current = head;
5456
while current.next.is_some() {
5557
print!("{} -> ", current.value);
5658
current = current.next.as_mut().unwrap();
5759
}
58-
print!("X");
60+
print!("{} -> X", current.value);
61+
}
62+
63+
fn main() {
64+
let mut head = DNode::new(0);
65+
read_ll(&mut head);
66+
display_ll(&mut head);
67+
insert_at_beginning(&mut head);
68+
display_ll(&mut head);
5969
}

0 commit comments

Comments
 (0)