simple_linked_list/lib.rs
1
2//! Structs
3
4
5
6/// Each `Node` contains a piece of data and an optional reference
7/// to the next node in the list. The reference to the next node
8/// is wrapped in an `Option` and a `Box`, which allows for nodes
9/// to be heap-allocated and linked together dynamically.
10#[derive(Debug,PartialEq,Clone)]
11pub struct Node<T>{
12 pub data : T,
13 pub next : Option<Box<Node<T>>>
14}
15
16/// The `LinkedList` structure is represent the singly linked list.
17/// The struct provide methods to create and manipulate the linked list.
18#[derive(Debug,PartialEq,Clone)]
19pub struct LinkedList<T>{
20 pub head : Option<Box<Node<T>>>
21}
22
23use std::fmt::Debug;
24
25impl<T:Debug> LinkedList<T> {
26
27 /// Created a new empty linked list.
28 ///
29 /// # Examples
30 ///
31 /// ```
32 /// use linked_list::LinkedList;
33 ///
34 /// let linked_list : LinkedList<i32> = LinkedList::new();
35 /// ```
36 pub fn new()-> Self{
37 LinkedList { head : None}
38 }
39
40 /// Check if the linked list is empty or not.
41 ///
42 /// # Examples
43 ///
44 /// ```
45 /// use linked_list::LinkedList;
46 ///
47 /// let linked_list : LinkedList<i32> = LinkedList::new();
48 /// assert!(linked_list.is_empty());
49 /// ```
50 pub fn is_empty(&self)-> bool{
51 self.head.is_none()
52 }
53
54
55 /// The 'push' method adds a new node to the top of the linked list.
56 /// # Example
57 ///
58 /// ```
59 ///
60 /// use linked_list::LinkedList;
61 ///
62 /// #[derive(Debug,PartialEq)]
63 /// pub struct User{
64 /// name : String,
65 /// roll_no : u32,
66 /// }
67 ///
68 /// //create some users
69 /// let user1 = User{
70 /// name : String::from("Payal Paunikar"),
71 /// roll_no : 1,
72 /// };
73 ///
74 /// let user2 = User{
75 /// name : String::from("Prachi Paunikar"),
76 /// roll_no : 2,
77 /// };
78 ///
79 /// //create a empty linked list
80 /// let mut user_linked_list : LinkedList<User>= LinkedList::new();
81 ///
82 /// //push user into the linked list
83 /// user_linked_list.push(user1);
84 /// user_linked_list.push(user2);
85 /// ```
86 pub fn push(&mut self, data : T){
87
88 let new_node = Box::new(
89 Node{
90 data,
91 next : self.head.take(),
92 }
93 );
94
95 self.head = Some(new_node);
96 }
97
98
99 /// The 'pop' method remove head node from the linked list
100 ///
101 /// # Example
102 ///
103 /// ```
104 ///
105 /// use linked_list::LinkedList;
106 ///
107 /// #[derive(Debug,PartialEq)]
108 /// pub struct User{
109 /// name : String,
110 /// roll_no : u32,
111 /// }
112 ///
113 /// //create some users
114 /// let user1 = User{
115 /// name : String::from("Payal Paunikar"),
116 /// roll_no : 1,
117 /// };
118 ///
119 /// let user2 = User{
120 /// name : String::from("Prachi Paunikar"),
121 /// roll_no : 2,
122 /// };
123 ///
124 /// //create a empty linked list
125 /// let mut user_linked_list =LinkedList::new();
126 ///
127 /// //push user into the linked list
128 /// user_linked_list.push(user1);
129 /// user_linked_list.push(user2);
130 ///
131 /// //pop the user from the linked list
132 /// user_linked_list.pop();
133 /// ```
134 pub fn pop(&mut self){
135
136 self.head.take().map(
137 |node|
138 self.head = node.next
139 );
140 }
141
142
143 /// The 'print_list' method print the linked list
144 ///
145 /// # Example 1
146 ///
147 /// ```
148 /// use linked_list::LinkedList;
149 ///
150 /// #[derive(Debug,PartialEq)]
151 /// pub struct User{
152 /// name : String,
153 /// roll_no : u32,
154 /// }
155 ///
156 /// //create some users
157 /// let user1 = User{
158 /// name : String::from("Payal Paunikar"),
159 /// roll_no : 1,
160 /// };
161 ///
162 /// let user2 = User{
163 /// name : String::from("Prachi Paunikar"),
164 /// roll_no : 2,
165 /// };
166 ///
167 /// //create a empty linked list
168 /// let mut user_linked_list = LinkedList::new();
169 ///
170 /// //push user into the linked list
171 /// user_linked_list.push(user1);
172 /// user_linked_list.push(user2);
173 ///
174 /// //print the linked list
175 /// user_linked_list.print_list();
176 ///
177 /// ```
178 ///
179 /// # Example 2
180 ///
181 /// ```
182 /// use linked_list::LinkedList;
183 ///
184 /// let mut linked_list : LinkedList<i32> = LinkedList::new();
185 /// linked_list.push(1);
186 /// linked_list.push(2);
187 ///
188 /// linked_list.print_list();
189 ///
190 /// ```
191 pub fn print_list(&self){
192
193 let mut current_node = &self.head;
194
195
196 while let Some(node) = current_node {
197 print!("{:?}->",node.data);
198 current_node = &node.next;
199 }
200
201 println!("None");
202
203 }
204
205
206 /// The 'len' method count the number of element store in the list
207 ///
208 /// # Example
209 /// ```
210 /// use linked_list::LinkedList;
211 /// let mut number_list : LinkedList<u32> = LinkedList::new();
212 /// number_list.push(1);
213 /// number_list.push(2);
214 ///
215 /// let number_list_count = number_list.len();
216 /// assert_eq!(2,number_list_count);
217 /// ```
218 pub fn len(&self)->usize{
219 let mut count = 0;
220 let mut current_node = &self.head;
221 while let Some(node) = current_node {
222 count += 1;
223 current_node = &node.next;
224 }
225 count
226
227 }
228
229
230
231
232}
233
234
235