SlideShare a Scribd company logo
2
Most read
7
Most read
8
Most read
ADVANCED DATA
STRUCTURES
Trees
Heaps
Hash tables
Graphs
Arrays
Linked Lists
Stacks
Queues
2
Introduction to Arrays
An array is a collection of items of the same variable type
that are stored at contiguous memory locations.
Basic Terminologies of Array
Array Index:
In an array, elements are identified by
their indexes. Array index starts from 0.
Array element:
Elements are items stored in an array
and can be accessed by their index.
Array Length:
The length of an array is determined by
the number of elements it can contain.
Data Structure and Algorithm Lesson 2.pptx
5
Linked Lists
A linked list is a linear data structure
consisting of a sequence of
elements called nodes, where each
node contains both the data and a
reference (or pointer) to the next
node in the sequence.
6
Types of Linked Lists
There are several types of linked lists, but the
two most common ones are:
1.Singly Linked List: In this type of linked list,
each node contains data and a pointer to the
next node in the sequence. The last node points
to null, indicating the end of the list.
2.Doubly Linked List: Each node in a doubly
linked list contains data, a pointer to the next
node, and a pointer to the previous node. This
allows for traversal in both directions.
OPERATIONS ON LINKED
LISTS:
7
1.Insertion: Adding a new node to
the list.
2.Deletion: Removing a node from
the list.
3.Traversal: Iterating through the list
to access or manipulate its elements.
4.Search: Finding a specific element
within the list.
5.Concatenation: Combining two
linked lists into one.
Insertion:
Example: Suppose we have a singly linked list with nodes
containing integers. To insert a new node with the value 10 at the
end of the list:
Original Linked List: 1 -> 2 -> 3 -> NULL
After Insertion: 1 -> 2 -> 3 -> 10 -> NULL
Deletion:
•Example: Continuing from the previous example, if we want to
remove the node containing the value 2 from the list:
Original Linked List: 1 -> 2 -> 3 -> 10 -> NULL
After Deletion: 1 -> 3 -> 10 -> NULL
Traversal:
Example: Suppose we have the following singly linked list with
characters representing a string. Traversing the list would mean
iterating through each node and printing its value:
Linked List: 'H' -> 'e' -> 'l' -> 'l' -> 'o' -> NULL
Traversal Output: Hello
Search:
Example: Consider the same linked list from the traversal example.
If we want to search for the character 'l' in the list:
Linked List: 'H' -> 'e' -> 'l' -> 'l' -> 'o' -> NULL
Search for 'l': Found at index 2 and 3
Concatenation:
Example: Suppose we have two singly linked lists, list1 and list2,
with the following elements:
list1: 1 -> 2 -> 3 -> NULL
list2: 4 -> 5 -> 6 -> NULL
Concatenating list2 to the end of list1 would result in:
Concatenated List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
13
Applications of Linked Lists
•Dynamic Memory Allocation: Linked lists are
commonly used in dynamic memory allocation systems,
such as malloc() and free() in C.
•Implementation of Stacks and Queues: Linked lists
serve as the underlying data structure for implementing
stacks (using singly linked lists) and queues (using either
singly or doubly linked lists).
•Polynomial Manipulation: Linked lists can be used to
represent and manipulate polynomials in mathematical
computations.
•Sparse Matrix Representation: Linked lists are
suitable for representing sparse matrices efficiently.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed!n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
struct Node* head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);
printf("Linked List: ");
for (struct Node* current = head; current != NULL; current = current->next)
printf("%d -> ", current->data);
printf("NULLn");
for (struct Node* current = head; current != NULL;) {
struct Node* temp = current;
current = current->next;
free(temp);
}
return 0;
}
16
Stacks:
•Definition and properties of stacks (Last In First Out -
LIFO).
•Implementation of stacks using arrays or linked lists.
•Operations on stacks (push, pop, peek).
•Applications of stacks in expression evaluation,
function calls, and backtracking algorithms.
17
Definition and Properties of Stacks
(Last In First Out - LIFO):
•A stack is a linear data structure that follows
the Last In First Out (LIFO) principle, meaning
that the last element added to the stack is the
first one to be removed.
•Stacks can be visualized as a collection of
elements arranged in a specific order where
elements are added and removed from the top
(the end where insertion and deletion occur).
18
Implementation of Stacks using Arrays
or Linked Lists:
•Stacks can be implemented using either arrays
or linked lists.
•Array-based Implementation: In this approach,
a fixed-size array is used to store stack elements.
The top of the stack is represented by a variable
that points to the index of the top element in the
array.
•Linked List-based Implementation: In this
approach, a linked list is used to represent the
stack.
19
Operations on Stacks (push, pop, peek):
•Push: Adds an element to the top of the stack.
•Pop: Removes the element from the top of the
stack.
•Peek: Returns the element at the top of the
stack without removing it.
20
Applications of Stacks
•Expression Evaluation
•Function Calls
•Backtracking Algorithms

