0% found this document useful (0 votes)
3 views

Data Structure BooK

Uploaded by

Abdelrahman Aref
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structure BooK

Uploaded by

Abdelrahman Aref
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

A

Data Structure in C
Eng. Mohamed Yousef
(Slides Book)

Egypt, 2020

The link of data structure in C course on my youtube channel is:

https://www.youtube.com/playlist?list=PLfgCIULRQavxpi-GYpkLt8_sFb2VlT6Zo

Follow me on:

https://www.youtube.com/mohamedyousef2

https://electronics010.blogspot.com/

https://www.facebook.com/electronics010
B

Contents:
1) Introduction
2) Array-based Stack implementation
3) Array-based Queue implementation
4) Linked List Implementation
Basic Data Structures

Eng. Mohamed Yousef

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

What is data structure?

Data Structure is the way of storing and access data from memory so that data can be used efficiently.

Actually in our programming data stored in main memory(RAM) and To develop efficient software or firmware
we need to care about memory.
To efficiently manage we required data structure.
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Types of Data Structure

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Types of Data Structure


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Types of Data Structure

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Types of Data Structure

Tree data structure


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Following terms are the foundation terms of a data structure.


Basic Data Structures

Eng. Mohamed Yousef

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack

Think of a stack as analogous to a pile of dishes.


When a dish is placed top (referred to
as the dish onto the stack).
Similarly, when a dish is removed
the top (referred to as the dish off the stack).
Stacks are known as last-in, first-out ( ) data structures the last item
pushed (inserted) on the stack is the first item popped (removed) from the
stack.
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

What is Stack?

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Implementation
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack using array.

How stack works


The operations work as follows:
1 ) A pointer called TOP is used to keep track of the top element in the stack.
2 ) When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1 .
3 ) On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP.
4 ) On popping an element, we return the element pointed to by TOP and reduce its value.
5 ) Before pushing, we check if stack is already full
6 ) Before popping, we check if stack is already empty

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack using Array

Stack can be implemented using one-dimensional array.


One-dimensional array is used to hold elements of a stack.
Implementing a stack using array can store fixed number of data values.
In a stack, initially top is set to -1 .
Top is used to keep track of the index of the top most element.

Following table shows the Position of Top which indicates the status of stack:

(Underflow)
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack using Array

Stack can be defined using array as follows:

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Specification

A stack is data structure that allows the following operations:


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Use of stack

The most common uses of a stack are:


To reverse a word
Put all the letters in a stack and pop them out.
Because of LIFO order of stack, you will get the letters in reverse order.

In compilers
Compilers use stack to calculate the value of expressions like 2 +4 /5 *(7 -9 ).

In browsers
The back button in a browser saves all the urls you have visited previously in a stack. Each time you visit a new
page, it is added on top of the stack. When you press the back button, the current URL is removed from the
stack and the previous url is accessed.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack declaration.
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack function prototypes.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Implementation.

Initialization of stack.
TOP points to the top-most element of stack.
1 ) TOP: = -1 ;
2 ) Exit
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Implementation.

Stack is full.
1 ) IF TOP = MAX - 1 then
return:=1 ;
1 ) Otherwise
return:=0 ;
1 ) End of IF
2 ) Exit

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Implementation.

Stack is empty.
1 ) IF TOP = - 1 then
return:=1 ;
2 ) Otherwise
return:=0 ;
3 ) End of IF
4 ) Exit
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Implementation.

Push an item into stack.


1) IF TOP = MAX - 1 then return 0;
Exit;
2) Otherwise
TOP: = TOP + 1;
STACK (TOP):= ITEM;
3) End of IF
1) Exit

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Implementation.

Pop an element from stack.


