Objectives of this lecture
What is a linked list?
#define
MINKEY … /*this the smallest possible element – depends
on the
application. It is placed at the header
node to simplify insert and delete process */
typedef
… ListEntry; //type of entry on the
list.
typedef
struct listnode{ ListEntry entry;
struct listnode *next;
} ListNode;
typedef
ListNode *List;
typedef
ListNode *NodePointer;
void
CreateList(List *list);
void
ClearList(List *list);
Boolean
EmptyList(List *list);
Boolean
FullList();
void
Traverse(List *list);
void
Insert(ListEntry newitem, List *list);
void
Delete(ListEntry target, List *list);
/* MakeListNode: creates a node and place entry
item in it.
Pre: The entry item is valid.
Post:
The function creates a new ListNode, initializes it with item,
and returns a pointer to the new node.
*/
ListNode
*MakeListNode(ListEntry item)
{ ListNode *p = (ListNode *)
malloc(sizeof(ListNode));
if (p != NULL)
{ p->entry = item;
p->next = NULL;
} else
Error("cannot obtain space for
additional node.");
return p;
}
/*
CreateList: Initilizes a list
Pre:
None
Post:
Header Node has been created and MINKEY is placed in its
info field and list is set to point
to the header node.
*/
void
CreateList(List *list)
{ *list = MakeListNode(MINKEY);
if (*list == NULL)
ERROR(“No enough memory to create the
list”);
}
Checking
for an empty list involves examining the node after the header node. The list itself never points to NULL because
of the header node.
/*
EmptyList: Checks if the list empty
Pre:
the list has been created
Post:
returns true or false depending on if the list is empty or not
*/
Boolean
EmptyList(List *list);
{
if ((*list)->next == NULL)
return 1;
else
return 0;
}
/*
Traverse: Visit each node in the list and process its entry
Pre:
list has been created
Post:
each entry in the list has been processed
Uses:
Process – depends on the application
*/
void
Traverse(List *list)
{ ListNode *p=(*list)->next;
while (p != NULL)
{
Process(p->entry); /* example
of process is printf */
p=p->next;
}
}
/*
Find Node: searches for a given node in a list
Pre:
list has been created
Post:
returns two pointers: the address of the node before the target
node and that of the target node if found or NULL if not
found.
*/
void
FindNode(List *list, ListEntry target, NodePointer *previous,
NodePointer *current)
{ *previous = *list;
*current = (*list)->next;
while ( (*current !=NULL) &&
(*current)->entry<target)
{
*previous = *current;
*current = (*current)->next;
}
if ( (*current != NULL) &&
(*current)->entry != target)
*current = NULL;
}
/*
Delete: Delete a node containing the target entry if it exists
Pre:
the list is created and contains the target entry
Post:
the node containing the target entry has been deleted
Uses:
FindNode
*/
void Delete(ListEntry
target, List *list)
{ ListNode *current, *previous;
FindNode(list, target, &previous,
¤t);
if (current ==NULL)
ERROR(“Attempt to delete non-existent
node”);
else
{
previous->next = current->next;
free(current);
}
}
/*
Insert: Inserts a node at the appropriate place in the list
Pre: The list has been created and newitem
is a valid entry
Post: The newitem has been inserted.
Uses: FindNode, MakeListNode
*/
void Insert(ListEntry
newitem, List *list)
{ ListNode *newnode, *previous, *ignore;
newnode=MakeListNode(newitem);
if (newnode == NULL)
ERROR(“Attempt to insert into a full
list”);
else
{
FindNode(list, newitem,&previous,&ignore);
newnode->next = previous->next;
previous->next = newnode;
}
}
Other Linked List Representations
typedef
struct ListNode {
ListEntry entry;
struct
ListNode *next;
struct
ListNode *previous;
} ListNode;