Data Structure Part9 v2
Data Structure Part9 v2
Press
here
#LEARN_IN DEPTH
#Be_professional_in
1 embedded_system
Embedded System
PART 9(Data Structure)
LIFO (STACK)
FIFO (QUEU)
Linked List
[Link] SHENOUDA
Learn in Depth
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
2 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
3
Introduction to Data Structures
embedded_system
Data Structure is a way of collecting and organizing data in such a way that we can perform operations on these data in
an effective way.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
4 embedded_system
Removing Data
Initialize a Structure
Clearing a Structure
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
5 embedded_system
LIFO Buffer
LEARN-IN-DEPTH
Be professional in
embedded system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
6
LIFO Buffers
embedded_system
LAST-In-First-Out (LIFO)
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
Base = Bottom #Be_professional_in
7
LIFO Buffer Example
embedded_system
Head = Temp1
count= 1
Base temp1
LIFO Buffer Structure head
Temp1 = get_temp();
Buffer_add(temp1);
Data Buffer
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
Base = Bottom #Be_professional_in
8
LIFO Buffer Example
embedded_system
Head = Temp2
count= 2
Base temp1
LIFO Buffer Structure
temp2
Temp1 = get_temp();
head
Buffer_add(temp1);
Temp2 = get_temp();
Data Buffer
Buffer_add(temp2);
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
Base = Bottom #Be_professional_in
9
LIFO Buffer Example
embedded_system
Head = Temp3
count= 3
Base temp1
LIFO Buffer Structure
temp2
Temp1 = get_temp();
temp3
Buffer_add(temp1); head
Temp2 = get_temp();
Data Buffer
Buffer_add(temp2);
Temp3 = get_temp();
Buffer_add(temp3);
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
Base = Bottom #Be_professional_in
10
LIFO Buffer Example
embedded_system
Head = Temp2
count= 2
Base temp1
LIFO Buffer Structure
temp2
Temp3 = Buffer_pop();
head Old data
Data Buffer
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
Base = Bottom #Be_professional_in
11
LIFO Buffer Example
embedded_system
Head = Temp1
count= 1
Base temp1
LIFO Buffer Structure head Old data
Temp3 = Buffer_pop();
Old data
Temp2 = Buffer_pop();
Data Buffer
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
Base = Bottom #Be_professional_in
12
LIFO Buffer Example
embedded_system
Head = Temp4
count= 2
Base temp1
LIFO Buffer Structure temp4
Temp3 = Buffer_pop();
head Old data
Temp2 = Buffer_pop();
Temp4 = get_temp();
Data Buffer
Buffer_add(temp4);
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
13
LIFO Structure
embedded_system
Items to track
Starting point in memory (BASE)
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
14
Stacks
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
15
Basic features of Stack
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
16
Applications of Stack
embedded_system
The simplest application of a stack is to reverse a word. You push a given word
to stack - letter by letter - and then pop letters from the stack.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
17
Implementation of Stack
embedded_system
Stack can be easily implemented using an Array or a Linked List. Arrays are
quick, but are limited in size and Linked List requires overhead to allocate, link,
unlink, and deallocate, but is not limited in size. Here we will implement Stack
using array.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
18
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
19
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
20
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
21
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
22
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
23
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
24
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
25
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
26
Stack Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
27
Status of Stack
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
28 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
29 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
30 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
31
Buffer FULL
embedded_system
Base
???
???
???
???
???
head
First_buffer
(SRAM)
Base = 0x100
Head = 0x100
Length = 5
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
32
Buffer ADD (Push)
embedded_system
Base
item1
??? head
???
???
???
First_buffer
(SRAM)
Base = 0x100
Head = 0x100
Length = 5
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
33
Buffer (POP)
embedded_system
Base
Old value head
???
???
???
???
First_buffer
(SRAM)
Base = 0x100
Head = 0x100
Length = 5
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
34 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
35 embedded_system
[Link]
Follow us
Press
Base = ? here
#LEARN_IN DEPTH
Tail = ? #Be_professional_in
36
Circular Buffer
embedded_system
Head=?
Length=?
FIFO Circular Buffer
Is an example of a First-In-First-
end end end
Out (FIFO) Buffer
head item
Adds data to one end (Head) (ADD)head item
item
Removes data items from the item item
item
other end (Tail) (Remove (Remove)
2items) item Tail item
Head and Tail will wrap around item
Tail empty
when boundary reached item empty
Base head item
Base item empty Base
Tail
Circular Circular
Circular
Buffer Buffer
Buffer
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
37 embedded_system
Logic Sequence
Head =3
Head & Tail pint to Head =1
Count =3
Base end Count =1 end Buffer_add(&buf,0xff);
end
Buffer_add(&buf,0xff);
Count =0 Buffer_add(&buf,0x12);
empty empty empty
Length =5 Buffer_add(&buf,0x34);
empty empty head empty
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
38 embedded_system
Logic Sequence
Head =3 Head =0
Count =2
end
end Count =4
Tail =1 Length =5 0x12
Buffer_add(&buf,0xff); empty Tail =1
0x12
Buffer_add(&buf,0x12); Buffer_add(&buf,0xff);
head empty
Buffer_add(&buf,0x34); Buffer_add(&buf,0x12);
0x43
0x43
Buffer_ren(&buf,&team) Buffer_add(&buf,0x34); 0x12
Tail
Tail 0x12 Buffer_ren(&buf,&team) head empty
Buffer_add(&buf,0x12); Base
Base empty
Buffer_add(&buf,0x12); Circular
Circular Buffer
Buffer
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
39
Queue
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
40
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
41
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
42
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
43
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
44
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
45
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
46
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
47
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
48
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
49
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
50
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
51
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
52
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
53
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
54
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
55
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
56
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
57
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
58
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
59
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
60
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
61
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
62
Queue Implementation with Array
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
63 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
64 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
65 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
66 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
67 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
68 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
69 embedded_system
[Link]
Follow us
Press
#Be_professional_in
70 embedded_system
Structure
linear data structures
which means that there is a
sequence and an order to how they
are constructed sequentially
Examples: arrays and linked lists
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
71 embedded_system
Linked List
LEARN-IN-DEPTH
Be professional in
embedded system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
72
Data Structure - Linked List
embedded_system
A linked list is a linear data structure where each element is a separate object.
A linked-list is a sequence of data structures which are connected together via links.
Link − Each Link of a linked list can store a data called an element.
Next − Each Link of a linked list contain a link to next link called Next.
LinkedList − A LinkedList contains the connection link to the first Link called First.
Linked list can be visualized as a chain of nodes, where every node points to the next node.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
73
Types of Linked List
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
74 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
75
Memory Allocation (Array Vs Linked List)
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
76
Linked List Basic Operations
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
77
Insertion Operation
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
78
Deletion Operation
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
79
Reverse Operation
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
80
Data Structure - Doubly Linked List
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
81
Singly Linked List as Circular
embedded_system
In singly linked list, the next pointer of the last node points to the first node.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
82
Doubly Linked List as Circular
embedded_system
n doubly linked list, the next pointer of the last node points to the first node
and the previous pointer of the first node points to the last node making the
circular in both directions.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
83
Dynamic Linked Lists
embedded_system
Problem Statement
Consider Students Database program, it appears that the program uses (realloc)
when
adding or deleting student member. Using (realloc) may solve the problem,
especially if
the structure size and the number of records are small.
Actually (realloc) function
1. Creates a new buffer with the new size
2. Copies the original contents
3. Deletes the original buffer
4. Returns the address of the new buffer
Consider a complicated SStudent structure containing all student information and
his courses
degrees as shown below:
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
84
Dynamic Linked Lists Contn.
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
85
Understanding the Solution
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
86 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
87
Write the Program
embedded_system
At the beginning of the program only it is required to have one empty pointer,
indicating that
there is no students added.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
88 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
89 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
90
To delete certain record to the list:
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
91 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
92
ViewStudents function:
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
93
DeleteAll function:
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
94 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
95 embedded_system
Write a GetNth() function that takes a linked list and an integer index and returns
the data value stored in the node at that index position.
Example:
Input: 1->10->30->14, index = 2
Output: 30
The node at index 2 is 30
Solution:
1. Initialize count = 0
2. Loop through the link list
a. if count is equal to the passed index then return current
node
b. Increment count
c. change current to point to next of the current.
[Link]
Follow us
Press
#Be_professional_in
96 embedded_system
[Link]
Follow us
Press
#Be_professional_in
97 embedded_system
[Link]
Follow us
Press
#Be_professional_in
98 embedded_system
of a Linked List
Given a Linked List and a number n, write a function that returns the value at
the n’th node from end of the Linked List.
For example, if input is below list and n = 3, then output is “B”
[Link]
Follow us
Press
#Be_professional_in
99 embedded_system
of a Linked List
Given a Linked List and a number n, write a function that returns the value at
the n’th node from end of the Linked List.
For example, if input is below list and n = 3, then output is “B”
[Link]
Follow us
#Be_professional_in
List
Method 2 (Use two pointers)
Maintain two pointers – reference pointer
and main pointer.
Initialize both reference and main pointers
to head.
First, move reference pointer to n nodes
from head.
Now move both pointers one by one until
the reference pointer reaches the end.
Now the main pointer will point to nth
node from the end. Return the main
pointer.
Below image is a dry run of the above
approach:
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
101 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
102 embedded_system
Method 2:
Traverse linked list using two
pointers. Move one pointer by one
and other pointer by two. When
the fast pointer reaches end slow
pointer will reach middle of the
linked list.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
103
How to Detect loop in a linked list
embedded_system
Given a linked list, check if the linked list has loop or not. Below diagram
shows a linked list with a loop.
[Link]
Follow us
Press
#Be_professional_in
104 embedded_system
in a linked list
Given a linked list, check if the linked list has
loop or not. Below diagram shows a linked
list with a loop.
Floyd’s Cycle-Finding Algorithm: This is the
fastest method and has been described
below:
Traverse linked list using two pointers.
Press
here
#LEARN_IN DEPTH
#Be_professional_in
105
Reverse a linked list
embedded_system
Given pointer to the head node of a linked list, the task is to reverse the linked
list. We need to reverse the list by changing links between nodes.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
106
Reverse a linked list
embedded_system
Given pointer to the head node of a linked list, the task is to reverse the linked
list. We need to reverse the list by changing links between nodes.
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
107
References
embedded_system
[Link]
[Link]
std::printf, std::fprintf, std::sprintf, std::snprintf…..
C Programming for Engineers, Dr. Mohamed Sobh
C programming expert.
[Link]/c-programming
C programming Interview questions and answers
C – Preprocessor directives
[Link]
[Link]
[Link]
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
108 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
109 embedded_system
[Link]