Implementation of a Singularly LinkedListInserting a node to a given position in a linked listImplementation of stackMerging sorted linked lists - C++Generic singly linked list using the C++11 standardLinked List design and implementation - C++Insert a Node at the Tail of a Linked ListHackerrank delete a nodeC++11 Singly Linked List with raw pointersLinked List using templates and smart pointersLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)
Does a bard know when a character uses their Bardic Inspiration?
Why have both: BJT and FET transistors on IC output?
Is there a way to say "double + any number" in German?
Using Forstner bits instead of hole saws
What printing process is this?
Why does BezierFunction not follow BezierCurve at npts>4?
Why do player start with fighting for the corners in go?
Approximating an expression for a potential
What is the reason behind water not falling from a bucket at the top of loop?
How was the cosmonaut of the Soviet moon mission supposed to get back in the return vehicle?
What does "autolyco-sentimental" mean?
(7 of 11: Fillomino) What is Pyramid Cult's Favorite Shape?
Is it moral to remove/hide certain parts of a photo, as a photographer?
Any information about the photo with Army Uniforms
What is Albrecht Dürer's Perspective Machine drawing style?
What license to choose for my PhD thesis?
Polygons crash kernel?
Pronouns when writing from the point of view of a robot
Is there a word that describes people who are extraverted and/or energetic, but uneducated, unintelligent and/or uncreative?
Can there be multiple energy eigenstates corresponding to the same eigenvalue of a Hamiltonian (Pauli-X)?
What is a summary of basic Jewish metaphysics or theology?
C# TCP server/client class
Subtle ways to render a planet uninhabitable
How to call made-up data?
Implementation of a Singularly LinkedList
Inserting a node to a given position in a linked listImplementation of stackMerging sorted linked lists - C++Generic singly linked list using the C++11 standardLinked List design and implementation - C++Insert a Node at the Tail of a Linked ListHackerrank delete a nodeC++11 Singly Linked List with raw pointersLinked List using templates and smart pointersLeetcode #146. LRUCache solution in Java (Doubly Linked List + HashMap)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I'm new to C++ and trying to learn by implementing data structures in it.
Is the code below the most efficient way of implementing the data structure in C++? Does it conform to C++ standards, and how readable is it?
#include <iostream>
class Node
public:
int data;
Node *next;
Node(int data)
this->data = data;
this->next = nullptr;
;
class LinkedList
public:
Node *head;
Node *tail;
int length;
LinkedList()
this->head = nullptr;
this->tail = nullptr;
this->length = 0;
void add(int data);
void add(int data, int position);
void delete_node(int position);
int get_length();
void print();
int get_first();
;
void LinkedList::add(int data)
Node *node = new Node(data);
if (this->head == nullptr)
this->head = node;
else
this->tail->next = node;
this->tail = node;
this->length = this->length + 1;
void LinkedList::add(int data, int position)
Node *temp_head = this->head;
Node *node = new Node(data);
int i = 0;
while (i < this->length)
if (i == position - 1)
node->next = this->head->next;
this->head->next = node;
this->head = this->head->next;
i++;
this->head = temp_head;
this->length = this->length + 1;
void LinkedList::print()
Node *temp_head = this->head;
while (this->head != nullptr)
std::cout << this->head->data << 'n';
this->head = this->head->next;
this->head = temp_head;
int LinkedList::get_length()
return this->length;
int LinkedList::get_first()
return this->head->data;
int main()
LinkedList *llist = new LinkedList();
llist->add(0);
llist->add(1);
llist->add(3);
llist->add(4);
llist->add(2, 2);
llist->print();
std::cout << "Length: " << llist->get_length() << 'n';
std::cout << "First Element : " << llist->get_first() << 'n';
c++ beginner linked-list
$endgroup$
add a comment |
$begingroup$
I'm new to C++ and trying to learn by implementing data structures in it.
Is the code below the most efficient way of implementing the data structure in C++? Does it conform to C++ standards, and how readable is it?
#include <iostream>
class Node
public:
int data;
Node *next;
Node(int data)
this->data = data;
this->next = nullptr;
;
class LinkedList
public:
Node *head;
Node *tail;
int length;
LinkedList()
this->head = nullptr;
this->tail = nullptr;
this->length = 0;
void add(int data);
void add(int data, int position);
void delete_node(int position);
int get_length();
void print();
int get_first();
;
void LinkedList::add(int data)
Node *node = new Node(data);
if (this->head == nullptr)
this->head = node;
else
this->tail->next = node;
this->tail = node;
this->length = this->length + 1;
void LinkedList::add(int data, int position)
Node *temp_head = this->head;
Node *node = new Node(data);
int i = 0;
while (i < this->length)
if (i == position - 1)
node->next = this->head->next;
this->head->next = node;
this->head = this->head->next;
i++;
this->head = temp_head;
this->length = this->length + 1;
void LinkedList::print()
Node *temp_head = this->head;
while (this->head != nullptr)
std::cout << this->head->data << 'n';
this->head = this->head->next;
this->head = temp_head;
int LinkedList::get_length()
return this->length;
int LinkedList::get_first()
return this->head->data;
int main()
LinkedList *llist = new LinkedList();
llist->add(0);
llist->add(1);
llist->add(3);
llist->add(4);
llist->add(2, 2);
llist->print();
std::cout << "Length: " << llist->get_length() << 'n';
std::cout << "First Element : " << llist->get_first() << 'n';
c++ beginner linked-list
$endgroup$
1
$begingroup$
If you are new to a language, you should add the beginner tag to your question. Reviewers take this tag into account.
$endgroup$
– dfhwze
8 hours ago
add a comment |
$begingroup$
I'm new to C++ and trying to learn by implementing data structures in it.
Is the code below the most efficient way of implementing the data structure in C++? Does it conform to C++ standards, and how readable is it?
#include <iostream>
class Node
public:
int data;
Node *next;
Node(int data)
this->data = data;
this->next = nullptr;
;
class LinkedList
public:
Node *head;
Node *tail;
int length;
LinkedList()
this->head = nullptr;
this->tail = nullptr;
this->length = 0;
void add(int data);
void add(int data, int position);
void delete_node(int position);
int get_length();
void print();
int get_first();
;
void LinkedList::add(int data)
Node *node = new Node(data);
if (this->head == nullptr)
this->head = node;
else
this->tail->next = node;
this->tail = node;
this->length = this->length + 1;
void LinkedList::add(int data, int position)
Node *temp_head = this->head;
Node *node = new Node(data);
int i = 0;
while (i < this->length)
if (i == position - 1)
node->next = this->head->next;
this->head->next = node;
this->head = this->head->next;
i++;
this->head = temp_head;
this->length = this->length + 1;
void LinkedList::print()
Node *temp_head = this->head;
while (this->head != nullptr)
std::cout << this->head->data << 'n';
this->head = this->head->next;
this->head = temp_head;
int LinkedList::get_length()
return this->length;
int LinkedList::get_first()
return this->head->data;
int main()
LinkedList *llist = new LinkedList();
llist->add(0);
llist->add(1);
llist->add(3);
llist->add(4);
llist->add(2, 2);
llist->print();
std::cout << "Length: " << llist->get_length() << 'n';
std::cout << "First Element : " << llist->get_first() << 'n';
c++ beginner linked-list
$endgroup$
I'm new to C++ and trying to learn by implementing data structures in it.
Is the code below the most efficient way of implementing the data structure in C++? Does it conform to C++ standards, and how readable is it?
#include <iostream>
class Node
public:
int data;
Node *next;
Node(int data)
this->data = data;
this->next = nullptr;
;
class LinkedList
public:
Node *head;
Node *tail;
int length;
LinkedList()
this->head = nullptr;
this->tail = nullptr;
this->length = 0;
void add(int data);
void add(int data, int position);
void delete_node(int position);
int get_length();
void print();
int get_first();
;
void LinkedList::add(int data)
Node *node = new Node(data);
if (this->head == nullptr)
this->head = node;
else
this->tail->next = node;
this->tail = node;
this->length = this->length + 1;
void LinkedList::add(int data, int position)
Node *temp_head = this->head;
Node *node = new Node(data);
int i = 0;
while (i < this->length)
if (i == position - 1)
node->next = this->head->next;
this->head->next = node;
this->head = this->head->next;
i++;
this->head = temp_head;
this->length = this->length + 1;
void LinkedList::print()
Node *temp_head = this->head;
while (this->head != nullptr)
std::cout << this->head->data << 'n';
this->head = this->head->next;
this->head = temp_head;
int LinkedList::get_length()
return this->length;
int LinkedList::get_first()
return this->head->data;
int main()
LinkedList *llist = new LinkedList();
llist->add(0);
llist->add(1);
llist->add(3);
llist->add(4);
llist->add(2, 2);
llist->print();
std::cout << "Length: " << llist->get_length() << 'n';
std::cout << "First Element : " << llist->get_first() << 'n';
c++ beginner linked-list
c++ beginner linked-list
edited 8 hours ago
Dan Savage
asked 8 hours ago
Dan SavageDan Savage
623 bronze badges
623 bronze badges
1
$begingroup$
If you are new to a language, you should add the beginner tag to your question. Reviewers take this tag into account.
$endgroup$
– dfhwze
8 hours ago
add a comment |
1
$begingroup$
If you are new to a language, you should add the beginner tag to your question. Reviewers take this tag into account.
$endgroup$
– dfhwze
8 hours ago
1
1
$begingroup$
If you are new to a language, you should add the beginner tag to your question. Reviewers take this tag into account.
$endgroup$
– dfhwze
8 hours ago
$begingroup$
If you are new to a language, you should add the beginner tag to your question. Reviewers take this tag into account.
$endgroup$
– dfhwze
8 hours ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
C++ is not Java. You may safely get rid of all
this->
.The client doesn't need to know about
Node
. Make it an inner class of theLinkedList
.The
~LinkedList()
destructor is missing. You should also consider (or prohibit) the copy constructor,operator=
, and move constructor.add(int data, int position)
may silently leak memory. If theposition
is too large, the created node is not linked into the list, and is lost. Also, there is no way to prepend a node.I don't like the
temp_head
approach. Better leave thehead
alone, and iterate using a cursor variable, e.g.:void LinkedList::print()
Node * cursor = head;
while (cursor)
std::cout << cursor->data << 'n';
cursor = cursor->next;
At least, there is no need to restore
head
.Ditto for
add
.
$endgroup$
add a comment |
Your Answer
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
);
);
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%2f225545%2fimplementation-of-a-singularly-linkedlist%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
C++ is not Java. You may safely get rid of all
this->
.The client doesn't need to know about
Node
. Make it an inner class of theLinkedList
.The
~LinkedList()
destructor is missing. You should also consider (or prohibit) the copy constructor,operator=
, and move constructor.add(int data, int position)
may silently leak memory. If theposition
is too large, the created node is not linked into the list, and is lost. Also, there is no way to prepend a node.I don't like the
temp_head
approach. Better leave thehead
alone, and iterate using a cursor variable, e.g.:void LinkedList::print()
Node * cursor = head;
while (cursor)
std::cout << cursor->data << 'n';
cursor = cursor->next;
At least, there is no need to restore
head
.Ditto for
add
.
$endgroup$
add a comment |
$begingroup$
C++ is not Java. You may safely get rid of all
this->
.The client doesn't need to know about
Node
. Make it an inner class of theLinkedList
.The
~LinkedList()
destructor is missing. You should also consider (or prohibit) the copy constructor,operator=
, and move constructor.add(int data, int position)
may silently leak memory. If theposition
is too large, the created node is not linked into the list, and is lost. Also, there is no way to prepend a node.I don't like the
temp_head
approach. Better leave thehead
alone, and iterate using a cursor variable, e.g.:void LinkedList::print()
Node * cursor = head;
while (cursor)
std::cout << cursor->data << 'n';
cursor = cursor->next;
At least, there is no need to restore
head
.Ditto for
add
.
$endgroup$
add a comment |
$begingroup$
C++ is not Java. You may safely get rid of all
this->
.The client doesn't need to know about
Node
. Make it an inner class of theLinkedList
.The
~LinkedList()
destructor is missing. You should also consider (or prohibit) the copy constructor,operator=
, and move constructor.add(int data, int position)
may silently leak memory. If theposition
is too large, the created node is not linked into the list, and is lost. Also, there is no way to prepend a node.I don't like the
temp_head
approach. Better leave thehead
alone, and iterate using a cursor variable, e.g.:void LinkedList::print()
Node * cursor = head;
while (cursor)
std::cout << cursor->data << 'n';
cursor = cursor->next;
At least, there is no need to restore
head
.Ditto for
add
.
$endgroup$
C++ is not Java. You may safely get rid of all
this->
.The client doesn't need to know about
Node
. Make it an inner class of theLinkedList
.The
~LinkedList()
destructor is missing. You should also consider (or prohibit) the copy constructor,operator=
, and move constructor.add(int data, int position)
may silently leak memory. If theposition
is too large, the created node is not linked into the list, and is lost. Also, there is no way to prepend a node.I don't like the
temp_head
approach. Better leave thehead
alone, and iterate using a cursor variable, e.g.:void LinkedList::print()
Node * cursor = head;
while (cursor)
std::cout << cursor->data << 'n';
cursor = cursor->next;
At least, there is no need to restore
head
.Ditto for
add
.
answered 7 hours ago
vnpvnp
42.6k2 gold badges35 silver badges109 bronze badges
42.6k2 gold badges35 silver badges109 bronze badges
add a comment |
add a comment |
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%2f225545%2fimplementation-of-a-singularly-linkedlist%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
1
$begingroup$
If you are new to a language, you should add the beginner tag to your question. Reviewers take this tag into account.
$endgroup$
– dfhwze
8 hours ago