More Related Content

PPTX
Data_structure.pptx
PPT
Data Structures 3
PPT
Data Structures and algorithms using c .ppt
PPT
DS Unit 2.ppt
PPT
LINKEDb2bb22bb3b3b3b3n3_LIST_UKL_1-2.ppt
PPTX
Data Structures Introduction & Linear DS
PPTX
DS_LinkedList.pptx
PPTX
Lecture 2 - Linear Data Structures & Implementation.pptx
Data_structure.pptx
Data Structures 3
Data Structures and algorithms using c .ppt
DS Unit 2.ppt
LINKEDb2bb22bb3b3b3b3n3_LIST_UKL_1-2.ppt
Data Structures Introduction & Linear DS
DS_LinkedList.pptx
Lecture 2 - Linear Data Structures & Implementation.pptx

Similar to Data Structure and Algorithm Lesson 2.pptx (20)

PPT
Data structures
PPTX
Linked list
PPTX
Revisiting a data structures in detail with linked list stack and queue
PDF
Document on Linked List as a presentation
PDF
Document on Linked List as a presentation
PPTX
unit 1.pptx
PDF
DS Module 03.pdf
PPT
lecture 02.2.ppt
PPTX
Data Structure
PPTX
Linked list
PPTX
Data structure
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
PPT
Linked list1.ppt
PPTX
VCE Unit 02 (1).pptx
PPTX
Data Structures(Part 1)
PPTX
DS-3asdfghjklxmmcnaefiuhavbifuhablc.pptx
PPTX
Stack and queue power point presentation data structure and algorithms Stack-...
PPTX
Linked List in Data Structure
PPTX
linkedlistforslideshare-210123143943.pptx
PPT
46267037-Data-Structures-PPT.ppt
Data structures
Linked list
Revisiting a data structures in detail with linked list stack and queue
Document on Linked List as a presentation
Document on Linked List as a presentation
unit 1.pptx
DS Module 03.pdf
lecture 02.2.ppt
Data Structure
Linked list
Data structure
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
Linked list1.ppt
VCE Unit 02 (1).pptx
Data Structures(Part 1)
DS-3asdfghjklxmmcnaefiuhavbifuhablc.pptx
Stack and queue power point presentation data structure and algorithms Stack-...
Linked List in Data Structure
linkedlistforslideshare-210123143943.pptx
46267037-Data-Structures-PPT.ppt
Ad

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Pharma ospi slides which help in ospi learning
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Complications of Minimal Access Surgery at WLH
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
master seminar digital applications in india
PPTX
Cell Types and Its function , kingdom of life
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose
PPH.pptx obstetrics and gynecology in nursing
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pharma ospi slides which help in ospi learning
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial disease of the cardiovascular and lymphatic systems
Complications of Minimal Access Surgery at WLH
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Supply Chain Operations Speaking Notes -ICLT Program
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
01-Introduction-to-Information-Management.pdf
master seminar digital applications in india
Cell Types and Its function , kingdom of life
Ad

