Doubly linked list in Rust using raw pointersDesigning function prototypes for a singly-linked list API in...
Can you earn endless XP using a Flameskull and its self-revival feature?
Why don't American passenger airlines operate dedicated cargo flights any more?
What is better: yes / no radio, or simple checkbox?
Using only 1s, make 29 with the minimum number of digits
How to prevent cleaner from hanging my lock screen in Ubuntu 16.04
Citing paywalled articles accessed via illegal web sharing
What to do when being responsible for data protection in your lab, yet advice is ignored?
A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?
What's the most convenient time of year to end the world?
If I delete my router's history can my ISP still provide it to my parents?
Why do neural networks need so many training examples to perform?
Why did this image turn out darker?
Compress command output by piping to bzip2
Why does String.replaceAll() work differently in Java 8 from Java 9?
Groups acting on trees
What is the purpose of easy combat scenarios that don't need resource expenditure?
How would one buy a used TIE Fighter or X-Wing?
What flying insects could re-enter the Earth's atmosphere from space without burning up?
Is there some relative to Dutch word "kijken" in German?
Does Windows 10's telemetry include sending *.doc files if Word crashed?
Book where aliens are selecting humans for food consumption
Why do members of Congress in committee hearings ask witnesses the same question multiple times?
Why does a metal block make a shrill sound but not a wooden block upon hammering?
Where are a monster’s hit dice found in the stat block?
Doubly linked list in Rust using raw pointers
Designing function prototypes for a singly-linked list API in CDoubly linked list reimplemented with smart pointersInsert a Node at the Tail of a Linked ListA doubly linked list implementation using C++ smart pointersDeque using doubly linked listLinked list with removal function in RustLinked List using templates and smart pointersDoubly linked list in RustLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)Implementing a doubly linked list with smart pointers
$begingroup$
I am practicing Rust by writing doubly linked list using raw pointers, Box<node>
to allocate data on heap, Box::from_raw
to free data from heap. I feel it very silly to use a lot of unsafe blocks.
struct node<T> {
value: T,
prev: *mut node<T>,
next: *mut node<T>
}
impl<T> Drop for node<T> {
fn drop(self: &mut Self) {
println!("Dropping anode");
}
}
impl<T> node<T> {
fn new(value: T, prev: *mut node<T>, next: *mut node<T>) -> Self {
return Self{value: value, prev: prev, next: next};
}
}
struct list<T> {
root: *mut node<T>
}
impl<T> Drop for list<T> {
fn drop(self: &mut Self) {
let mut this: *mut node<T> = self.root;
let mut drop: *mut node<T> = std::ptr::null_mut();
while this != std::ptr::null_mut() {
drop = this;
unsafe {this = (*this).next;}
unsafe {let anode = Box::from_raw(drop);}
}
}
}
impl std::fmt::Display for list<f64> {
fn fmt(self: &Self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "[");
let mut this: *mut node<f64> = self.root;
while this != std::ptr::null_mut() {
unsafe {write!(f, "{} ", (*this).value);}
unsafe {this = (*this).next;}
}
write!(f, "]");
return Ok(());
}
}
impl<T> list<T> {
fn new() -> Self {
let p : *mut node<T> = std::ptr::null_mut();
return Self {root: p};
}
fn len(self: &Self) -> i32 {
let mut len: i32 = 0;
let mut this: *mut node<T> = self.root;
while this != std::ptr::null_mut()
{
len += 1;
unsafe {
this = (*this).next;
}
}
return len;
}
fn getnode(self: &Self, mut i: i32) -> *mut node<T> {
let mut this: *mut node<T> = self.root;
let mut next: *mut node<T> = std::ptr::null_mut();
while i > 0 {
unsafe {
next = (*this).next;
}
if next == std::ptr::null_mut() {
return std::ptr::null_mut();
} else {
i -= 1;
this = next;
}
}
return this;
}
fn insert(mut self: &mut Self, mut i: i32, value: T) {
let mut next: *mut node<T> = self.getnode(i);
let mut prev: *mut node<T> = std::ptr::null_mut();
if i != 0 {
if next == std::ptr::null_mut() {
let lastindex: i32 = self.len() - 1;
prev = self.getnode(lastindex);
} else {
unsafe {prev = (*next).prev;}
}
}
let mut anode: Box<node<T>> = Box::new(node::new(value, prev, next));
let mut this = Box::into_raw(anode);
if i == 0 {
self.root = this;
}
if next != std::ptr::null_mut() {
unsafe {(*next).prev = this;}
}
if prev != std::ptr::null_mut() {
unsafe {(*prev).next = this;}
}
}
fn erase(mut self: &mut Self, mut i: i32) {
let mut this: *mut node<T> = self.getnode(i);
if this == std::ptr::null_mut() {return;}
let mut prev: *mut node<T> = std::ptr::null_mut();
let mut next: *mut node<T> = std::ptr::null_mut();
unsafe {
prev = (*this).prev;
next = (*this).next;
}
if prev != std::ptr::null_mut() {
unsafe {(*prev).next = next;}
}
if next != std::ptr::null_mut() {
unsafe {(*next).prev = prev;}
}
if i == 0 {
self.root = next;
}
unsafe {let anode: Box<node<T>> = Box::from_raw(this);}
}
fn getvalue(self: Self, mut i: i32) -> &'static T {
let this: *mut node<T> = self.getnode(i);
unsafe {
return &(*this).value;
}
}
}
pub fn main() {
let mut l: list<f64> = list::new();
l.insert(0, 3.0);
println!("{}", l);
l.insert(0, 4.0);
println!("{}", l);
l.insert(1, 1.0);
println!("{}", l);
l.insert(2, 6.0);
println!("{}", l);
l.insert(2, 5.4);
println!("{}", l);
l.insert(5, 3.3);
println!("{}", l);
l.erase(3);
println!("{}", l);
l.erase(4);
println!("{}", l);
l.erase(0);
println!("{}", l);
}
beginner linked-list rust
New contributor
$endgroup$
add a comment |
$begingroup$
I am practicing Rust by writing doubly linked list using raw pointers, Box<node>
to allocate data on heap, Box::from_raw
to free data from heap. I feel it very silly to use a lot of unsafe blocks.
struct node<T> {
value: T,
prev: *mut node<T>,
next: *mut node<T>
}
impl<T> Drop for node<T> {
fn drop(self: &mut Self) {
println!("Dropping anode");
}
}
impl<T> node<T> {
fn new(value: T, prev: *mut node<T>, next: *mut node<T>) -> Self {
return Self{value: value, prev: prev, next: next};
}
}
struct list<T> {
root: *mut node<T>
}
impl<T> Drop for list<T> {
fn drop(self: &mut Self) {
let mut this: *mut node<T> = self.root;
let mut drop: *mut node<T> = std::ptr::null_mut();
while this != std::ptr::null_mut() {
drop = this;
unsafe {this = (*this).next;}
unsafe {let anode = Box::from_raw(drop);}
}
}
}
impl std::fmt::Display for list<f64> {
fn fmt(self: &Self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "[");
let mut this: *mut node<f64> = self.root;
while this != std::ptr::null_mut() {
unsafe {write!(f, "{} ", (*this).value);}
unsafe {this = (*this).next;}
}
write!(f, "]");
return Ok(());
}
}
impl<T> list<T> {
fn new() -> Self {
let p : *mut node<T> = std::ptr::null_mut();
return Self {root: p};
}
fn len(self: &Self) -> i32 {
let mut len: i32 = 0;
let mut this: *mut node<T> = self.root;
while this != std::ptr::null_mut()
{
len += 1;
unsafe {
this = (*this).next;
}
}
return len;
}
fn getnode(self: &Self, mut i: i32) -> *mut node<T> {
let mut this: *mut node<T> = self.root;
let mut next: *mut node<T> = std::ptr::null_mut();
while i > 0 {
unsafe {
next = (*this).next;
}
if next == std::ptr::null_mut() {
return std::ptr::null_mut();
} else {
i -= 1;
this = next;
}
}
return this;
}
fn insert(mut self: &mut Self, mut i: i32, value: T) {
let mut next: *mut node<T> = self.getnode(i);
let mut prev: *mut node<T> = std::ptr::null_mut();
if i != 0 {
if next == std::ptr::null_mut() {
let lastindex: i32 = self.len() - 1;
prev = self.getnode(lastindex);
} else {
unsafe {prev = (*next).prev;}
}
}
let mut anode: Box<node<T>> = Box::new(node::new(value, prev, next));
let mut this = Box::into_raw(anode);
if i == 0 {
self.root = this;
}
if next != std::ptr::null_mut() {
unsafe {(*next).prev = this;}
}
if prev != std::ptr::null_mut() {
unsafe {(*prev).next = this;}
}
}
fn erase(mut self: &mut Self, mut i: i32) {
let mut this: *mut node<T> = self.getnode(i);
if this == std::ptr::null_mut() {return;}
let mut prev: *mut node<T> = std::ptr::null_mut();
let mut next: *mut node<T> = std::ptr::null_mut();
unsafe {
prev = (*this).prev;
next = (*this).next;
}
if prev != std::ptr::null_mut() {
unsafe {(*prev).next = next;}
}
if next != std::ptr::null_mut() {
unsafe {(*next).prev = prev;}
}
if i == 0 {
self.root = next;
}
unsafe {let anode: Box<node<T>> = Box::from_raw(this);}
}
fn getvalue(self: Self, mut i: i32) -> &'static T {
let this: *mut node<T> = self.getnode(i);
unsafe {
return &(*this).value;
}
}
}
pub fn main() {
let mut l: list<f64> = list::new();
l.insert(0, 3.0);
println!("{}", l);
l.insert(0, 4.0);
println!("{}", l);
l.insert(1, 1.0);
println!("{}", l);
l.insert(2, 6.0);
println!("{}", l);
l.insert(2, 5.4);
println!("{}", l);
l.insert(5, 3.3);
println!("{}", l);
l.erase(3);
println!("{}", l);
l.erase(4);
println!("{}", l);
l.erase(0);
println!("{}", l);
}
beginner linked-list rust
New contributor
$endgroup$
$begingroup$
Hi, please use a title that reflect what your code does, not what you want from the review :)
$endgroup$
– IEatBagels
13 hours ago
add a comment |
$begingroup$
I am practicing Rust by writing doubly linked list using raw pointers, Box<node>
to allocate data on heap, Box::from_raw
to free data from heap. I feel it very silly to use a lot of unsafe blocks.
struct node<T> {
value: T,
prev: *mut node<T>,
next: *mut node<T>
}
impl<T> Drop for node<T> {
fn drop(self: &mut Self) {
println!("Dropping anode");
}
}
impl<T> node<T> {
fn new(value: T, prev: *mut node<T>, next: *mut node<T>) -> Self {
return Self{value: value, prev: prev, next: next};
}
}
struct list<T> {
root: *mut node<T>
}
impl<T> Drop for list<T> {
fn drop(self: &mut Self) {
let mut this: *mut node<T> = self.root;
let mut drop: *mut node<T> = std::ptr::null_mut();
while this != std::ptr::null_mut() {
drop = this;
unsafe {this = (*this).next;}
unsafe {let anode = Box::from_raw(drop);}
}
}
}
impl std::fmt::Display for list<f64> {
fn fmt(self: &Self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "[");
let mut this: *mut node<f64> = self.root;
while this != std::ptr::null_mut() {
unsafe {write!(f, "{} ", (*this).value);}
unsafe {this = (*this).next;}
}
write!(f, "]");
return Ok(());
}
}
impl<T> list<T> {
fn new() -> Self {
let p : *mut node<T> = std::ptr::null_mut();
return Self {root: p};
}
fn len(self: &Self) -> i32 {
let mut len: i32 = 0;
let mut this: *mut node<T> = self.root;
while this != std::ptr::null_mut()
{
len += 1;
unsafe {
this = (*this).next;
}
}
return len;
}
fn getnode(self: &Self, mut i: i32) -> *mut node<T> {
let mut this: *mut node<T> = self.root;
let mut next: *mut node<T> = std::ptr::null_mut();
while i > 0 {
unsafe {
next = (*this).next;
}
if next == std::ptr::null_mut() {
return std::ptr::null_mut();
} else {
i -= 1;
this = next;
}
}
return this;
}
fn insert(mut self: &mut Self, mut i: i32, value: T) {
let mut next: *mut node<T> = self.getnode(i);
let mut prev: *mut node<T> = std::ptr::null_mut();
if i != 0 {
if next == std::ptr::null_mut() {
let lastindex: i32 = self.len() - 1;
prev = self.getnode(lastindex);
} else {
unsafe {prev = (*next).prev;}
}
}
let mut anode: Box<node<T>> = Box::new(node::new(value, prev, next));
let mut this = Box::into_raw(anode);
if i == 0 {
self.root = this;
}
if next != std::ptr::null_mut() {
unsafe {(*next).prev = this;}
}
if prev != std::ptr::null_mut() {
unsafe {(*prev).next = this;}
}
}
fn erase(mut self: &mut Self, mut i: i32) {
let mut this: *mut node<T> = self.getnode(i);
if this == std::ptr::null_mut() {return;}
let mut prev: *mut node<T> = std::ptr::null_mut();
let mut next: *mut node<T> = std::ptr::null_mut();
unsafe {
prev = (*this).prev;
next = (*this).next;
}
if prev != std::ptr::null_mut() {
unsafe {(*prev).next = next;}
}
if next != std::ptr::null_mut() {
unsafe {(*next).prev = prev;}
}
if i == 0 {
self.root = next;
}
unsafe {let anode: Box<node<T>> = Box::from_raw(this);}
}
fn getvalue(self: Self, mut i: i32) -> &'static T {
let this: *mut node<T> = self.getnode(i);
unsafe {
return &(*this).value;
}
}
}
pub fn main() {
let mut l: list<f64> = list::new();
l.insert(0, 3.0);
println!("{}", l);
l.insert(0, 4.0);
println!("{}", l);
l.insert(1, 1.0);
println!("{}", l);
l.insert(2, 6.0);
println!("{}", l);
l.insert(2, 5.4);
println!("{}", l);
l.insert(5, 3.3);
println!("{}", l);
l.erase(3);
println!("{}", l);
l.erase(4);
println!("{}", l);
l.erase(0);
println!("{}", l);
}
beginner linked-list rust
New contributor
$endgroup$
I am practicing Rust by writing doubly linked list using raw pointers, Box<node>
to allocate data on heap, Box::from_raw
to free data from heap. I feel it very silly to use a lot of unsafe blocks.
struct node<T> {
value: T,
prev: *mut node<T>,
next: *mut node<T>
}
impl<T> Drop for node<T> {
fn drop(self: &mut Self) {
println!("Dropping anode");
}
}
impl<T> node<T> {
fn new(value: T, prev: *mut node<T>, next: *mut node<T>) -> Self {
return Self{value: value, prev: prev, next: next};
}
}
struct list<T> {
root: *mut node<T>
}
impl<T> Drop for list<T> {
fn drop(self: &mut Self) {
let mut this: *mut node<T> = self.root;
let mut drop: *mut node<T> = std::ptr::null_mut();
while this != std::ptr::null_mut() {
drop = this;
unsafe {this = (*this).next;}
unsafe {let anode = Box::from_raw(drop);}
}
}
}
impl std::fmt::Display for list<f64> {
fn fmt(self: &Self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "[");
let mut this: *mut node<f64> = self.root;
while this != std::ptr::null_mut() {
unsafe {write!(f, "{} ", (*this).value);}
unsafe {this = (*this).next;}
}
write!(f, "]");
return Ok(());
}
}
impl<T> list<T> {
fn new() -> Self {
let p : *mut node<T> = std::ptr::null_mut();
return Self {root: p};
}
fn len(self: &Self) -> i32 {
let mut len: i32 = 0;
let mut this: *mut node<T> = self.root;
while this != std::ptr::null_mut()
{
len += 1;
unsafe {
this = (*this).next;
}
}
return len;
}
fn getnode(self: &Self, mut i: i32) -> *mut node<T> {
let mut this: *mut node<T> = self.root;
let mut next: *mut node<T> = std::ptr::null_mut();
while i > 0 {
unsafe {
next = (*this).next;
}
if next == std::ptr::null_mut() {
return std::ptr::null_mut();
} else {
i -= 1;
this = next;
}
}
return this;
}
fn insert(mut self: &mut Self, mut i: i32, value: T) {
let mut next: *mut node<T> = self.getnode(i);
let mut prev: *mut node<T> = std::ptr::null_mut();
if i != 0 {
if next == std::ptr::null_mut() {
let lastindex: i32 = self.len() - 1;
prev = self.getnode(lastindex);
} else {
unsafe {prev = (*next).prev;}
}
}
let mut anode: Box<node<T>> = Box::new(node::new(value, prev, next));
let mut this = Box::into_raw(anode);
if i == 0 {
self.root = this;
}
if next != std::ptr::null_mut() {
unsafe {(*next).prev = this;}
}
if prev != std::ptr::null_mut() {
unsafe {(*prev).next = this;}
}
}
fn erase(mut self: &mut Self, mut i: i32) {
let mut this: *mut node<T> = self.getnode(i);
if this == std::ptr::null_mut() {return;}
let mut prev: *mut node<T> = std::ptr::null_mut();
let mut next: *mut node<T> = std::ptr::null_mut();
unsafe {
prev = (*this).prev;
next = (*this).next;
}
if prev != std::ptr::null_mut() {
unsafe {(*prev).next = next;}
}
if next != std::ptr::null_mut() {
unsafe {(*next).prev = prev;}
}
if i == 0 {
self.root = next;
}
unsafe {let anode: Box<node<T>> = Box::from_raw(this);}
}
fn getvalue(self: Self, mut i: i32) -> &'static T {
let this: *mut node<T> = self.getnode(i);
unsafe {
return &(*this).value;
}
}
}
pub fn main() {
let mut l: list<f64> = list::new();
l.insert(0, 3.0);
println!("{}", l);
l.insert(0, 4.0);
println!("{}", l);
l.insert(1, 1.0);
println!("{}", l);
l.insert(2, 6.0);
println!("{}", l);
l.insert(2, 5.4);
println!("{}", l);
l.insert(5, 3.3);
println!("{}", l);
l.erase(3);
println!("{}", l);
l.erase(4);
println!("{}", l);
l.erase(0);
println!("{}", l);
}
beginner linked-list rust
beginner linked-list rust
New contributor
New contributor
edited 12 hours ago
200_success
130k16153417
130k16153417
New contributor
asked 14 hours ago
Ngọc Khánh NguyễnNgọc Khánh Nguyễn
61
61
New contributor
New contributor
$begingroup$
Hi, please use a title that reflect what your code does, not what you want from the review :)
$endgroup$
– IEatBagels
13 hours ago
add a comment |
$begingroup$
Hi, please use a title that reflect what your code does, not what you want from the review :)
$endgroup$
– IEatBagels
13 hours ago
$begingroup$
Hi, please use a title that reflect what your code does, not what you want from the review :)
$endgroup$
– IEatBagels
13 hours ago
$begingroup$
Hi, please use a title that reflect what your code does, not what you want from the review :)
$endgroup$
– IEatBagels
13 hours ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Ngọc Khánh Nguyễn is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214555%2fdoubly-linked-list-in-rust-using-raw-pointers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ngọc Khánh Nguyễn is a new contributor. Be nice, and check out our Code of Conduct.
Ngọc Khánh Nguyễn is a new contributor. Be nice, and check out our Code of Conduct.
Ngọc Khánh Nguyễn is a new contributor. Be nice, and check out our Code of Conduct.
Ngọc Khánh Nguyễn is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214555%2fdoubly-linked-list-in-rust-using-raw-pointers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Hi, please use a title that reflect what your code does, not what you want from the review :)
$endgroup$
– IEatBagels
13 hours ago