Doubly linked list in Rust using raw pointersDesigning function prototypes for a singly-linked list API in...
Why do members of Congress in committee hearings ask witnesses the same question multiple times?
Would these multi-classing house rules cause unintended problems?
Notes in a lick that don't fit in the scale associated with the chord
Do authors have to be politically correct in article-writing?
Difference between two quite-similar Terminal commands
Why Smart Plugs don't require setting port forwarding on router and how to accomplish this with NodeMCU (ESP8266)
If I delete my router's history can my ISP still provide it to my parents?
Is there any differences between "Gucken" and "Schauen"?
It took me a lot of time to make this, pls like. (YouTube Comments #1)
Slow moving projectiles from a hand-held weapon - how do they reach the target?
What flying insects could re-enter the Earth's atmosphere from space without burning up?
What kind of hardware implements Fourier transform?
Process to change collation on a database
Cryptic with missing capitals
Groups acting on trees
What is the most triangles you can make from a capital "H" and 3 straight lines?
Does fast page mode apply to ROM?
Where are a monster’s hit dice found in the stat block?
Checking for the existence of multiple directories
What is the wife of a henpecked husband called?
Explain the objections to these measures against human trafficking
What's the most convenient time of year to end the world?
Compress command output by piping to bzip2
Citing paywalled articles accessed via illegal web sharing
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
12 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 13 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
12 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
12 hours ago
$begingroup$
Hi, please use a title that reflect what your code does, not what you want from the review :)
$endgroup$
– IEatBagels
12 hours ago
$begingroup$
Hi, please use a title that reflect what your code does, not what you want from the review :)
$endgroup$
– IEatBagels
12 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
12 hours ago