Data Structure and Algorithm Lesson 2.pptx

  • 2. 2 Introduction to Arrays An array is a collection of items of the same variable type that are stored at contiguous memory locations.
  • 3. Basic Terminologies of Array Array Index: In an array, elements are identified by their indexes. Array index starts from 0. Array element: Elements are items stored in an array and can be accessed by their index. Array Length: The length of an array is determined by the number of elements it can contain.
  • 5. 5 Linked Lists A linked list is a linear data structure consisting of a sequence of elements called nodes, where each node contains both the data and a reference (or pointer) to the next node in the sequence.
  • 6. 6 Types of Linked Lists There are several types of linked lists, but the two most common ones are: 1.Singly Linked List: In this type of linked list, each node contains data and a pointer to the next node in the sequence. The last node points to null, indicating the end of the list. 2.Doubly Linked List: Each node in a doubly linked list contains data, a pointer to the next node, and a pointer to the previous node. This allows for traversal in both directions.
  • 7. OPERATIONS ON LINKED LISTS: 7 1.Insertion: Adding a new node to the list. 2.Deletion: Removing a node from the list. 3.Traversal: Iterating through the list to access or manipulate its elements. 4.Search: Finding a specific element within the list. 5.Concatenation: Combining two linked lists into one.
  • 8. Insertion: Example: Suppose we have a singly linked list with nodes containing integers. To insert a new node with the value 10 at the end of the list: Original Linked List: 1 -> 2 -> 3 -> NULL After Insertion: 1 -> 2 -> 3 -> 10 -> NULL
  • 9. Deletion: •Example: Continuing from the previous example, if we want to remove the node containing the value 2 from the list: Original Linked List: 1 -> 2 -> 3 -> 10 -> NULL After Deletion: 1 -> 3 -> 10 -> NULL
  • 10. Traversal: Example: Suppose we have the following singly linked list with characters representing a string. Traversing the list would mean iterating through each node and printing its value: Linked List: 'H' -> 'e' -> 'l' -> 'l' -> 'o' -> NULL Traversal Output: Hello
  • 11. Search: Example: Consider the same linked list from the traversal example. If we want to search for the character 'l' in the list: Linked List: 'H' -> 'e' -> 'l' -> 'l' -> 'o' -> NULL Search for 'l': Found at index 2 and 3
  • 12. Concatenation: Example: Suppose we have two singly linked lists, list1 and list2, with the following elements: list1: 1 -> 2 -> 3 -> NULL list2: 4 -> 5 -> 6 -> NULL Concatenating list2 to the end of list1 would result in: Concatenated List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
  • 13. 13 Applications of Linked Lists •Dynamic Memory Allocation: Linked lists are commonly used in dynamic memory allocation systems, such as malloc() and free() in C. •Implementation of Stacks and Queues: Linked lists serve as the underlying data structure for implementing stacks (using singly linked lists) and queues (using either singly or doubly linked lists). •Polynomial Manipulation: Linked lists can be used to represent and manipulate polynomials in mathematical computations. •Sparse Matrix Representation: Linked lists are suitable for representing sparse matrices efficiently.
  • 14. #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* createNode(int data) { struct Node* newNode = malloc(sizeof(struct Node)); if (!newNode) { printf("Memory allocation failed!n"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; }
  • 15. int main() { struct Node* head = createNode(10); head->next = createNode(20); head->next->next = createNode(30); printf("Linked List: "); for (struct Node* current = head; current != NULL; current = current->next) printf("%d -> ", current->data); printf("NULLn"); for (struct Node* current = head; current != NULL;) { struct Node* temp = current; current = current->next; free(temp); } return 0; }
  • 16. 16 Stacks: •Definition and properties of stacks (Last In First Out - LIFO). •Implementation of stacks using arrays or linked lists. •Operations on stacks (push, pop, peek). •Applications of stacks in expression evaluation, function calls, and backtracking algorithms.
  • 17. 17 Definition and Properties of Stacks (Last In First Out - LIFO): •A stack is a linear data structure that follows the Last In First Out (LIFO) principle, meaning that the last element added to the stack is the first one to be removed. •Stacks can be visualized as a collection of elements arranged in a specific order where elements are added and removed from the top (the end where insertion and deletion occur).
  • 18. 18 Implementation of Stacks using Arrays or Linked Lists: •Stacks can be implemented using either arrays or linked lists. •Array-based Implementation: In this approach, a fixed-size array is used to store stack elements. The top of the stack is represented by a variable that points to the index of the top element in the array. •Linked List-based Implementation: In this approach, a linked list is used to represent the stack.
  • 19. 19 Operations on Stacks (push, pop, peek): •Push: Adds an element to the top of the stack. •Pop: Removes the element from the top of the stack. •Peek: Returns the element at the top of the stack without removing it.
  • 20. 20 Applications of Stacks •Expression Evaluation •Function Calls •Backtracking Algorithms

