SlideShare a Scribd company logo
Static arrays are structures whose size is fixed at compile time and therefore cannot be extended
or reduced to fit the data set. A dynamic array can be extended by doubling the size but there is
overhead associated with the operation of copying old data and freeing the memory associated
with the old data structure. One potential problem of using arrays for storing data is that arrays
require a contiguous block of memory which may not be available, if the requested contiguous
block is too large. However the advantages of using arrays are that each element in the array can
be accessed very efficiently using an index. However, for applications that can be better
managed without using contiguous memory we define a concept called “linked lists”.
A linked list is a collection of objects linked together by references from one object to another
object. By convention these objects are named as nodes. So the basic linked list is collection of
nodes where each node contains one or more data fields AND a reference to the next node. The
last node points to a NULL reference to indicate the end of the list.
Types of Linked Lists
Linked lists are widely used in many applications because of the flexibility it provides. Unlike
arrays that are dynamically assigned, linked lists do not require memory from a contiguous
block. This makes it very appealing to store data in a linked list, when the data set is large or
device (eg: PDA) has limited memory. One of the disadvantages of linked lists is that they are
not random accessed like arrays. To find information in a linked list one must start from the head
of the list and traverse the list sequentially until it finds (or not find) the node. Another advantage
of linked lists over arrays is that when a node is inserted or deleted, there is no need to “adjust”
the array.
There are few different types of linked lists. A singly linked list as described above provides
access to the list from the head node. Traversal is allowed only one way and there is no going
back. A doubly linked list is a list that has two references, one to the next node and another to
previous node. Doubly linked list also starts from head node, but provide access both ways. That
is one can traverse forward or backward from any node. A multilinked list (see figures 1 & 2) is
a more general linked list with multiple links from nodes. For examples, we can define a Node
that has two references, age pointer and a name pointer. With this structure it is possible to
maintain a single list, where if we follow the name pointer we can traverse the list in alphabetical
order of names and if we traverse the age pointer, we can traverse the list sorted by ages. This
type of node organization may be useful for maintaining a customer list in a bank where same
list can be traversed in any order (name, age, or any other criteria) based on the need.
Designing the Node of a Linked List
Linked list is a collection of linked nodes. A node is a struct with at least a data field and a
reference to a node of the same type. A node is called a self-referential object, since it contains a
pointer to a variable that refers to a variable of the same type. For example, a struct Node that
contains an int data field and a pointer to another node can be defined as follows.
typedef struct node {
int data;
struct node* next;
} node;
node* head = NULL;
Creating the first node
Memory must be allocated for one node and assigned to head as follows.
head = (node*) malloc(sizeof(node));
headdata = 10;
headnext = NULL;
Adding the second node and linking
node* nextnode = malloc(sizeof(node));
nextnodedata = 12;
nextnodenext = NULL;
headnext = nextnode;
Linked list is a data structure in which the items are ordered in a linear way. Although modern
programming languages support very flexible and rich libraries that works with arrays, which use
arrays to represent lists, the principles of building a linked list remain very important. The way
linked lists are implemented is a ground level in order to build more complex data structures
such as trees.
It’s true that almost every operation we can perform on a linked list can be done with an array.
It’s also true that some operations can be faster on arrays compared to linked lists.
However understanding how to implement the basic operations of a linked list such as INSERT,
DELETE, INSERT_AFTER, PRINT and so on is crucial in order to implement data structures as
rooted trees, B-trees, red-black trees, etc.
Big-O notation is a way to express the efficiency of an algorithm. If you’re going to be working
with code, it is extremely important that you understand Big-O. It is, quite literally, the language
we use to express efficiency.
Big-O is an expression of how the execution time of a program scales with the input data. That
is, as the input gets bigger, how much longer will the program take? Just a little bit? A lot
longer?
Suppose you have a function for which does some processing on an array of size N. If foo takes
O(N) time, then, as the array grows (that is, as N increases), the number of seconds foo takes will
also increase in some sort of linear fashion.
The first decision in planning the linked-list implementation of the Queue class is which end of
the list will correspond to the front of the queue. Recall that items need to be added to the rear of
the queue, and removed from the front of the queue. Therefore, we should make our choice
based on whether it is easier to add/remove a node from the front/end of a linked list.
If we keep pointers to both the first and last nodes of the list, we can add a node at either end in
constant time. However, while we can remove the first node in the list in constant time,
removing the last node requires first locating the previous node, which takes time proportional to
the length of the list. Therefore, we should choose to make the end of the list be the rear of the
queue, and the front of the list be the front of the queue.
Other than saving space, the first advantage I can see for singly linked lists over doubly linked
lists is to avoid having to update two links instead of one when you modify the list, when adding
an element for example.
Of course, one might say that you can always ignore the second link, but in that case, it is no
longer doubly linked, but only an unused field.What might also be seen as an advantage is that
singly linked list are necessarily consistent, though possibly wrong when there are bugs in the
program. Doubly linked lists can end-up having inconsistent links forward and backward. But it
is debatable which of the two cases, if any, is an advantage.
update:
I had tried to see some impact on garbage collection, but found none. Actually, there is one when
storage reclamation is done by reference counting, as reference counting is defeated by loops,
and doubly linked lists contain loops. Singly linked list escape the problem. Of course, there are
ways around it for doubly linked lists, such as the use of weak pointers, or programmable
reference counting as part of data abstraction.
Solution
Static arrays are structures whose size is fixed at compile time and therefore cannot be extended
or reduced to fit the data set. A dynamic array can be extended by doubling the size but there is
overhead associated with the operation of copying old data and freeing the memory associated
with the old data structure. One potential problem of using arrays for storing data is that arrays
require a contiguous block of memory which may not be available, if the requested contiguous
block is too large. However the advantages of using arrays are that each element in the array can
be accessed very efficiently using an index. However, for applications that can be better
managed without using contiguous memory we define a concept called “linked lists”.
A linked list is a collection of objects linked together by references from one object to another
object. By convention these objects are named as nodes. So the basic linked list is collection of
nodes where each node contains one or more data fields AND a reference to the next node. The
last node points to a NULL reference to indicate the end of the list.
Types of Linked Lists
Linked lists are widely used in many applications because of the flexibility it provides. Unlike
arrays that are dynamically assigned, linked lists do not require memory from a contiguous
block. This makes it very appealing to store data in a linked list, when the data set is large or
device (eg: PDA) has limited memory. One of the disadvantages of linked lists is that they are
not random accessed like arrays. To find information in a linked list one must start from the head
of the list and traverse the list sequentially until it finds (or not find) the node. Another advantage
of linked lists over arrays is that when a node is inserted or deleted, there is no need to “adjust”
the array.
There are few different types of linked lists. A singly linked list as described above provides
access to the list from the head node. Traversal is allowed only one way and there is no going
back. A doubly linked list is a list that has two references, one to the next node and another to
previous node. Doubly linked list also starts from head node, but provide access both ways. That
is one can traverse forward or backward from any node. A multilinked list (see figures 1 & 2) is
a more general linked list with multiple links from nodes. For examples, we can define a Node
that has two references, age pointer and a name pointer. With this structure it is possible to
maintain a single list, where if we follow the name pointer we can traverse the list in alphabetical
order of names and if we traverse the age pointer, we can traverse the list sorted by ages. This
type of node organization may be useful for maintaining a customer list in a bank where same
list can be traversed in any order (name, age, or any other criteria) based on the need.
Designing the Node of a Linked List
Linked list is a collection of linked nodes. A node is a struct with at least a data field and a
reference to a node of the same type. A node is called a self-referential object, since it contains a
pointer to a variable that refers to a variable of the same type. For example, a struct Node that
contains an int data field and a pointer to another node can be defined as follows.
typedef struct node {
int data;
struct node* next;
} node;
node* head = NULL;
Creating the first node
Memory must be allocated for one node and assigned to head as follows.
head = (node*) malloc(sizeof(node));
headdata = 10;
headnext = NULL;
Adding the second node and linking
node* nextnode = malloc(sizeof(node));
nextnodedata = 12;
nextnodenext = NULL;
headnext = nextnode;
Linked list is a data structure in which the items are ordered in a linear way. Although modern
programming languages support very flexible and rich libraries that works with arrays, which use
arrays to represent lists, the principles of building a linked list remain very important. The way
linked lists are implemented is a ground level in order to build more complex data structures
such as trees.
It’s true that almost every operation we can perform on a linked list can be done with an array.
It’s also true that some operations can be faster on arrays compared to linked lists.
However understanding how to implement the basic operations of a linked list such as INSERT,
DELETE, INSERT_AFTER, PRINT and so on is crucial in order to implement data structures as
rooted trees, B-trees, red-black trees, etc.
Big-O notation is a way to express the efficiency of an algorithm. If you’re going to be working
with code, it is extremely important that you understand Big-O. It is, quite literally, the language
we use to express efficiency.
Big-O is an expression of how the execution time of a program scales with the input data. That
is, as the input gets bigger, how much longer will the program take? Just a little bit? A lot
longer?
Suppose you have a function for which does some processing on an array of size N. If foo takes
O(N) time, then, as the array grows (that is, as N increases), the number of seconds foo takes will
also increase in some sort of linear fashion.
The first decision in planning the linked-list implementation of the Queue class is which end of
the list will correspond to the front of the queue. Recall that items need to be added to the rear of
the queue, and removed from the front of the queue. Therefore, we should make our choice
based on whether it is easier to add/remove a node from the front/end of a linked list.
If we keep pointers to both the first and last nodes of the list, we can add a node at either end in
constant time. However, while we can remove the first node in the list in constant time,
removing the last node requires first locating the previous node, which takes time proportional to
the length of the list. Therefore, we should choose to make the end of the list be the rear of the
queue, and the front of the list be the front of the queue.
Other than saving space, the first advantage I can see for singly linked lists over doubly linked
lists is to avoid having to update two links instead of one when you modify the list, when adding
an element for example.
Of course, one might say that you can always ignore the second link, but in that case, it is no
longer doubly linked, but only an unused field.What might also be seen as an advantage is that
singly linked list are necessarily consistent, though possibly wrong when there are bugs in the
program. Doubly linked lists can end-up having inconsistent links forward and backward. But it
is debatable which of the two cases, if any, is an advantage.
update:
I had tried to see some impact on garbage collection, but found none. Actually, there is one when
storage reclamation is done by reference counting, as reference counting is defeated by loops,
and doubly linked lists contain loops. Singly linked list escape the problem. Of course, there are
ways around it for doubly linked lists, such as the use of weak pointers, or programmable
reference counting as part of data abstraction.

