0

I am new in Data Structure. I came up with a question when I was trying to write code using linked list using C++.

In linked list, is pointer next, previous defined by the compiler? Or we have to name our own and it does not matter how we call it?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
jsh6303
  • 2,010
  • 3
  • 24
  • 50
  • You'll have to define it yourself. – Paul Draper Oct 30 '13 at 04:37
  • *When we sit down to write programs, we become gods before our worlds, as long as we follow some rules: follow the standard, don't use global variables, and don't code without coffee.* (It's your choice, since you or someone else defined the data structure) – Zeta Oct 30 '13 at 04:37
  • Under normal circumstances, you shouldn't be defining a linked list at all--you should use `std::list` instead (or, more often than not, `std::vector` or `std::deque` instead of using a linked list at all). – Jerry Coffin Oct 30 '13 at 04:43
  • There's a predefined `std::next`, but that's a function. – MSalters Oct 30 '13 at 08:30

1 Answers1

2

next and previous are not predefined. We have to define it ourself. Usually for a single linked list, we come up with a node structure which consists of a data field and anext pointer which is a pointer to a node of the same type. A simple example is as follows:

struct node{
   int data;     //data part
   struct node *next;    // next pointer - points to a node of the same type
}
jester
  • 3,491
  • 19
  • 30