Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Linked Lists Lecture Notes

LINKED LISTS
What is a linked list?
- is a data structure in which each data item points to the
next data item. This “linking” is accomplished by
keeping an address variable (a pointer) together with each
data item.
- This pointer is used to store the address of the next data
item in the list.
- The structure that is used to store one element of a linked
list is called a node.
- A node has two parts: data and next address. The data
part contains the necessary information about the items of
the list, the next address part contains the address of the
next node.
Accessing and Creating Linked Lists:
A special pointer will be used to point to the first node of a
linked list.
- pointer head.
o At the beginning, when there are no nodes, head will
be NULL (pointer pointing to nowhere).
o As soon as there is a node in the list, head will
contain the address of this first node.
- A list with no nodes is called an empty list or a null list.
o A NULL Pointer is used to signal the end of a list.
The last node will have NULL in its next address
part.

- Dynamic Memory Allocation: The memory to store a


node of a linked list is allocated whenever needed. This
allocation is made by the help of a function called
malloc.
malloc is defined in <stdlib.h>
The function prototype for malloc is:
void * malloc(size_t size);
- Freeing Allocated Memory:
Whenever an allocated memory block is not necessary, it
may be freed by using free function defined in
stdlib.h.
void * free( void *block);

- Defining a Node: Assume that we will keep information


about courses in a linked list. The information consists of
the:
o course code
o course name
o credits
o no. of lecture hours
o no. of lab hours.
Each course will point to the
course to which it is a prerequisite.
Simple Node Example:
typedef struct node_s
{
int item; /* data consisting of an integer */

struct node_s *next; /* pointer to the next node */

} node_t;

Another Node Example:


typedef struct node_s
{
char name[NAMELEN]; /* data: name */
int year; /* data: year */
struct node_s *next; /* pointer to the next node
*/
} node_t;
More Complex Node Example:
Data part:
typedef struct
{
char code[8]; /* course code */
char name[26]; /* course name */
int credits; /* course credits */
int lectures; /* no. of lecture hours */
int labs; /* no. of lab hours */
} course_t;
//Node: declaration

typedef struct node_s


{
course_t course; /* course information */

struct node_s *next; /* pointer to the next node */

} node_t;
// Pointer to a node:
typedef node_t *nodeptr;

//Declaring a node:
node_t course_node;
//Declaring a pointer to a node:

nodeptr ptr;
node_t *ptr; /* This has exactly the same meaning */

// Allocating space for a node: (Dynamic Memory Allocation)

ptr = (node_t *) malloc(sizeof(node_t));

//Getting a new node:function


/* allocates memory for a new node */

nodeptr getnode(void)
{
nodeptr ptr;
ptr = (node_t *)malloc(sizeof(node_t));
return(ptr);
}
Actions Related With Linked Lists:
(These will be explained in the lecture.)
Adding a node to a Linked List:
(a) To the beginning
(b) To the end
(c) Between two nodes.
(d)Traversing a Linked List.
(e)Deleting a node from a Linked List:

Functions Related with Linked Lists:

· Add (insert) a new node to the beginning


· Add (insert) a new node to the end
· Add (insert) a new node after n'th node
· Search: a node with a given data
· Add (insert) a new node after a node with a given data (uses Search)
· Add (insert) a new node before a node with a given data
· Traverse all nodes
· Delete a node from the beginning
· Delete a node from the end
· Delete a node with a given data
· Delete the n'th node
· Modify the data of all nodes in a linked list
· Modify data of nodes for a given criteria
Examples:
1. Write a program that reads numbers entered by the user and
stores these numbers into a Linked List.
2. Write a program that reads names entered by the user and
stores these names into a Linked List.
uk
DOUBLY LINKED LISTS
What is a Doubly Linked List?
Doubly Linked List (DLL) is a type of Linked List in which
each data item points to the next and also
to the previous data item. This “linking” is accomplished by
keeping two address variables (pointers)
together with each data item. These pointers are used to store
the address of the previous and the
address of the next data items in the list.
The structure that is used to store one element of a linked list
is called a node like in Linked Lists.
A node has three parts: data, previous address and next
address.
The data part contains the necessary information about the
items of the list, the previous address
part contains the address of the previous node, the next
address part contains the address of the
next node.
head
\\
data prev next data prev next data prev next data prev next
or:
head
\\
prev data next prev data next prev data next prev data next
Simple Node Example:
typedef struct node_s
{
int item; /* data consisting of an integer */
struct node_s prev; /* pointer to the previous node */
struct node_s next; /* pointer to the next node */
} node_t;
Another Node Example:
typedef struct node_s
{
char name[NAMELEN]; /* data: name */
int year; /* data: year */
struct node_s prev; /* pointer to the previous node */
struct node_s next; /* pointer to the next node */
} node_t;
More Complex Node Example:
Data part:
typedef struct
{
char code[8]; /* course code */
char name[26];/* course name */
int credits; /* course credits */
int lectures; /* no. of lecture hours */
} course_t;
Node:
typedef struct node_s
{
course_t course; /* course information */
struct node_s prev; /* pointer to the previous node */
struct node_s next; /* pointer to the next node */
} node_t;
Type casting:
Since malloc returns a void pointer, the return value will be converted to a type
which is necessary for the specisific use of the pointer. In the above example,
the pointer will be used to point to a memory block of type node_t.
Conversion to a requested type is possible by an action called casting. It is
posible to convert a value temporarily to a different type by placing the
requested type in paranthesis just to the beginning of an expression, a value of a
function call.

Examples for casting:


int number = 9;
double result;
result = (double)number / 2;

You might also like