More Related Content

PPTX
Data Structures-UNIT Four_Linked_List.pptx
PPTX
Data Structures and Algorithms - Lec 05.pptx
PPTX
Linear Data Structures - List, Stack and Queue
PPTX
Linked List
PPTX
Link list
PPTX
Link list
PPT
2- link-list.ppt
PPTX
Linked list
Data Structures-UNIT Four_Linked_List.pptx
Data Structures and Algorithms - Lec 05.pptx
Linear Data Structures - List, Stack and Queue
Linked List
Link list
Link list
2- link-list.ppt
Linked list

Similar to Static arrays are structures whose size is fixed at compile time and.pdf (20)

PDF
Linked list (introduction) 1
PPTX
Linked List.pptx
PPTX
linked list.pptx
PDF
Data structure
ODP
Linked List
PPTX
CH4.pptx
PPT
Lecture 2b lists
PPTX
mbit_Unit-2_Linked List.pptx
PPTX
Unit 2 linear data structures
DOCX
Link list assi
PPTX
Linked List Representation of a Linked List.pptx
PPTX
Linked lists
PPTX
linked list in Data Structure, Simple and Easy Tutorial
PPTX
Datastucture-Unit 4-Linked List Presentation.pptx
PPTX
Data Structures_Linear data structures Linked Lists.pptx
PPTX
Linked lists linked lists vs Arrays.pptx
PDF
DS Module 03.pdf
PDF
Linked list basics
DOCX
Linked List
Linked list (introduction) 1
Linked List.pptx
linked list.pptx
Data structure
Linked List
CH4.pptx
Lecture 2b lists
mbit_Unit-2_Linked List.pptx
Unit 2 linear data structures
Link list assi
Linked List Representation of a Linked List.pptx
Linked lists
linked list in Data Structure, Simple and Easy Tutorial
Datastucture-Unit 4-Linked List Presentation.pptx
Data Structures_Linear data structures Linked Lists.pptx
Linked lists linked lists vs Arrays.pptx
DS Module 03.pdf
Linked list basics
Linked List
Ad

