Title: Linked Lists
1Linked Lists
linked list - Is an ordered collection of data in
which each element contains the location of the
next element that is, each element contains two
parts data and link. The data part holds the
useful information the data to be processed.
The link is used to chain the data together it
contains a pointer that identifies the next node
in the list. A pointer variable points to the
first node in the list. The name of the list is
the same as the name of this pointer variable.
The link in the last node contains NULL,
indicating the end of the list. The following is
a logical view of a link list named pList with 3
nodes.
node
pList
data
link
data
link
data
link
We define an empty linked list to be a single
pointer having the value of NULL. node - is a
structure in a link list that has at least two
fields. One of the fields is a data field the
other is a pointer that contains the address of
the next node in the sequence.
pList
2Linked Lists
A definition of a type appropriate for a node of
a linked list holding student records could
be typedef struct stu_tag char
id10 char name26 int gradePoints st
ruct stu_tag linkp STUDENT When defining
a structure type in C , we have the option of
including a structure tag such as stu_tag after
the reserved word struct. Then the phrase struct
stu_tag is an alternative name for type STUDENT.
Here we use the type struct stu_tag in the
declaration of one component to indicate that the
linkp component of our node points to another
node of the same type. We use struct stu_tag
rather than STUDENT because the compiler has
not yet seen the name STUDENT. We can allocate
and initialize the data components of a node as
follows STUDENT stu1 stu1 (STUDENT )
malloc(sizeof (STUDENT) ) strcpy(stu1-gtid,
234567741 strcpy(stu1-gtname, John
Doe stu1 -gt gradPoint 65
Structure tag
Structure tag use
3Linked Lists
One purpose of using dynamically allocated nodes
is to enable us to grow data structures of
varying size. We accomplish this by connecting
individual nodes. The connection is made by
assigning the memory address of one node to the
linkp component of another node. Assuming we have
allocate memory for another node stud2 we could
then link the two nodes together. stu1-gtlinkp
stu2
stu1
stu2
234567741
John Doe
65
589247555
Fred Smith
94
Without a physical relationship between the
nodes, we need some way to distinguish the
beginning of the list, that is, to identify the
first logical node in the list. This pointer is
known as the head pointer because it points to
the node at the head of the list. In the above
list stu1 would be the head pointer. The link in
the last node contains null, indicating the end
of the list. stu2-gtlinkp NULL
4Linked Lists
Building a simple linked list. - The logic of
building a linked list requires us to allocate
the first node and then enter a simple loop that
allocates additional nodes, filling the
components with data and then linking the node to
the previous node.
STUDENT get_list(void) STUDENT
plist STUDENT prear STUDENT pnew int
numRec 0, i printf("How many records are you
going to enter ? gt ") scanf("d",numRec) plis
t prear (STUDENT )malloc(sizeof (STUDENT)
) printf("enter the data for student No.
1") scanf("ssd", plist-gtid, plist-gtname,
plist-gtgradePoints) for(i 2 i lt numRec
i) pnew (STUDENT )malloc(sizeof
(STUDENT) ) printf("enter the data for student
No. d", i) scanf("ssd",pnew-gtid,
pnew-gtname,pnew-gtgradePoints) prear-gtlinkp
pnew prear pnew prear-gtlinkp
NULL return(plist)
5Linked Lists
Building a simple linked list (continued. - The
following builds a link list with data read from
a file.
STUDENT get_list_file( FILE inp ) STUDENT
plist STUDENT prear STUDENT pnew plist
prear (STUDENT )malloc(sizeof (STUDENT) )
fscanf(inp,"ssd", plist-gtid, plist-gtname,
plist-gtgradePoints) pnew (STUDENT
)malloc(sizeof (STUDENT) ) while(fscanf(inp,"s
sd", pnew-gtid, pnew-gtname,
pnew-gtgradePoints)! EOF)
prear-gtlinkp pnew prear pnew
pnew (STUDENT )malloc(sizeof (STUDENT)
) prear-gtlinkp NULL return(plist)
6Linked Lists
Traversing Linked Lists - Algorithms that
traverse a list, start at the first node and
examine each node in succession until the last
node has been processed. Several different types
of functions use list traversal logic. In
addition to printing the list, we could count the
number of nodes in the list, total a numeric
field in the node, or calculate the average of a
field. The following function would print the
STUDENT linked list.
void printList(STUDENT pList) STUDENT
pWalker pWalker pList while(pWalker)
printf("Student No. s\n", pWalker-gtid)
printf("Student Name s\n", pWalker-gtname)
printf("Grade d\n\n", pWalker-gtgradePoints)
pWalker pWalker-gtlinkp return
7Linked Lists
Traversing Linked Lists (continued)- The
following function would search the STUDENT
linked list for a students name.
int searchList(STUDENT pList, STUDENT
pPre, STUDENT pFound,
char target) pPre NULL pFound
pList while(pFound ! NULL
strcmp((pFound)-gtname, target))
pPre pFound pFound
(pFound)-gtlinkp return(pFound !
0)
8Linked Lists
Primitive Linked List Functions - to work with
linked lists, we need some basic operations that
manipulate the nodes. For example we need
function to add a node or delete a node. The
following function would add a student node to
the STUDENT linked list
STUDENT addNode(STUDENT pList,
STUDENT pPre,
STUDENT pInsert) STUDENT pNew pNew
(STUDENT ) malloc( sizeof( STUDENT)) pNew
pInsert if( pPre NULL) pNew-gtlinkp
pList pList pNew else pNew-
gtlinkp pPre-gtlinkp pPre-gtlinkp
pNew return pList
9Linked Lists
Primitive Linked List Functions (continued) The
following function would delete a student node
from the STUDENT linked list
STUDENT deleteNode(STUDENT pList,
STUDENT pPre,
STUDENT pDelete) if( pPre
NULL) pList pDelete-gtlinkp
else pPre-gtlinkp pDelete-gtlinkp
free( pDelete) return pList
10Linked Lists
Updating a Linked List - The following two
high-level functions (insertNode and removeNote)
were written with the three low-level functions (
searchList, addNode and deleteNode). The
following function deletes a student from the
linked list.
STUDENT removeNode(STUDENT pList) int
searchList(STUDENT pList, STUDENT
pPre, STUDENT pFound,
char target) STUDENT
deleteNode(STUDENT pList, STU
DENT pPre, STUDENT
pDelete) STUDENT pFound,pPre char
name26 printf("Enter the name of a student to
delete gt") scanf("s",name)
if(searchList(pList,pPre,pFound, name))
pList deleteNode(pList,pPre,pFound)
else printf("student not
found\n") return pList
11Linked Lists
Updating a Linked List (continued - The
following function adds a student to the linked
list.
STUDENT insertNode(STUDENT pList) STUDENT
addNode(STUDENT pList, STUDE
NT pPre, STUDENT pInsert
) STUDENT studData printf("enter the data
for student No. 1") scanf("ssd",
studData.id, studData.name, stu
dData.gradePoints) pList addNode(pList,
NULL, studData) return pList
12Linked Lists
Representing a Stack with a Linked List - a
stack is a data structure in which only the top
element can be accessed. To illustrate, the
plates stored in the spring-loaded device in a
buffet line perform like a stack. A Customer
always takes the top plate when a plate is
removed, the plate beneath it moves to the top.
The following diagram shows a stack of three
characters. The letter C, the character at the
top of the stack, is the only one we can access.
We must remove C from the stack in order to
access the symbol . Removing the value from a
stack is called popping the stack, and storing an
item in a stack is called pushing it onto the
stack.
C
2
Stack can be implemented with arrays or linked
lists. The following code implements a stack
with an array structure. The formal parameter
top points to the variable that stores the
subscript of the element at the top of the stack.
Each push operation increments the value pointed
to by the top before storing the new item at the
top of the stack . Each pop operation returns
the item currently at the top of the stack and
then decrements the value pointed to by top. The
if condition in push checks that there is room on
the stack before storing a new item. The if
condition in pop checks that the stack is not
empty before popping it. If the stack is empty,
STACK_EMPTY (a previously defined constant
macro) is returned instead.
13Linked Lists
Representing a Stack with a Linked List
(continued) -
void push(char stack, / input/output -
the stack / char item, / input - data
being pushed onto the stack / int
top, / input/output - pointer to top
of stack / int max_size) / input -
maximum size of stack / if (top lt
max_size-1) (top)
stacktop item
14Linked Lists
Representing a Stack with a Linked List
(continued) -
char pop(char stack, / input/output -
the stack / int top) /
input/output - pointer to top of stack /
char item / value popped off the
stack / if (top gt 0) item
stacktop --(top) else
item STACK_EMPTY return
(item)
15Linked Lists
Representing a Stack with a Linked List
(continued) - We have seen that in a stack,
elements are inserted (pushed) and removed
(popped) at the same end of the list, the top of
the stack. Since the element that is removed
first is the one that has been waiting the
shortest length of time, a stack is called a
last-in, first-out list (LIFO). A stack can also
be implemented as a linked list in which all
insertions and deletions are performed at the
list head. A logical view of a stack represented
by a linked list is shown below
Stack of three characters
C
C
2
s
data
link
data
link
data
link
2
Stack after insertion (push ) of /
C
2
/
data
link
data
link
data
link
s
data
link
/
C
2
16Linked Lists
Representing a Stack with a Linked List
(continued) - The typedefs below define a node
for a stack type.
/ header file 14-22.h / typedef struct
stack_node_s char element
struct stack_node_s restp stack_node_t
17Linked Lists
Representing a Stack with a Linked List
(continued) -
/ Creates and manipulates a stack of
characters / include ltstdio.hgt include
ltstdlib.hgt include "14-22.h" / Include
typedefs from revised .h file / void
push(stack_node_t sp, char c) char
pop(stack_node_t sp) int main(void)
stack_node_t s NULL / stack of characters -
initially empty / / Builds first stack
of Fig. 14.21 / push(s, '2')
push(s, '') push(s, 'C') /
Completes second stack of Fig. 14.21 /
push(s, '/') / Empties stack element
by element / printf("\nEmptying
stack \n") do
printf("c\n", pop(s)) while (s !
NULL) return (0)
18Linked Lists
Representing a Stack with a Linked List
(continued) -
/ The value in c is placed on top of the
stack accessed through topp Pre the stack is
defined / void push(stack_node_t topp, char
c) stack_node_t newp / pointer to new
stack node / / Creates and defines new
node / newp
(stack_node_t )malloc(sizeof (stack_node_t))
newp-gtelement c newp-gtrestp
topp / Sets stack pointer to point to
new node / topp newp
19Linked Lists
Representing a Stack with a Linked List
(continued) -
/ Removes and frees top node of stack,
returning character value stored there.
Pre the stack is not empty / char
pop(stack_node_t topp) stack_node_t
to_freep / pointer to node removed /
char ans / value at top of stack /
to_freep topp / saves
pointer to node being deleted/ ans
to_freep-gtelement / retrieves value to
return / topp to_freep-gtrestp /
deletes top node / free(to_freep)
/ deallocates space / return
(ans)
Output Emptying stack / C 2