Editor's Notes

  • #3: The dream of every programmer is to become not just a good, but also a great programmer. We all want to achieve our goals and to achieve our goals, we must have a great plan with us. In this context, we have decided to provide a complete guide for Arrays interview preparation, which will help you to tackle the problems that are mostly asked in the interview, such as What is an Array, What is Array in C language, How do you initialize an Array in C, How to sort an Array, etc. We have also covered the topics such as Top Theoretical interview questions and Top interview coding questions in this complete guide for Array interview preparation. It’s one of the most popular and simple data structures and is often used to implement other data structures. Each item in an array is indexed starting with 0.
  • #7: Unlike arrays, linked lists do not have a fixed size, and their elements are not stored in contiguous memory locations. Instead, each element (node) points to the next one, forming a chain-like structure.
  • #15: In this example: We define a structure Node to represent a node in a linked list. It contains an integer data field and a pointer to the next node. The createNode() function dynamically allocates memory for a new node using malloc(). If memory allocation fails, it prints an error message and exits the program. In the main() function, we initialize an empty linked list and insert nodes dynamically using the createNode() function. After printing the elements of the linked list, we free the memory allocated for each node using the free() function to prevent memory leaks. This example demonstrates how linked lists are used in conjunction with dynamic memory allocation functions like malloc() and free() to manage memory dynamically in C programs. ``` 1. **`#include <stdio.h>` and `#include <stdlib.h>`**: - These are preprocessor directives in C. - `#include <stdio.h>` includes the standard input/output library, which provides functions like `printf()` and `scanf()`. - `#include <stdlib.h>` includes the standard library, which provides functions like memory allocation (`malloc()`, `calloc()`, `realloc()`) and memory deallocation (`free()`). 2. **`struct Node { ... };`**: - This defines a structure named `Node`. - It contains two members: `int data` (to store integer data) and `struct Node* next` (a pointer to the next node in the linked list). 3. **`struct Node* createNode(int data) { ... }`**: - This is a function named `createNode()` that returns a pointer to a newly allocated `Node` structure. - It takes an integer parameter `data`, which represents the data to be stored in the node. - Inside the function: - It allocates memory for a new `Node` structure using `malloc(sizeof(struct Node))`. If memory allocation fails (`malloc()` returns `NULL`), it prints an error message and exits the program using `exit(1)`. - It assigns the input `data` to the `data` member of the newly created node. - It initializes the `next` pointer of the node to `NULL`, indicating that it's the last node in the list. - Finally, it returns a pointer to the newly created node. Overall, this code defines a basic structure for a singly linked list node (`Node`) and a function to dynamically create new nodes (`createNode()`). The inclusion of standard libraries (`<stdio.h>` and `<stdlib.h>`) provides necessary functionality for input/output operations and memory allocation/deallocation.
  • #16: int main() { // Create the head of the linked list and initialize it with a node containing data 10 struct Node* head = createNode(10); // Create the second node with data 20 and attach it to the head node head->next = createNode(20); // Create the third node with data 30 and attach it to the second node head->next->next = createNode(30); // Print the elements of the linked list printf("Linked List: "); for (struct Node* current = head; current != NULL; current = current->next) printf("%d -> ", current->data); printf("NULL\n"); // Free memory allocated for the linked list nodes for (struct Node* current = head; current != NULL;) { // Store the current node in a temporary variable struct Node* temp = current; // Move to the next node current = current->next; // Free the memory allocated for the current node free(temp
  • #21: 4. Applications of Stacks: Expression Evaluation: Stacks are commonly used in evaluating arithmetic expressions, such as infix, postfix, and prefix expressions. Operators and operands are pushed onto the stack, and operations are performed based on their precedence and associativity. Function Calls: Stacks are used in function call mechanisms, such as maintaining the call stack in recursive function calls. When a function is called, its local variables and the return address are pushed onto the stack, and they are popped off when the function returns. Backtracking Algorithms: Stacks are utilized in backtracking algorithms, such as depth-first search (DFS), to store the state of exploration. During backtracking, the algorithm pops elements from the stack to backtrack to previous states and explore other paths.