More from anjanacottonmills (20)

PDF
Step1 First compound has 2 double bonds ; Second .pdf
PDF
Molarity = molesvolume9liter) Molarity of NH4Cl.pdf
PDF
HOCH2CH2OH is more soluble in water. more OH grou.pdf
PDF
x = -2Solutionx = -2.pdf
PDF
z=(2n+1)4where n=all integersSolutionz=(2n+1)4where n=al.pdf
PDF
This above plant name is Solidago sempervirens, belong to genus Soli.pdf
PDF
The Java Program for the above given isimport java.io.File;impo.pdf
PDF
The fidelity of DNA replication determines the genome stability and .pdf
PDF
E= 0.28V reactants are favored .pdf
PDF
The differences between ipv4 and ipv6 are as below1. Header1.Ch.pdf
PDF
TCP - TCP breaks data into manageable packets and tracks information.pdf
PDF
D) II, III, IV, and V only .pdf
PDF
NumberList.java (implements the linked list)public class NumberLis.pdf
PDF
c) H2(g) + 12 O2(g) -H2O (l) as all are in the.pdf
PDF
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
PDF
Interphase.This is the phase where the cell prepares for the next .pdf
PDF
B.the rotation will be zero as L and D will cance.pdf
PDF
In the above conversation it is belonging to stereotypes.Stereotyp.pdf
PDF
In general, the objective of an audit is to assess the risk of mater.pdf
PDF
if one can understand a few things it is easier to solve these kind .pdf
Step1 First compound has 2 double bonds ; Second .pdf
Molarity = molesvolume9liter) Molarity of NH4Cl.pdf
HOCH2CH2OH is more soluble in water. more OH grou.pdf
x = -2Solutionx = -2.pdf
z=(2n+1)4where n=all integersSolutionz=(2n+1)4where n=al.pdf
This above plant name is Solidago sempervirens, belong to genus Soli.pdf
The Java Program for the above given isimport java.io.File;impo.pdf
The fidelity of DNA replication determines the genome stability and .pdf
E= 0.28V reactants are favored .pdf
The differences between ipv4 and ipv6 are as below1. Header1.Ch.pdf
TCP - TCP breaks data into manageable packets and tracks information.pdf
D) II, III, IV, and V only .pdf
NumberList.java (implements the linked list)public class NumberLis.pdf
c) H2(g) + 12 O2(g) -H2O (l) as all are in the.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
Interphase.This is the phase where the cell prepares for the next .pdf
B.the rotation will be zero as L and D will cance.pdf
In the above conversation it is belonging to stereotypes.Stereotyp.pdf
In general, the objective of an audit is to assess the risk of mater.pdf
if one can understand a few things it is easier to solve these kind .pdf
Ad

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Types and Its function , kingdom of life
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Pre independence Education in Inndia.pdf
PPTX
Lesson notes of climatology university.
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
PPH.pptx obstetrics and gynecology in nursing
Pharmacology of Heart Failure /Pharmacotherapy of CHF
VCE English Exam - Section C Student Revision Booklet
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
Cell Types and Its function , kingdom of life
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pre independence Education in Inndia.pdf
Lesson notes of climatology university.