1) IF TOP = - 1 then return 0;
Exit;
2) Otherwise
ITEM: =STACK (TOP);
TOP: = TOP - 1;
3) End of IF
1) Exit
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Stack Implementation.
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include <stdio.h>
12 #include "lib/stack.h"
13
14 //
===========================================================
================
15 int main(){
16 Stack stack;
17 Stack *ptr_stack = &stack;
18
19 DATA_TYPE element;
20 DATA_TYPE *ptr_element = &element;
21
22 int x = 0, temp = 0;
23
24 Init(ptr_stack);
25
26 // ----------------------------------------------------
27
28 Push('S', ptr_stack);
29 Push('y', ptr_stack);
30 Push('s', ptr_stack);
31 Push('t', ptr_stack);
32 Push('e', ptr_stack);
33 Push('m', ptr_stack);
34
35 // ----------------------------------------------------
36
37 if(IsFull(ptr_stack)){
38
39 printf("\n Stack is full.");
40
41 } else if(IsEmpty(ptr_stack)){
42
43 printf("\n Stack is empty.");
44
45 } else {
46
47 printf("\n Stack size: %d", StackSize(ptr_stack));
48 }
49
50 printf("\n ---------------");
51
52 // ----------------------------------------------------
53
54 temp = ptr_stack->top;
55
56 for(x = 0; x <= temp; ++x){
57 Pop(ptr_element, ptr_stack);
58 printf("\n Element: %c", *ptr_element);
59 printf("\n Size: %d", StackSize(ptr_stack));
60 printf("\n ---------------");
61 }
62
63 // ----------------------------------------------------
64
65 return 0;
66 }
67 //
===========================================================
================
68
69
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #ifndef STACK_H_INCLUDED
12 #define STACK_H_INCLUDED
13 // ----------------------------------------------------
14
15 #define MAX_SIZE 10
16 #define DATA_TYPE char
17
18 typedef struct{
19 DATA_TYPE elements[MAX_SIZE];
20 int top;
21 }Stack;
22
23 void Init(Stack *);
24 int IsFull(Stack *);
25 int IsEmpty(Stack *);
26 int Push(DATA_TYPE, Stack *);
27 int Pop(DATA_TYPE *, Stack *);
28 int StackSize(Stack *);
29 void ClearStack(Stack *);
30
31 // ----------------------------------------------------
32 #endif // STACK_H_INCLUDED
33
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include "stack.h"
12
13 void Init(Stack *ptr_stack){
14
15 ptr_stack->top = -1;
16 }
17 //
===========================================================
================
18 int IsFull(Stack *ptr_stack){
19
20 if(ptr_stack->top == MAX_SIZE - 1){
21 return 1;
22 } else {
23 return 0;
24 }
25 }
26 //
===========================================================
================
27 int IsEmpty(Stack *ptr_stack){
28
29 if(ptr_stack->top == -1){
30 return 1;
31 } else {
32 return 0;
33 }
34 }
35 //
===========================================================
================
36 int Push(DATA_TYPE element, Stack *ptr_stack){
37
38 if(ptr_stack->top == MAX_SIZE - 1){ // Is stack full?
39 return 0;
40 } else {
41 ptr_stack->top++;
42 ptr_stack->elements[ptr_stack->top] = element;
43 return 1;
44 }
45 }
46 //
===========================================================
================
47 int Pop(DATA_TYPE *ptr_element, Stack *ptr_stack){
48
49 if(ptr_stack->top == -1){ // Is stack empty?
50 return 0;
51 } else {
52 *ptr_element = ptr_stack->elements[ptr_stack->top];
53 ptr_stack->top--;
54 return 1;
55 }
56 }
57 //
===========================================================
================
58 int StackSize(Stack *ptr_stack){
59
60 if(ptr_stack->top == -1){ // Is stack empty?
61 return 0;
62 } else {
63 return (ptr_stack->top + 1);
64 }
65 }
66 //
===========================================================
================
67 void ClearStack(Stack *ptr_stack){
68 ptr_stack->top = -1;
69 }
70 //
===========================================================
================
71
72
73
74
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include <stdio.h>
12 #include <string.h>
13
14 #include "lib/stack.h"
15
16 //
===========================================================
================
17 int main(){
18 Stack stack;
19 Stack *ptr_stack = &stack;
20
21 char *text = "12345";
22 char inverse[6] = {'\0'};
23
24 int x = 0, temp = 0;
25
26 Init(ptr_stack);
27
28 // ----------------------------------------------------
29
30 for(x = 0; x < strlen(text); ++x){
31 Push(*(text + x), ptr_stack);
32 }
33
34 // ----------------------------------------------------
35
36 temp = ptr_stack->top;
37
38 for(x = 0; x <= temp; ++x){
39 Pop(&inverse[x], ptr_stack);
40 }
41
42 printf("\n Text: %s", inverse);
43
44 // ----------------------------------------------------
45
46 return 0;
47 }
48 //
===========================================================
================
49
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #ifndef STACK_H_INCLUDED
12 #define STACK_H_INCLUDED
13 // ----------------------------------------------------
14
15 #define MAX_SIZE 10
16 #define DATA_TYPE char
17
18 typedef struct{
19 DATA_TYPE elements[MAX_SIZE];
20 int top;
21 }Stack;
22
23 void Init(Stack *);
24 int IsFull(Stack *);
25 int IsEmpty(Stack *);
26 int Push(DATA_TYPE, Stack *);
27 int Pop(DATA_TYPE *, Stack *);
28 int StackSize(Stack *);
29 void ClearStack(Stack *);
30
31 // ----------------------------------------------------
32 #endif // STACK_H_INCLUDED
33
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include "stack.h"
12
13 void Init(Stack *ptr_stack){
14
15 ptr_stack->top = -1;
16 }
17 //
===========================================================
================
18 int IsFull(Stack *ptr_stack){
19
20 if(ptr_stack->top == MAX_SIZE - 1){
21 return 1;
22 } else {
23 return 0;
24 }
25 }
26 //
===========================================================
================
27 int IsEmpty(Stack *ptr_stack){
28
29 if(ptr_stack->top == -1){
30 return 1;
31 } else {
32 return 0;
33 }
34 }
35 //
===========================================================
================
36 int Push(DATA_TYPE element, Stack *ptr_stack){
37
38 if(ptr_stack->top == MAX_SIZE - 1){ // Is stack full?
39 return 0;
40 } else {
41 ptr_stack->top++;
42 ptr_stack->elements[ptr_stack->top] = element;
43 return 1;
44 }
45 }
46 //
===========================================================
================
47 int Pop(DATA_TYPE *ptr_element, Stack *ptr_stack){
48
49 if(ptr_stack->top == -1){ // Is stack empty?
50 return 0;
51 } else {
52 *ptr_element = ptr_stack->elements[ptr_stack->top];
53 ptr_stack->top--;
54 return 1;
55 }
56 }
57 //
===========================================================
================
58 int StackSize(Stack *ptr_stack){
59
60 if(ptr_stack->top == -1){ // Is stack empty?
61 return 0;
62 } else {
63 return (ptr_stack->top + 1);
64 }
65 }
66 //
===========================================================
================
67 void ClearStack(Stack *ptr_stack){
68 ptr_stack->top = -1;
69 }
70 //
===========================================================
================
71
72
73
74
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description : Application: Determining Well-Formed
Expressions with Parenthesis
9 ===========================================================
===================
10 */
11 #include <stdio.h>
12 #include <string.h>
13
14 #include "lib/stack.h"
15
16 int IsMatchingPair(char opened_char, char closed_char);
17 int IsBalanced(char exp[]);
18 //
===========================================================
================
19 int main(){
20 char text[] = "b-{(x+y)*z+3}";
21
22 if(IsBalanced(text)){
23
24 printf("\n Balanced");
25 } else {
26 printf("\n Not Balanced");
27 }
28
29 return 0;
30 }
31 //
===========================================================
================
32 int IsMatchingPair(char opened_char, char closed_char){
33
34 if (opened_char == '(' && closed_char == ')'){
35 return 1;
36 } else if (opened_char == '{' && closed_char == '}') {
37 return 1;
38 } else if (opened_char == '[' && closed_char == ']') {
39 return 1;
40 } else {
41 return 0;
42 }
43 }
44 //
===========================================================
================
45 int IsBalanced(char exp[]){
46 Stack stack;
47 Stack *ptr_stack = &stack;
48 int x = 0;
49 char element = '\0';
50 int length = strlen(exp);
51
52 Init(ptr_stack);
53
54 // Test for each character in expression
55 for(x = 0; x < length; ++x){
56
57 //
-------------------------------------------------------
58 if( exp[x]=='(' || exp[x]=='{' || exp[x]=='['){
59
60 Push(exp[x], ptr_stack);
61
62 } else {
63
64 if( exp[x]==')' || exp[x]=='}' || exp[x]==']'
){
65
66 if(IsEmpty(ptr_stack)){ // there is no
opening part
67 return 0;
68 } else {
69 Pop(&element, ptr_stack);
70 if( !IsMatchingPair(element, exp[x]))
return 0;
71 }
72 }
73 }
74 //
-------------------------------------------------------
75 } // END for loop
76
77 // If stack is not empty, then the closing parts is
less than
78 // the opening parts.
79 if(IsEmpty(ptr_stack)){
80 return 1;
81 } else {
82 return 0;
83 }
84 }
85 //
===========================================================
================
86
87
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #ifndef STACK_H_INCLUDED
12 #define STACK_H_INCLUDED
13 // ----------------------------------------------------
14
15 #define MAX_SIZE 30
16 #define DATA_TYPE char
17
18 typedef struct{
19 DATA_TYPE elements[MAX_SIZE];
20 int top;
21 }Stack;
22
23 void Init(Stack *);
24 int IsFull(Stack *);
25 int IsEmpty(Stack *);
26 int Push(DATA_TYPE, Stack *);
27 int Pop(DATA_TYPE *, Stack *);
28 int StackSize(Stack *);
29 void ClearStack(Stack *);
30
31 // ----------------------------------------------------
32 #endif // STACK_H_INCLUDED
33
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based stack implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include "stack.h"
12
13 void Init(Stack *ptr_stack){
14
15 ptr_stack->top = -1;
16 }
17 //
===========================================================
================
18 int IsFull(Stack *ptr_stack){
19
20 if(ptr_stack->top == MAX_SIZE - 1){
21 return 1;
22 } else {
23 return 0;
24 }
25 }
26 //
===========================================================
================
27 int IsEmpty(Stack *ptr_stack){
28
29 if(ptr_stack->top == -1){
30 return 1;
31 } else {
32 return 0;
33 }
34 }
35 //
===========================================================
================
36 int Push(DATA_TYPE element, Stack *ptr_stack){
37
38 if(ptr_stack->top == MAX_SIZE - 1){ // Is stack full?
39 return 0;
40 } else {
41 ptr_stack->top++;
42 ptr_stack->elements[ptr_stack->top] = element;
43 return 1;
44 }
45 }
46 //
===========================================================
================
47 int Pop(DATA_TYPE *ptr_element, Stack *ptr_stack){
48
49 if(ptr_stack->top == -1){ // Is stack empty?
50 return 0;
51 } else {
52 *ptr_element = ptr_stack->elements[ptr_stack->top];
53 ptr_stack->top--;
54 return 1;
55 }
56 }
57 //
===========================================================
================
58 int StackSize(Stack *ptr_stack){
59
60 if(ptr_stack->top == -1){ // Is stack empty?
61 return 0;
62 } else {
63 return (ptr_stack->top + 1);
64 }
65 }
66 //
===========================================================
================
67 void ClearStack(Stack *ptr_stack){
68 ptr_stack->top = -1;
69 }
70 //
===========================================================
================
71
72
73
74
Basic Data Structures

