How do I initiliaze a head in a doubly-linked list to be NULL?
up vote
0
down vote
favorite
I have a homework to implement a doubly-linked list in C++ with two structs. One of them should hold the next and previous pointers, as well as the data. The other struct should hold two pointers, one for the head and one for the tail
Struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
Struct Dlist
Dlist * head;
Dlist * tail;
//some functions
the problem I'm having is I don't know how to initialize both the head and tail to be null in order to use them. Any help would be greatly appreciated!
c++
add a comment |
up vote
0
down vote
favorite
I have a homework to implement a doubly-linked list in C++ with two structs. One of them should hold the next and previous pointers, as well as the data. The other struct should hold two pointers, one for the head and one for the tail
Struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
Struct Dlist
Dlist * head;
Dlist * tail;
//some functions
the problem I'm having is I don't know how to initialize both the head and tail to be null in order to use them. Any help would be greatly appreciated!
c++
3
What is wrong with assigningNULL
to each?
– Scott Hunter
Nov 10 at 23:06
Hey Scott, thanks for the answer. I'm quite new to C++ so I didn't know if this was allowed :)
– Tonislav Tachev
Nov 10 at 23:10
Usually, the best choice for C++ is nullptr , a literal since C++11.
– 2785528
Nov 10 at 23:23
See also: stackoverflow.com/a/177007/2785528
– 2785528
Nov 10 at 23:48
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a homework to implement a doubly-linked list in C++ with two structs. One of them should hold the next and previous pointers, as well as the data. The other struct should hold two pointers, one for the head and one for the tail
Struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
Struct Dlist
Dlist * head;
Dlist * tail;
//some functions
the problem I'm having is I don't know how to initialize both the head and tail to be null in order to use them. Any help would be greatly appreciated!
c++
I have a homework to implement a doubly-linked list in C++ with two structs. One of them should hold the next and previous pointers, as well as the data. The other struct should hold two pointers, one for the head and one for the tail
Struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
Struct Dlist
Dlist * head;
Dlist * tail;
//some functions
the problem I'm having is I don't know how to initialize both the head and tail to be null in order to use them. Any help would be greatly appreciated!
c++
c++
asked Nov 10 at 23:03
Tonislav Tachev
33
33
3
What is wrong with assigningNULL
to each?
– Scott Hunter
Nov 10 at 23:06
Hey Scott, thanks for the answer. I'm quite new to C++ so I didn't know if this was allowed :)
– Tonislav Tachev
Nov 10 at 23:10
Usually, the best choice for C++ is nullptr , a literal since C++11.
– 2785528
Nov 10 at 23:23
See also: stackoverflow.com/a/177007/2785528
– 2785528
Nov 10 at 23:48
add a comment |
3
What is wrong with assigningNULL
to each?
– Scott Hunter
Nov 10 at 23:06
Hey Scott, thanks for the answer. I'm quite new to C++ so I didn't know if this was allowed :)
– Tonislav Tachev
Nov 10 at 23:10
Usually, the best choice for C++ is nullptr , a literal since C++11.
– 2785528
Nov 10 at 23:23
See also: stackoverflow.com/a/177007/2785528
– 2785528
Nov 10 at 23:48
3
3
What is wrong with assigning
NULL
to each?– Scott Hunter
Nov 10 at 23:06
What is wrong with assigning
NULL
to each?– Scott Hunter
Nov 10 at 23:06
Hey Scott, thanks for the answer. I'm quite new to C++ so I didn't know if this was allowed :)
– Tonislav Tachev
Nov 10 at 23:10
Hey Scott, thanks for the answer. I'm quite new to C++ so I didn't know if this was allowed :)
– Tonislav Tachev
Nov 10 at 23:10
Usually, the best choice for C++ is nullptr , a literal since C++11.
– 2785528
Nov 10 at 23:23
Usually, the best choice for C++ is nullptr , a literal since C++11.
– 2785528
Nov 10 at 23:23
See also: stackoverflow.com/a/177007/2785528
– 2785528
Nov 10 at 23:48
See also: stackoverflow.com/a/177007/2785528
– 2785528
Nov 10 at 23:48
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
Initialize the pointers with NULL when you create a DlistElem and Dlist.
struct DlistElem
DlistElem* prev;
DlistElem* next;
int data;
DlistElem() :
prev(NULL),
next(NULL),
data()
struct Dlist
Dlist* head;
Dlist* tail;
Dlist() :
head(NULL),
tail(NULL)
add a comment |
up vote
0
down vote
Another way this can be done in c++11 is:
struct DlistElem
int data;
DlistElem* next = nullptr;
DlistElem* prev = nullptr;
struct Dlist
Dlist * head = nullptr;
Dlist * tail = nullptr;
add a comment |
up vote
0
down vote
I don't know how to initialize both the head and tail to be null in order to use them.
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
DlistElem() : data, next, prev
;
or
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
;
One thing I forgot to mention is, how do I later on access it some methods ?
– Tonislav Tachev
Nov 10 at 23:30
@TonislavTachev What is "on access it some methods ?" ?
– Swordfish
Nov 11 at 5:23
@TonislavTachev The same way you access all other struct members? Not sure what your problem is.
– Fei Xiang
Nov 11 at 5:26
Since I want to use the head in let's say an InsertAtFirst() method, where I first check if the head is null, afterwards if it is I'll proceed to add a new node. But I'm getting an error message, saying that head wasn't defined in this scope
– Tonislav Tachev
Nov 11 at 11:07
@TonislavTachev comments are not for questions. if you have another question, go and ask another question.
– Swordfish
Nov 11 at 11:23
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: "1"
;
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53244284%2fhow-do-i-initiliaze-a-head-in-a-doubly-linked-list-to-be-null%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Initialize the pointers with NULL when you create a DlistElem and Dlist.
struct DlistElem
DlistElem* prev;
DlistElem* next;
int data;
DlistElem() :
prev(NULL),
next(NULL),
data()
struct Dlist
Dlist* head;
Dlist* tail;
Dlist() :
head(NULL),
tail(NULL)
add a comment |
up vote
0
down vote
Initialize the pointers with NULL when you create a DlistElem and Dlist.
struct DlistElem
DlistElem* prev;
DlistElem* next;
int data;
DlistElem() :
prev(NULL),
next(NULL),
data()
struct Dlist
Dlist* head;
Dlist* tail;
Dlist() :
head(NULL),
tail(NULL)
add a comment |
up vote
0
down vote
up vote
0
down vote
Initialize the pointers with NULL when you create a DlistElem and Dlist.
struct DlistElem
DlistElem* prev;
DlistElem* next;
int data;
DlistElem() :
prev(NULL),
next(NULL),
data()
struct Dlist
Dlist* head;
Dlist* tail;
Dlist() :
head(NULL),
tail(NULL)
Initialize the pointers with NULL when you create a DlistElem and Dlist.
struct DlistElem
DlistElem* prev;
DlistElem* next;
int data;
DlistElem() :
prev(NULL),
next(NULL),
data()
struct Dlist
Dlist* head;
Dlist* tail;
Dlist() :
head(NULL),
tail(NULL)
answered Nov 10 at 23:09
Ted Lyngmo
1,795315
1,795315
add a comment |
add a comment |
up vote
0
down vote
Another way this can be done in c++11 is:
struct DlistElem
int data;
DlistElem* next = nullptr;
DlistElem* prev = nullptr;
struct Dlist
Dlist * head = nullptr;
Dlist * tail = nullptr;
add a comment |
up vote
0
down vote
Another way this can be done in c++11 is:
struct DlistElem
int data;
DlistElem* next = nullptr;
DlistElem* prev = nullptr;
struct Dlist
Dlist * head = nullptr;
Dlist * tail = nullptr;
add a comment |
up vote
0
down vote
up vote
0
down vote
Another way this can be done in c++11 is:
struct DlistElem
int data;
DlistElem* next = nullptr;
DlistElem* prev = nullptr;
struct Dlist
Dlist * head = nullptr;
Dlist * tail = nullptr;
Another way this can be done in c++11 is:
struct DlistElem
int data;
DlistElem* next = nullptr;
DlistElem* prev = nullptr;
struct Dlist
Dlist * head = nullptr;
Dlist * tail = nullptr;
answered Nov 11 at 0:35
Patrick
808
808
add a comment |
add a comment |
up vote
0
down vote
I don't know how to initialize both the head and tail to be null in order to use them.
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
DlistElem() : data, next, prev
;
or
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
;
One thing I forgot to mention is, how do I later on access it some methods ?
– Tonislav Tachev
Nov 10 at 23:30
@TonislavTachev What is "on access it some methods ?" ?
– Swordfish
Nov 11 at 5:23
@TonislavTachev The same way you access all other struct members? Not sure what your problem is.
– Fei Xiang
Nov 11 at 5:26
Since I want to use the head in let's say an InsertAtFirst() method, where I first check if the head is null, afterwards if it is I'll proceed to add a new node. But I'm getting an error message, saying that head wasn't defined in this scope
– Tonislav Tachev
Nov 11 at 11:07
@TonislavTachev comments are not for questions. if you have another question, go and ask another question.
– Swordfish
Nov 11 at 11:23
add a comment |
up vote
0
down vote
I don't know how to initialize both the head and tail to be null in order to use them.
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
DlistElem() : data, next, prev
;
or
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
;
One thing I forgot to mention is, how do I later on access it some methods ?
– Tonislav Tachev
Nov 10 at 23:30
@TonislavTachev What is "on access it some methods ?" ?
– Swordfish
Nov 11 at 5:23
@TonislavTachev The same way you access all other struct members? Not sure what your problem is.
– Fei Xiang
Nov 11 at 5:26
Since I want to use the head in let's say an InsertAtFirst() method, where I first check if the head is null, afterwards if it is I'll proceed to add a new node. But I'm getting an error message, saying that head wasn't defined in this scope
– Tonislav Tachev
Nov 11 at 11:07
@TonislavTachev comments are not for questions. if you have another question, go and ask another question.
– Swordfish
Nov 11 at 11:23
add a comment |
up vote
0
down vote
up vote
0
down vote
I don't know how to initialize both the head and tail to be null in order to use them.
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
DlistElem() : data, next, prev
;
or
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
;
I don't know how to initialize both the head and tail to be null in order to use them.
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
DlistElem() : data, next, prev
;
or
struct DlistElem
int data;
DlistElem* next;
DlistElem* prev;
;
edited Nov 11 at 5:23
answered Nov 10 at 23:14
Swordfish
1
1
One thing I forgot to mention is, how do I later on access it some methods ?
– Tonislav Tachev
Nov 10 at 23:30
@TonislavTachev What is "on access it some methods ?" ?
– Swordfish
Nov 11 at 5:23
@TonislavTachev The same way you access all other struct members? Not sure what your problem is.
– Fei Xiang
Nov 11 at 5:26
Since I want to use the head in let's say an InsertAtFirst() method, where I first check if the head is null, afterwards if it is I'll proceed to add a new node. But I'm getting an error message, saying that head wasn't defined in this scope
– Tonislav Tachev
Nov 11 at 11:07
@TonislavTachev comments are not for questions. if you have another question, go and ask another question.
– Swordfish
Nov 11 at 11:23
add a comment |
One thing I forgot to mention is, how do I later on access it some methods ?
– Tonislav Tachev
Nov 10 at 23:30
@TonislavTachev What is "on access it some methods ?" ?
– Swordfish
Nov 11 at 5:23
@TonislavTachev The same way you access all other struct members? Not sure what your problem is.
– Fei Xiang
Nov 11 at 5:26
Since I want to use the head in let's say an InsertAtFirst() method, where I first check if the head is null, afterwards if it is I'll proceed to add a new node. But I'm getting an error message, saying that head wasn't defined in this scope
– Tonislav Tachev
Nov 11 at 11:07
@TonislavTachev comments are not for questions. if you have another question, go and ask another question.
– Swordfish
Nov 11 at 11:23
One thing I forgot to mention is, how do I later on access it some methods ?
– Tonislav Tachev
Nov 10 at 23:30
One thing I forgot to mention is, how do I later on access it some methods ?
– Tonislav Tachev
Nov 10 at 23:30
@TonislavTachev What is "on access it some methods ?" ?
– Swordfish
Nov 11 at 5:23
@TonislavTachev What is "on access it some methods ?" ?
– Swordfish
Nov 11 at 5:23
@TonislavTachev The same way you access all other struct members? Not sure what your problem is.
– Fei Xiang
Nov 11 at 5:26
@TonislavTachev The same way you access all other struct members? Not sure what your problem is.
– Fei Xiang
Nov 11 at 5:26
Since I want to use the head in let's say an InsertAtFirst() method, where I first check if the head is null, afterwards if it is I'll proceed to add a new node. But I'm getting an error message, saying that head wasn't defined in this scope
– Tonislav Tachev
Nov 11 at 11:07
Since I want to use the head in let's say an InsertAtFirst() method, where I first check if the head is null, afterwards if it is I'll proceed to add a new node. But I'm getting an error message, saying that head wasn't defined in this scope
– Tonislav Tachev
Nov 11 at 11:07
@TonislavTachev comments are not for questions. if you have another question, go and ask another question.
– Swordfish
Nov 11 at 11:23
@TonislavTachev comments are not for questions. if you have another question, go and ask another question.
– Swordfish
Nov 11 at 11:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2fstackoverflow.com%2fquestions%2f53244284%2fhow-do-i-initiliaze-a-head-in-a-doubly-linked-list-to-be-null%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
3
What is wrong with assigning
NULL
to each?– Scott Hunter
Nov 10 at 23:06
Hey Scott, thanks for the answer. I'm quite new to C++ so I didn't know if this was allowed :)
– Tonislav Tachev
Nov 10 at 23:10
Usually, the best choice for C++ is nullptr , a literal since C++11.
– 2785528
Nov 10 at 23:23
See also: stackoverflow.com/a/177007/2785528
– 2785528
Nov 10 at 23:48