Static arrays are structures whose size is fixed at compile time and.pdf

  • 1. Static arrays are structures whose size is fixed at compile time and therefore cannot be extended or reduced to fit the data set. A dynamic array can be extended by doubling the size but there is overhead associated with the operation of copying old data and freeing the memory associated with the old data structure. One potential problem of using arrays for storing data is that arrays require a contiguous block of memory which may not be available, if the requested contiguous block is too large. However the advantages of using arrays are that each element in the array can be accessed very efficiently using an index. However, for applications that can be better managed without using contiguous memory we define a concept called “linked lists”. A linked list is a collection of objects linked together by references from one object to another object. By convention these objects are named as nodes. So the basic linked list is collection of nodes where each node contains one or more data fields AND a reference to the next node. The last node points to a NULL reference to indicate the end of the list. Types of Linked Lists Linked lists are widely used in many applications because of the flexibility it provides. Unlike arrays that are dynamically assigned, linked lists do not require memory from a contiguous block. This makes it very appealing to store data in a linked list, when the data set is large or device (eg: PDA) has limited memory. One of the disadvantages of linked lists is that they are not random accessed like arrays. To find information in a linked list one must start from the head of the list and traverse the list sequentially until it finds (or not find) the node. Another advantage of linked lists over arrays is that when a node is inserted or deleted, there is no need to “adjust” the array. There are few different types of linked lists. A singly linked list as described above provides access to the list from the head node. Traversal is allowed only one way and there is no going back. A doubly linked list is a list that has two references, one to the next node and another to previous node. Doubly linked list also starts from head node, but provide access both ways. That is one can traverse forward or backward from any node. A multilinked list (see figures 1 & 2) is a more general linked list with multiple links from nodes. For examples, we can define a Node that has two references, age pointer and a name pointer. With this structure it is possible to maintain a single list, where if we follow the name pointer we can traverse the list in alphabetical order of names and if we traverse the age pointer, we can traverse the list sorted by ages. This type of node organization may be useful for maintaining a customer list in a bank where same list can be traversed in any order (name, age, or any other criteria) based on the need. Designing the Node of a Linked List Linked list is a collection of linked nodes. A node is a struct with at least a data field and a reference to a node of the same type. A node is called a self-referential object, since it contains a
  • 2. pointer to a variable that refers to a variable of the same type. For example, a struct Node that contains an int data field and a pointer to another node can be defined as follows. typedef struct node { int data; struct node* next; } node; node* head = NULL; Creating the first node Memory must be allocated for one node and assigned to head as follows. head = (node*) malloc(sizeof(node)); headdata = 10; headnext = NULL; Adding the second node and linking node* nextnode = malloc(sizeof(node)); nextnodedata = 12; nextnodenext = NULL; headnext = nextnode; Linked list is a data structure in which the items are ordered in a linear way. Although modern programming languages support very flexible and rich libraries that works with arrays, which use arrays to represent lists, the principles of building a linked list remain very important. The way linked lists are implemented is a ground level in order to build more complex data structures such as trees. It’s true that almost every operation we can perform on a linked list can be done with an array. It’s also true that some operations can be faster on arrays compared to linked lists. However understanding how to implement the basic operations of a linked list such as INSERT, DELETE, INSERT_AFTER, PRINT and so on is crucial in order to implement data structures as rooted trees, B-trees, red-black trees, etc. Big-O notation is a way to express the efficiency of an algorithm. If you’re going to be working with code, it is extremely important that you understand Big-O. It is, quite literally, the language we use to express efficiency. Big-O is an expression of how the execution time of a program scales with the input data. That is, as the input gets bigger, how much longer will the program take? Just a little bit? A lot longer? Suppose you have a function for which does some processing on an array of size N. If foo takes O(N) time, then, as the array grows (that is, as N increases), the number of seconds foo takes will also increase in some sort of linear fashion.
  • 3. The first decision in planning the linked-list implementation of the Queue class is which end of the list will correspond to the front of the queue. Recall that items need to be added to the rear of the queue, and removed from the front of the queue. Therefore, we should make our choice based on whether it is easier to add/remove a node from the front/end of a linked list. If we keep pointers to both the first and last nodes of the list, we can add a node at either end in constant time. However, while we can remove the first node in the list in constant time, removing the last node requires first locating the previous node, which takes time proportional to the length of the list. Therefore, we should choose to make the end of the list be the rear of the queue, and the front of the list be the front of the queue. Other than saving space, the first advantage I can see for singly linked lists over doubly linked lists is to avoid having to update two links instead of one when you modify the list, when adding an element for example. Of course, one might say that you can always ignore the second link, but in that case, it is no longer doubly linked, but only an unused field.What might also be seen as an advantage is that singly linked list are necessarily consistent, though possibly wrong when there are bugs in the program. Doubly linked lists can end-up having inconsistent links forward and backward. But it is debatable which of the two cases, if any, is an advantage. update: I had tried to see some impact on garbage collection, but found none. Actually, there is one when storage reclamation is done by reference counting, as reference counting is defeated by loops, and doubly linked lists contain loops. Singly linked list escape the problem. Of course, there are ways around it for doubly linked lists, such as the use of weak pointers, or programmable reference counting as part of data abstraction. Solution Static arrays are structures whose size is fixed at compile time and therefore cannot be extended or reduced to fit the data set. A dynamic array can be extended by doubling the size but there is overhead associated with the operation of copying old data and freeing the memory associated with the old data structure. One potential problem of using arrays for storing data is that arrays require a contiguous block of memory which may not be available, if the requested contiguous block is too large. However the advantages of using arrays are that each element in the array can be accessed very efficiently using an index. However, for applications that can be better managed without using contiguous memory we define a concept called “linked lists”. A linked list is a collection of objects linked together by references from one object to another object. By convention these objects are named as nodes. So the basic linked list is collection of
  • 4. nodes where each node contains one or more data fields AND a reference to the next node. The last node points to a NULL reference to indicate the end of the list. Types of Linked Lists Linked lists are widely used in many applications because of the flexibility it provides. Unlike arrays that are dynamically assigned, linked lists do not require memory from a contiguous block. This makes it very appealing to store data in a linked list, when the data set is large or device (eg: PDA) has limited memory. One of the disadvantages of linked lists is that they are not random accessed like arrays. To find information in a linked list one must start from the head of the list and traverse the list sequentially until it finds (or not find) the node. Another advantage of linked lists over arrays is that when a node is inserted or deleted, there is no need to “adjust” the array. There are few different types of linked lists. A singly linked list as described above provides access to the list from the head node. Traversal is allowed only one way and there is no going back. A doubly linked list is a list that has two references, one to the next node and another to previous node. Doubly linked list also starts from head node, but provide access both ways. That is one can traverse forward or backward from any node. A multilinked list (see figures 1 & 2) is a more general linked list with multiple links from nodes. For examples, we can define a Node that has two references, age pointer and a name pointer. With this structure it is possible to maintain a single list, where if we follow the name pointer we can traverse the list in alphabetical order of names and if we traverse the age pointer, we can traverse the list sorted by ages. This type of node organization may be useful for maintaining a customer list in a bank where same list can be traversed in any order (name, age, or any other criteria) based on the need. Designing the Node of a Linked List Linked list is a collection of linked nodes. A node is a struct with at least a data field and a reference to a node of the same type. A node is called a self-referential object, since it contains a pointer to a variable that refers to a variable of the same type. For example, a struct Node that contains an int data field and a pointer to another node can be defined as follows. typedef struct node { int data; struct node* next; } node; node* head = NULL; Creating the first node Memory must be allocated for one node and assigned to head as follows. head = (node*) malloc(sizeof(node)); headdata = 10;
  • 5. headnext = NULL; Adding the second node and linking node* nextnode = malloc(sizeof(node)); nextnodedata = 12; nextnodenext = NULL; headnext = nextnode; Linked list is a data structure in which the items are ordered in a linear way. Although modern programming languages support very flexible and rich libraries that works with arrays, which use arrays to represent lists, the principles of building a linked list remain very important. The way linked lists are implemented is a ground level in order to build more complex data structures such as trees. It’s true that almost every operation we can perform on a linked list can be done with an array. It’s also true that some operations can be faster on arrays compared to linked lists. However understanding how to implement the basic operations of a linked list such as INSERT, DELETE, INSERT_AFTER, PRINT and so on is crucial in order to implement data structures as rooted trees, B-trees, red-black trees, etc. Big-O notation is a way to express the efficiency of an algorithm. If you’re going to be working with code, it is extremely important that you understand Big-O. It is, quite literally, the language we use to express efficiency. Big-O is an expression of how the execution time of a program scales with the input data. That is, as the input gets bigger, how much longer will the program take? Just a little bit? A lot longer? Suppose you have a function for which does some processing on an array of size N. If foo takes O(N) time, then, as the array grows (that is, as N increases), the number of seconds foo takes will also increase in some sort of linear fashion. The first decision in planning the linked-list implementation of the Queue class is which end of the list will correspond to the front of the queue. Recall that items need to be added to the rear of the queue, and removed from the front of the queue. Therefore, we should make our choice based on whether it is easier to add/remove a node from the front/end of a linked list. If we keep pointers to both the first and last nodes of the list, we can add a node at either end in constant time. However, while we can remove the first node in the list in constant time, removing the last node requires first locating the previous node, which takes time proportional to the length of the list. Therefore, we should choose to make the end of the list be the rear of the queue, and the front of the list be the front of the queue. Other than saving space, the first advantage I can see for singly linked lists over doubly linked lists is to avoid having to update two links instead of one when you modify the list, when adding
  • 6. an element for example. Of course, one might say that you can always ignore the second link, but in that case, it is no longer doubly linked, but only an unused field.What might also be seen as an advantage is that singly linked list are necessarily consistent, though possibly wrong when there are bugs in the program. Doubly linked lists can end-up having inconsistent links forward and backward. But it is debatable which of the two cases, if any, is an advantage. update: I had tried to see some impact on garbage collection, but found none. Actually, there is one when storage reclamation is done by reference counting, as reference counting is defeated by loops, and doubly linked lists contain loops. Singly linked list escape the problem. Of course, there are ways around it for doubly linked lists, such as the use of weak pointers, or programmable reference counting as part of data abstraction.