linked lists in c, c++ and java
TRANSCRIPT
-
8/6/2019 Linked Lists in C, C++ and Java
1/23
Linked Lists in C, C++ and Java
-
8/6/2019 Linked Lists in C, C++ and Java
2/23
Doubly Linked List in C
Doublylinked list allows you to go through a
list in either direction.
The trick is to include an extra pointer in eachstructure
That stores the address ofthe previous
structure in addition to the pointer to the next
structure.
Compile and run the c program, 11.5
-
8/6/2019 Linked Lists in C, C++ and Java
3/23
How it works
The initial pointer declarations are now as follows:
struct horse *first = NULL; /* Pointer tofirst horse */
struct horse *current = NULL; /* Pointer to current horse */
struct horse *last = NULL; /* Pointer to previous horse */
struct horse /* Horse structure declaration */ {
int age;
int height;
char name[20];
char father[20];
char mother[20];CHAPTER 11 STRUCTURING DATA 429 struct horse *next; /* Pointer to next structure */
struct horse *previous; /* Pointer to previous structure */
};
-
8/6/2019 Linked Lists in C, C++ and Java
4/23
How it works
Two pointers:
One to point forward in the list, called next,
The other to point backward to the preceding structure, called previous.
for the pointer structure member previous. In the beginning ofthe input loop youhave the following
Other changes occur in the beginning ofthe loop if(first == NULL )
{
first = current; /* Set pointer tofirst horse */
current->previous = NULL;
}
else {
last->next = current; /* Set next address for previous horse */
current->previous = last; /* Previous address for current horse */
}
-
8/6/2019 Linked Lists in C, C++ and Java
5/23
How it works, C, doublylinked list
The other change is at the end ofthe input loop:
last = current; /* Save address oflasthorse */
This statement is added to allow the pointerprevious in the next structure tobe set to theappropriate value,
which is the current structure that yourerecording in the variable last.
-
8/6/2019 Linked Lists in C, C++ and Java
6/23
Templates: C++ Function Templates
Overloaded functions normally perform
similar or identicaloperations on different
types ofdata.
Ifthe operations are identicalfor each type,
they can be expressed more compactlyusing
function templates.
Allfunction-template definitions begin with
keyword template followed by a list of
-
8/6/2019 Linked Lists in C, C++ and Java
7/23
Templates: C++ Function Templates
Allfunction-template definitions begin with
keyword template followed by a list of
template parameters to the function template
enclosed in angle brackets (< and >)
Each template parameter that represents a
type must be preceded by either ofthe
keywords class or typenane
-
8/6/2019 Linked Lists in C, C++ and Java
8/23
Templates: C++ Function Templates
Template declaration example:
template< typename T >
or or
template< class ElementType >
template< typename BorderType, typenameFillType >
-
8/6/2019 Linked Lists in C, C++ and Java
9/23
Templates continued
The type template parameters are used to
specify types offunction arguments and
function return types
Keywords typename and class used to specify
function-template parameters actuallymean
"anyfundamental type or user-defined type.
-
8/6/2019 Linked Lists in C, C++ and Java
10/23
Templates continued
Load, compile and run 14_01.cpp
Note that templates saves youwriting the
same function logic for different data types
-
8/6/2019 Linked Lists in C, C++ and Java
11/23
Templates continued
void printArray( const int * const, int );
void printArray( const double * const, int );
void printArray( const char * const, int );
-
8/6/2019 Linked Lists in C, C++ and Java
12/23
Class Templates
Class templates are called parameterized
types, because they require one or more
typeparameters to specify how to customize a
"generic class" template
-
8/6/2019 Linked Lists in C, C++ and Java
13/23
Class Templates
To produce a varietyofclass-template
specializations youwrite onlyone
class-template definition. Each time anadditional class-template specialization is
needed, youuse a concise, simple notation,
and the compiler writes the source code for
the specialization you
-
8/6/2019 Linked Lists in C, C++ and Java
14/23
Class Templates
One Stack class template, for example, could
thus become the basis for creating many Stack
classes
"Stack ofdouble
"Stack ofint"
"Stack ofchar "Stack ofEmployee"
-
8/6/2019 Linked Lists in C, C++ and Java
15/23
Class Templates
Creating Class Template Stack< T>
Note the Stack class-template definition in Fig,
14,2, It looks like a conventional classdefinition, except that it's preceded by the
header (line 6)
template< typename T >
-
8/6/2019 Linked Lists in C, C++ and Java
16/23
Class Templates
member-function definitions that appear
outside the class template definition each
begin with the header
template< typename T >
Load compile run 14_03.cpp
-
8/6/2019 Linked Lists in C, C++ and Java
17/23
C++ Linked Lists
Linked lists are collections ofdata items
logically "lined up in a row"
insertions and removals are made anywherein a linked list.
A self-referential class contains a pointer
member that points to a class object ofthe
same class type.
-
8/6/2019 Linked Lists in C, C++ and Java
18/23
C++ Linked Lists
The programofFigs. 20.320.5 uses a List
class template
The primary Li st functions are insertAt- Front (lines 6274)
insertAtBack (lines 7789)
removeFromFront (lines 92110) removeFromBack (lines 113140)
-
8/6/2019 Linked Lists in C, C++ and Java
19/23
C++ Linked Lists
Function i sEmpty(lines 143147) is called a
predicate functionit does not alter the List;
rather, it determines whether the List is empty
(i.e., the pointer to the first node ofthe List is
NULL)
-
8/6/2019 Linked Lists in C, C++ and Java
20/23
Java Linked Lists
Alinked list is a linear collection (i.e., a
sequence) ofself-referential-class objects,
connected by reference linkshence, theterm linked list. Typically, a program
accesses a linked list via a reference to the first
node in the list.
-
8/6/2019 Linked Lists in C, C++ and Java
21/23
Java Linked Lists
The programofFigs. 17.317.5 uses an object
ofour List class tomanipulate a list of
miscellaneous objects. The program consists
offour classes
ListNode
List
EmptyListException
ListTest
-
8/6/2019 Linked Lists in C, C++ and Java
22/23
Java Linked Lists
Class ListNode (Fig. 17.3, lines 637) declares
package-access fields data and next-Node.
The data field is an Object reference, so it canrefer to anyobject.
ListNode member nextNode stores a reference
to the next ListNode object in the linked list
(or null ifthe node is the last one in the list).
-
8/6/2019 Linked Lists in C, C++ and Java
23/23
Java Linked Lists
Method isEmpty(lines 121124) is a
predicate method that determines whether
the list is empty(i.e., the reference to thefirstnode ofthe list is null)
Load Compile and run the code in Eclipse