Eng. Mohamed Yousef

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue
Front
Rear
Enqueue
Dequeue
First-In-First-Out (FIFO)
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

What is Queue?

A queue is a collection of data that are added and removed based on the first-in-first-out (FIFO) principle.
It means that the first element added to the queue will be the first one to be removed from the queue.
Front points to the beginning of the queue and Rear points to the end of the queue.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linear Queue
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linear Queue

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linear Queue
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linear Queue

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Circular Queue

size = 5
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Circular Queue

size = 5
Enqueue No. rear index

rear = (rear+ 1) % size

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Circular Queue
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Circular Queue

IF
(rear+ 1) % size = front
Then
queue = full

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Applications of Queue

Queue
in First In First Out order like .
This property of Queue makes it also useful When a resource is shared among multiple consumers.
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue declaration.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue functions prototypes.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue Implementation.
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue Implementation.
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue Implementation.
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Queue Implementation.
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based Queue implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include <stdio.h>
12 #include "lib/queue.h"
13
14 //
===========================================================
================
15 int main(){
16 Queue queue;
17 Queue *ptr_queue = &queue;
18
19 DATA_TYPE element;
20 DATA_TYPE *ptr_element = &element;
21
22 int x = 0, temp = 0;
23
24 Init(ptr_queue);
25
26 // ----------------------------------------------------
27
28 Enqueue('S', ptr_queue);
29 Enqueue('y', ptr_queue);
30 Enqueue('s', ptr_queue);
31 Enqueue('t', ptr_queue);
32 Enqueue('e', ptr_queue);
33 Enqueue('m', ptr_queue);
34
35 // ----------------------------------------------------
36
37 if(IsFull(ptr_queue)){
38
39 printf("\n Queue is full.");
40
41 } else if(IsEmpty(ptr_queue)){
42
43 printf("\n Queue is empty.");
44
45 } else {
46
47 printf("\n Queue size: %d", QueueSize(ptr_queue));
48 }
49
50 printf("\n ---------------");
51
52 // ----------------------------------------------------
53
54 temp = ptr_queue->front;
55
56 for(x = temp; x <= ptr_queue->rear; ++x){
57
58 Dequeue(ptr_element, ptr_queue);
59 printf("\n Element: %c", *ptr_element);
60 printf("\n Size: %d", QueueSize(ptr_queue));
61 printf("\n ---------------");
62 }
63
64 // ----------------------------------------------------
65
66 return 0;
67 }
68 //
===========================================================
================
69
70
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based Queue implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #ifndef QUEUE_H_INCLUDED
12 #define QUEUE_H_INCLUDED
13 // ----------------------------------------------------
14
15 #define MAX_SIZE 10
16 #define DATA_TYPE char
17
18 typedef struct{
19 DATA_TYPE elements[MAX_SIZE];
20 int front;
21 int rear;
22 }Queue;
23
24 void Init(Queue *);
25 int IsFull(Queue *);
26 int IsEmpty(Queue *);
27 int Enqueue(DATA_TYPE, Queue *);
28 int Dequeue(DATA_TYPE *, Queue *);
29 int QueueSize(Queue *);
30 void ClearQueue(Queue *);
31
32 // ----------------------------------------------------
33 #endif // STACK_H_INCLUDED
34
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based Queue implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include "queue.h"
12
13 void Init(Queue *ptr_queue){
14
15 ptr_queue->front = -1;
16 ptr_queue->rear = -1;
17 }
18 //
===========================================================
================
19 int IsFull(Queue *ptr_queue){
20
21 if((ptr_queue->rear + 1) % MAX_SIZE ==
ptr_queue->front){
22 return 1;
23 } else {
24 return 0;
25 }
26 }
27 //
===========================================================
================
28 int IsEmpty(Queue *ptr_queue){
29
30 if((ptr_queue->front == -1) && (ptr_queue->rear ==
-1)){
31 return 1;
32 } else {
33 return 0;
34 }
35 }
36 //
===========================================================
================
37 int Enqueue(DATA_TYPE element, Queue *ptr_queue){
38
39 // Is queue full?
40 if((ptr_queue->rear + 1) % MAX_SIZE ==
ptr_queue->front){
41 return 0;
42
43 // Is queue empty?
44 } else if ((ptr_queue->front == -1) &&
(ptr_queue->rear == -1)){
45 ptr_queue->front = ptr_queue->rear = 0;
46
47 } else {
48 ptr_queue->rear = (ptr_queue->rear + 1) % MAX_SIZE;
49 }
50
51 ptr_queue->elements[ptr_queue->rear] = element;
52
53 return 1;
54 }
55 //
===========================================================
================
56 int Dequeue(DATA_TYPE *ptr_element, Queue *ptr_queue){
57
58 // Is queue empty?
59 if((ptr_queue->front == -1) && (ptr_queue->rear ==
-1)){
60
61 return 0;
62 }
63
64 *ptr_element = ptr_queue->elements[ptr_queue->front];
65
66 // if one element in queue
67 if (ptr_queue->front == ptr_queue->rear){
68
69 // reset queue
70 ptr_queue->front = ptr_queue->rear = -1;
71
72 } else {
73 ptr_queue->front = (ptr_queue->front + 1) %
MAX_SIZE;
74 }
75
76 return 1;
77 }
78 //
===========================================================
================
79 int QueueSize(Queue *ptr_queue){
80
81 if((ptr_queue->front == -1) && (ptr_queue->rear ==
-1)){// Is queue empty?
82
83 return 0;
84
85 } else if(ptr_queue->front < ptr_queue->rear){
86
87 return (ptr_queue->rear - ptr_queue->front +1);
88
89 } else if(ptr_queue->rear < ptr_queue->front){
90
91 return ((MAX_SIZE - ptr_queue->front) +
(ptr_queue->rear + 1));
92
93 } else {
94
95 return 1;
96 }
97 }
98 //
===========================================================
================
99 void ClearQueue(Queue *ptr_queue){
100
101 ptr_queue->front = -1;
102 ptr_queue->rear = -1;
103 }
104 //
===========================================================
================
105
106
107
108
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based Queue implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include <stdio.h>
12 #include "lib/queue.h"
13
14 void Test_Size(Queue *);
15 //
===========================================================
================
16 int main(){
17 Queue queue;
18 Queue *ptr_queue = &queue;
19
20 DATA_TYPE element;
21 DATA_TYPE *ptr_element = &element;
22
23 int x = 0;
24
25 Init(ptr_queue);
26 Test_Size(ptr_queue); // Queue is empty
27
28 // ----------------------------------------------------
29
30 for(x=0; x < MAX_SIZE; ++x){
31
32 Enqueue(x, ptr_queue);
33 }
34
35 Test_Size(ptr_queue); // Queue is full
36
37 // ----------------------------------------------------
38
39 // remove 2 elements
40 Dequeue(ptr_element, ptr_queue);
41 Dequeue(ptr_element, ptr_queue);
42
43 Test_Size(ptr_queue);
44
45 // ----------------------------------------------------
46
47 // add 2 elements
48 Enqueue(11, ptr_queue);
49 Enqueue(12, ptr_queue);
50
51 Test_Size(ptr_queue); // Queue is full
52
53 // ----------------------------------------------------
54
55 printf("\n\n ===================================");
56 printf("\n Front\tElement\tSize after Dequeue");
57 printf("\n ===================================");
58
59 for(x = 0; x < MAX_SIZE; ++x){
60
61 printf("\n %d",ptr_queue->front);
62 Dequeue(ptr_element, ptr_queue);
63 printf("\t %d", *ptr_element);
64 printf("\t\t%d", QueueSize(ptr_queue));
65 printf("\n -----------------------------------");
66 }
67
68 // ----------------------------------------------------
69
70 return 0;
71 }
72 //
===========================================================
================
73 void Test_Size(Queue *ptr_queue){
74
75 if(IsFull(ptr_queue)){
76
77 printf("\n Queue is full.");
78
79 } else if(IsEmpty(ptr_queue)){
80
81 printf("\n Queue is empty.");
82
83 } else {
84
85 printf("\n Queue size: %d", QueueSize(ptr_queue));
86 }
87
88 printf("\n ---------------");
89 }
90 //
===========================================================
================
91
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based Queue implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #ifndef QUEUE_H_INCLUDED
12 #define QUEUE_H_INCLUDED
13 // ----------------------------------------------------
14
15 #define MAX_SIZE 5
16 #define DATA_TYPE int
17
18 typedef struct{
19 DATA_TYPE elements[MAX_SIZE];
20 int front;
21 int rear;
22 }Queue;
23
24 void Init(Queue *);
25 int IsFull(Queue *);
26 int IsEmpty(Queue *);
27 int Enqueue(DATA_TYPE, Queue *);
28 int Dequeue(DATA_TYPE *, Queue *);
29 int QueueSize(Queue *);
30 void ClearQueue(Queue *);
31
32 // ----------------------------------------------------
33 #endif // STACK_H_INCLUDED
34
1 /*
2 ===========================================================
===================
3 PROGRAM : Array-based Queue implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include "queue.h"
12
13 void Init(Queue *ptr_queue){
14
15 ptr_queue->front = -1;
16 ptr_queue->rear = -1;
17 }
18 //
===========================================================
================
19 int IsFull(Queue *ptr_queue){
20
21 if((ptr_queue->rear + 1) % MAX_SIZE ==
ptr_queue->front){
22 return 1;
23 } else {
24 return 0;
25 }
26 }
27 //
===========================================================
================
28 int IsEmpty(Queue *ptr_queue){
29
30 if((ptr_queue->front == -1) && (ptr_queue->rear ==
-1)){
31 return 1;
32 } else {
33 return 0;
34 }
35 }
36 //
===========================================================
================
37 int Enqueue(DATA_TYPE element, Queue *ptr_queue){
38
39 // Is queue full?
40 if((ptr_queue->rear + 1) % MAX_SIZE ==
ptr_queue->front){
41 return 0;
42
43 // Is queue empty?
44 } else if ((ptr_queue->front == -1) &&
(ptr_queue->rear == -1)){
45 ptr_queue->front = ptr_queue->rear = 0;
46
47 } else {
48 ptr_queue->rear = (ptr_queue->rear + 1) % MAX_SIZE;
49 }
50
51 ptr_queue->elements[ptr_queue->rear] = element;
52
53 return 1;
54 }
55 //
===========================================================
================
56 int Dequeue(DATA_TYPE *ptr_element, Queue *ptr_queue){
57
58 // Is queue empty?
59 if((ptr_queue->front == -1) && (ptr_queue->rear ==
-1)){
60
61 return 0;
62 }
63
64 *ptr_element = ptr_queue->elements[ptr_queue->front];
65
66 // if one element in queue
67 if (ptr_queue->front == ptr_queue->rear){
68
69 // reset queue
70 ptr_queue->front = ptr_queue->rear = -1;
71
72 } else {
73 ptr_queue->front = (ptr_queue->front + 1) %
MAX_SIZE;
74 }
75
76 return 1;
77
78 }
79 //
===========================================================
================
80 int QueueSize(Queue *ptr_queue){
81
82 if((ptr_queue->front == -1) && (ptr_queue->rear ==
-1)){// Is queue empty?
83
84 return 0;
85
86 } else if(ptr_queue->front < ptr_queue->rear){
87
88 return (ptr_queue->rear - ptr_queue->front +1);
89
90 } else if(ptr_queue->rear < ptr_queue->front){
91
92 return ((MAX_SIZE - ptr_queue->front) +
(ptr_queue->rear + 1));
93
94 } else {
95
96 return 1;
97 }
98
99
100 }
101 //
===========================================================
================
102 void ClearQueue(Queue *ptr_queue){
103
104 ptr_queue->front = -1;
105 ptr_queue->rear = -1;
106 }
107 //
===========================================================
================
108
109
110
111
Basic Data Structures

Eng. Mohamed Yousef

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List

It is a collection of data elements, called nodes pointing to the next node by means of a pointer.
Linked list contains data items connected together via links.
It can be visualized as a chain of nodes, where every node points to the next node.
Link field is called next and each link is linked with its next link.
Last link carries a link to null to mark the end of the list.
Entry point into the linked list is called the head of the list.
Note: Head is not a separate node but it is a reference to the first node.
If the list is empty, the head is a null reference.
While accessing a particular item, start at the head and follow the references until you get that data item.
Linked list is used while dealing with an unknown number of objects

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List

The address of the first node is always store in a reference node known as Head or Front.
Reference part of the last node must be null.
electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List

Advantages of Linked List:


Linked list is dynamic in nature which allocates the memory when required.
Insert and delete operation can be easily implemented in linked list.

Disadvantages of Linked List:


Linked list has to access each node sequentially; no element can be accessed randomly.
In linked list, the memory is wasted as pointer requires extra memory for storage.

Types of Linked List


1 . Singly Linked List
2 . Doubly Linked List
3 . Circular Linked List
4 . Doubly Circular Linked List

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List declaration


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List functions prototypes.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

IF linked list is empty


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

IF linked list is not empty

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

IF linked list is not empty


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.


electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

Linked List Implementation.

electronics010.blogspot.com Basic Data Structures Eng. Mohamed Yousef

LAB
1 /*
2 ===========================================================
===================
3 PROGRAM : Linked list implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include <stdio.h>
12 #include "lib/list.h"
13
14 //
===========================================================
================
15
16 int main(){
17 Node *ptr_head = NULL;
18 int len = 0, x = 0;
19 DATA_TYPE element = 0;
20 DATA_TYPE *ptr_element = &element;
21
22 ptr_head = Append(ptr_head, 'S');
23 Append(ptr_head, 'y');
24 Append(ptr_head, 's');
25 Append(ptr_head, 't');
26 Append(ptr_head, 'e');
27 Append(ptr_head, 'm');
28
29 //
------------------------------------------------------
30
31 len = Length(ptr_head);
32 for(x=1; x<= len; ++x){
33 Get_Element(ptr_head, x, ptr_element);
34 printf("\n %d --> %c", x, *ptr_element);
35 }
36
37 printf("\n ---------");
38
39 //
------------------------------------------------------
40
41 ptr_head = Delete_First_Node(ptr_head);
42
43 len = Length(ptr_head);
44 for(x=1; x<= len; ++x){
45 Get_Element(ptr_head, x, ptr_element);
46 printf("\n %d --> %c", x, *ptr_element);
47 }
48
49 printf("\n ---------");
50
51 //
------------------------------------------------------
52
53 Delete_Node(ptr_head, 5);
54 len = Length(ptr_head);
55 for(x=1; x<= len; ++x){
56 Get_Element(ptr_head, x, ptr_element);
57 printf("\n %d --> %c", x, *ptr_element);
58 }
59
60 printf("\n ---------");
61
62 //
------------------------------------------------------
63
64 ptr_head = Insert_Frist(ptr_head, 'A');
65 len = Length(ptr_head);
66 for(x=1; x<= len; ++x){
67 Get_Element(ptr_head, x, ptr_element);
68 printf("\n %d --> %c", x, *ptr_element);
69 }
70
71 printf("\n ---------");
72
73 //
------------------------------------------------------
74 Insert_After(ptr_head, 2, 'M');
75 len = Length(ptr_head);
76 for(x=1; x<= len; ++x){
77 Get_Element(ptr_head, x, ptr_element);
78 printf("\n %d --> %c", x, *ptr_element);
79 }
80
81 printf("\n ---------");
82
83 //
------------------------------------------------------
84
85 return 0;
86 }
87
1 /*
2 ===========================================================
===================
3 PROGRAM : Linked list implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #ifndef LIST_H_INCLUDED
12 #define LIST_H_INCLUDED
13 // ----------------------------------------------------
14
15 #define DATA_TYPE char
16
17 typedef struct node{
18 DATA_TYPE element;
19 struct node *next;
20 }Node;
21
22 // Prototypes
23 // ----------
24 Node* Append(Node *, DATA_TYPE);
25 int Length(Node *);
26 int Get_Element(Node *, int, DATA_TYPE *);
27 Node* Delete_First_Node(Node *);
28 int Delete_Node(Node *, int);
29 Node* Insert_Frist(Node *, DATA_TYPE);
30 int Insert_After(Node *, int, DATA_TYPE);
31
32 // ----------------------------------------------------
33 #endif // LIST_H_INCLUDED
34
1 /*
2 ===========================================================
===================
3 PROGRAM : Linked list implementation
4 Author : Eng. Mohamed Sayed Yousef
5 http://electronics010.blogspot.com.eg/
6 Date : November 2018
7 Version : 1.0
8 Description :
9 ===========================================================
===================
10 */
11 #include "list.h"
12 #include <stdlib.h>
13
14 //
===========================================================
================
15 Node* Append (Node *head, DATA_TYPE element){
16 Node *new_node = NULL; // Local (in stack)
17
18 // create a node
19 // reserve a block in heap
20 new_node =(Node *)malloc(sizeof(Node));
21
22 if(new_node == NULL){
23 return NULL;
24 }
25
26 // set data into a new node
27 new_node->element = element;
28 new_node->next = NULL;
29
30 // link the new node to the 'List'
31 if(head != NULL){ // linked list is not empty
32 // set address in head to 'temp'
33 // this address points to 1st node
34 Node *temp = head;
35
36 while(temp->next != NULL){
37 temp = temp->next;
38 }
39
40 temp->next = new_node;
41 }
42
43 // return address of new 'Node'
44 return new_node;
45 }
46 //
===========================================================
================
47 int Length(Node *head){
48 int len = 1;
49
50 if(head == NULL){ // linked list is empty
51 return 0;
52 }
53 // set address in head to 'temp'
54 // this address points to 1st node
55 Node *temp = head;
56
57 while(temp->next != NULL){
58
59 len++;
60 // move temp to the next node
61 temp = temp->next;
62 }
63
64 return len;
65 }
66 //
===========================================================
================
67 int Get_Element(Node *head, int node_number, DATA_TYPE
*element){
68
69 if(head == NULL){ // linked list is empty
70 return 0;
71 }
72
73 if((node_number > Length(head)) || (node_number < 1)){
74 return -1;
75 }
76 // --------------------------------------------------
77
78 // set address in head to 'temp'
79 // this address points to 1st node
80 Node *temp = head;
81 int x = 1;
82
83 while(x < node_number){
84 // move temp to the next node
85 temp = temp->next;
86 x++;
87 }
88
89 *element = temp->element;
90
91 return 1;
92 }
93 //
===========================================================
================
94 Node* Delete_First_Node(Node *head){
95
96 // linked list is empty
97 if(head == NULL){
98 return NULL;
99 }
100
101 // check List length
102 if(Length(head) == 1){
103 free(head);
104 return NULL;
105 } else { // more than one node
106 Node *second_node;
107 second_node = head->next;
108 free(head);
109 return second_node;
110 }
111 }
112 //
===========================================================
================
113 int Delete_Node(Node *head, int node_number){
114
115 // linked list is empty
116 if((head == NULL)){
117 return 0;
118 }
119
120 // Invalid node number
121 if((node_number > Length(head)) || (node_number < 2)){
122 return -1;
123 }
124 // --------------------------------------------------
125
126 // delete any node in the middle or in last
127 Node *prev = head, *current;
128 int x = 1;
129
130 while(x < node_number - 1){
131 prev = prev->next;
132 x++;
133 }
134
135 current = prev->next;
136 prev->next = current->next;
137 // current->next = NULL;
138 free(current);
139
140 return 1;
141 }
142 //
===========================================================
================
143 Node* Insert_Frist(Node *head, DATA_TYPE element){
144
145 // linked list is empty
146 if(head == NULL){
147 return NULL;
148 }
149
150 Node *first_node;
151
152 // create a node
153 // reserve a block in heap
154 first_node =(Node *)malloc(sizeof(Node));
155
156 if(first_node == NULL){
157 return head;
158 }
159
160 // set data into a new node
161 first_node->element = element;
162 first_node->next = head;
163
164 head = first_node;
165
166 return head;
167 }
168 //
===========================================================
================
169 int Insert_After(Node *head, int node_number, DATA_TYPE
element){
170
171 // linked list is empty
172 if((head == NULL)){
173 return 0;
174 }
175
176 // Invalid node number
177 if((node_number > Length(head)) || (node_number < 1)){
178 return -1;
179 }
180
181 // --------------------------------------------------
182
183 Node *prev = head, *current;
184 int x = 1;
185
186 // create a node
187 // reserve a block in heap
188 current =(Node *)malloc(sizeof(Node));
189
190 if(current == NULL){
191 return 0;
192 }
193
194 while(x < node_number){
195 prev = prev->next;
196 x++;
197 }
198
199 // set data into a new node
200 current->element = element;
201 current->next = prev->next;
202
203 prev->next = current;
204
205 return 1;
206 }
207 //
===========================================================
================
208

You might also like