3. Linked Lists: Introduction
A lists (Linear linked list) can be defined as a collection of variable number of data items
Ex. 2.1: Brokerage Firm Data
Suppose a brokerage firm maintains a
file where each record contains a
customer’s name and his or her
salesman
S. No. Customer Salesperson
1 Adams Smith
2 Brown Ray
3 Clark Jones
4 Drew Ray
5 Evans Smith
6 Farmer Jones
7 Geller Ray
8 Hill Smith
9 Infeld Ray
Is it the most effective way to represent the data?
No
4. Linked Lists: Introduction
Let us try to solve this, so as to have following representation
Ex. 2.1: Brokerage Firm Data
Suppose the firm wants to list the
customers for the given salesperson
S. No. Customer Salesperson
Pointer
1 Adams 3
2 Brown 2
3 Clark 1
4 Drew 2
5 Evans 3
6 Farmer 1
7 Geller 2
8 Hill 3
9 Infeld 2
Is it the most effective way to
represent the data?
No
Salespers
on Pointer
Salesperson
1 Jones
2 Ray
3 Smith
5. Linked Lists: Introduction
Following representation will solve the problem
S. No. Customer Link
1 Adams 5
2 Brown 4
3 Clark 6
4 Drew 7
5 Evans 8
6 Farmer 0
7 Geller 9
8 Hill 0
9 Infeld 0
Is it the most effective way to represent the data?
Yes
Salesperson
Pointer
Salesperso
n
Start
Pointer
1 Jones 3
2 Ray 2
3 Smith 1
Jones Brow
n
Drew Geller Infeld
6. Linked List
A linked list is a way to store a collection of elements. Like an array these can be character or integers.
Each element in a linked list is stored in the form of a node.
Definition
• Pointer
• Structure
Node
7. Pointers
If you have a variable in your program, will give you its address in the memory
Pointers (pointer variables)
are special variables that are
used to store addresses
rather than values.
Pointer Syntax
int* p;
(declared a pointer
p of int type.)
int *p1;
int * p2;
(declared two
pointers p1 and
p2)
int* p1, p2;
(declared a
pointer p1 and a
normal variable p2)
8. Accessing Pointers
Assigning addresses to Pointers
int* pc, c;
c = 5;
pc = &c;
Output: 5 Output: 1
1
What is the output ?
int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc);
What is the output
int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c);
printf("%d", *pc);
9. Structure
In C programming, a struct (or structure) is a collection of variables (can be of different
types) under a single name.
struct Person {
char name[50];
int IDNo;
float salary;
};
struct Person {
// code
};
int main() {
struct Person person1,
person2, p[20];
return 0;
}
struct Person {
// code
} person1, person2, p[20];
Create Struct Variables
• person1 and person2 are struct Person variables
• p[] is a struct Person array of size 20.
10. Node of a Linked List
Each node of a linked list consists of following component
struct node
{
int data;
struct node *next;
};
Node Representation
• A data item
• An address of another node
We wrap both the data item and the next
node reference in a struct as:
11. Arrays Vs Linked List
Data Structure:
• Contiguous
Memory
Allocation:
• Typically
allocated to the
whole array
Insertion/
Deletion:
• Inefficient
Access:
• Random
Data Structure:
• Non-Contiguous
Memory
Allocation:
• Typically
allocated one by
one to individual
elements
Insertion/
Deletion:
• Efficient
Access:
• Sequential
Linked
List
Arrays
12. Stack
A stack is also an ordered collection of elements like arrays, but it has a special feature that
deletion and insertion of elements can be done only from one end called the top of the stack
(TOP)
Definition
Due to this property, it is also
called as last in first out type of
data structure (LIFO).
13. Stack
When an element is inserted into a stack or removed from the stack, its base remains fixed
where the top of stack changes
Insertion of element into
stack is called PUSH and
deletion of element from
stack is called POP.
14. Stack: Implementation
The stack can
be
implemented
into two ways:
Using arrays (Static implementation)
Using Linked List (Dynamic implementation)
15. Queue
A Queue is also an ordered collection of elements that follows the principle of "First in, First out"
(FIFO), where the first element added to the queue is the first one to be removed.
Definition
Due to this property, it is also
called as First in first out type of
data structure (FIFO).
16. Queue
In a queue new elements are added to the queue from one end called REAR end and the
element are always removed from the other end called the FRONT end.
Insertion of element into
queue is called Enqueue
and deletion of element
from queue is called
Dequeue.
17. Queue: Implementation
The Queue can
be
implemented
into two ways:
Using arrays (Static implementation)
Using Linked List (Dynamic implementation)
18. Tree
Tree data structure is a hierarchical structure that is used to represent and organize data in the
form of parent child relationship.
Definition
• There is a special data item at the top of hierarchy
called the Root of the tree.
• The remaining data items are partitioned into
number of mutually exclusive subset, each of which
is itself, a tree which is called the sub tree.
• The tree always grows in length towards bottom
in data structures, unlike natural trees which grows
upwards.
19. Graph
A graph G(V,E) is a set of vertices V and a set of edges E.
Definition
A graph data structure is a collection of nodes that have
data and are connected to other nodes.
20. Graph
An edge connects a pair of vertices and many have weight such as length, cost and another
measuring instrument for according the graph.
Vertices on the graph are shown as point or circles and edges are drawn as arcs or line
segment.
Example of graph:
21. Question
1. What is the correct syntax for declaring an integer array in C?
2. What is the major limitation of arrays?
A. They can only store integers.
B. They require predefined size.
C. They can only store characters.
D. They are immutable.
C
B
22. Question
3. What does each node in a linked list consist of?
A. Only data
B. Only a pointer to the next node
C. Data and a pointer to another node
D. None of the above
4. In a graph, what does an edge represent?
A. single node
B. A connection between two vertices
C. The weight of the graph
D. A hierarchical structure
C
B