Unit 4pps2

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

UNIT-4

What is Data Structure?


A data structure is a particular way of organising data in a computer so that it can be used effectively.
The idea is to reduce the space and time complexities of different tasks.
The choice of a good data structure makes it possible to perform a variety of critical operations effectively. An efficient
data structure also uses minimum memory space and execution time to process the structure. A data structure is not only
used for organising the data. It is also used for processing, retrieving, and storing data.
Need Of Data Structure:
The structure of the data and the synthesis of the algorithm are relative to each other. Data presentation must be easy to
understand so the developer, as well as the user, can make an efficient implementation of the operation.
Data structures provide an easy way of organising, retrieving, managing, and storing data.
Here is a list of the needs for data.
 Data structure modification is easy.
 It requires less time.
 Save storage memory space.
 Data representation is easy.
 Easy access to the large database
Classification/Types of Data Structures:
1. Linear Data Structure
2. Non-Linear Data Structure.

Linear Data Structure:

 Elements are arranged in one dimension ,also known as linear dimension.


 Example: lists, stack, queue, etc.

Non-Linear Data Structure

 Elements are arranged in one-many, many-one and many-many dimensions.


 Example: tree, graph, table, etc.

Most Popular Data Structures:


1. Array:
An array is a collection of data items stored at contiguous memory locations. The idea is to store multiple items of the
same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base
value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
2. Linked Lists:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location; the
elements are linked using pointers. Stack is a linear data structure that follows a particular order in which the operations are performed. The
order is LIFO(Last in first out) . Entering and retrieving data is possible from only one end. The entering and retrieving of data is also called
push and pop operation in a stack. There are different operations possible in a stack like reversing a stack using recursion, Sorting, Deleting
the middle element of a stack, etc.

3. Stack:
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In
First Out) or FILO(First In Last Out). In stack, all insertion and deletion are permitted at only one end of the list.

Stack Operations:
 push(): When this operation is performed, an element is inserted into the stack.
 pop(): When this operation is performed, an element is removed from the top of the stack and is returned.
 top(): This operation will return the last inserted element that is at the top without removing it.
 size(): This operation will return the size of the stack i.e. the total number of elements present in the stack.
 isEmpty(): This operation indicates whether the stack is empty or not.

Characteristics of a Stack:

Stack has various different characteristics which are as follows:


 Stack is used in many different algorithms like Tower of Hanoi, tree traversal, recursion, etc.
 Stack is implemented through an array or linked list.
 It follows the Last In First Out operation i.e., an element that is inserted first will pop in last and vice versa.
 The insertion and deletion are performed at one end i.e. from the top of the stack.
 In stack, if the allocated space for the stack is full, and still anyone attempts to add more elements, it will lead to stack overflow.

Applications of Stack:

Different applications of Stack are as follows:


 The stack data structure is used in the evaluation and conversion of arithmetic expressions.
 It is used for parenthesis checking.
 While reversing a string, the stack is used as well.
 Stack is used in memory management.
 It is also used for processing function calls.
 The stack is used to convert expressions from infix to postfix.
 The stack is used to perform undo as well as redo operations in word processors.
 The stack is used in virtual machines like JVM.
 The stack is used in the media players. Useful to play the next and previous song.
 The stack is used in recursion operations.

Operation performed on stack ;

A stack is a linear data structure that implements the Last-In-First-Out (LIFO) principle. Here are some common operations performed
on stacks:
 Push: Elements can be pushed onto the top of the stack, adding a new element to the top of the stack.
 Pop: The top element can be removed from the stack by performing a pop operation, effectively removing the last element that was
pushed onto the stack.
 Peek: The top element can be inspected without removing it from the stack using a peek operation.
 IsEmpty: A check can be made to determine if the stack is empty.
 Size: The number of elements in the stack can be determined using a size operation.
These are some of the most common operations performed on stacks. The specific operations and algorithms used may vary based on
the requirements of the problem and the programming language used. Stacks are commonly used in applications such as evaluating
expressions, implementing function call stacks in computer programs, and many others.

Real-Life Applications of Stack:

 Real life example of a stack is the layer of eating plates arranged one above the other. When you remove a plate from the pile, you
can take the plate to the top of the pile. But this is exactly the plate that was added most recently to the pile. If you want the plate at
the bottom of the pile, you must remove all the plates on top of it to reach it.
 Browsers use stack data structures to keep track of previously visited sites.
 Call log in mobile also uses stack data structure.

4. Queue:
Like Stack, Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In
First Out (FIFO). In the queue, items are inserted at one end and deleted from the other end. A good example of the queue is any queue
of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in
removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Queue is a linear data structure that follows a particular order in which the operations are performed. The order is First In First Out(FIFO) i.e.
the data item stored first will be accessed first. In this, entering and retrieving data is not done from only one end. An example of a queue is
any queue of consumers for a resource where the consumer that came first is served first. Different operations are performed on a Queue like
Reversing a Queue (with or without using recursion), Reversing the first K elements of a Queue, etc. A few basic operations performed In
Queue are enqueue, dequeue, front, rear, etc.

Queue Operations:
 Enqueue(): Adds (or stores) an element to the end of the queue..
 Dequeue(): Removal of elements from the queue.
 Peek() or front(): Acquires the data element available at the front node of the queue without deleting it.
 rear(): This operation returns the element at the rear end without removing it.
 isFull(): Validates if the queue is full.
 isNull(): Checks if the queue is empty.

Characteristics of a Queue:
The queue has various different characteristics which are as follows:
 The queue is a FIFO (First In First Out) structure.
 To remove the last element of the Queue, all the elements inserted before the new element in the queue must be removed.
 A queue is an ordered list of elements of similar data types.

Applications of Queue:

Different applications of Queue are as follows:


 Queue is used for handling website traffic.
 It helps to maintain the playlist in media players.
 Queue is used in operating systems for handling interrupts.
 It helps in serving requests on a single shared resource, like a printer, CPU task scheduling, etc.
 It is used in the asynchronous transfer of data e.g. pipes, file IO, and sockets.
 Queues are used for job scheduling in the operating system.
 In social media to upload multiple photos or videos queue is used.
 To send an e-mail queue data structure is used.
 To handle website traffic at a time queues are used.
 In Windows operating system, to switch multiple applications.

Operation performed on queue:

A queue is a linear data structure that implements the First-In-First-Out (FIFO) principle. Here are some common operations performed on
queues:
 Enqueue: Elements can be added to the back of the queue, adding a new element to the end of the queue.
 Dequeue: The front element can be removed from the queue by performing a dequeue operation, effectively removing the first element
that was added to the queue.
 Peek: The front element can be inspected without removing it from the queue using a peek operation.
 IsEmpty: A check can be made to determine if the queue is empty.
 Size: The number of elements in the queue can be determined using a size operation.

These are some of the most common operations performed on queues. The specific operations and algorithms used may vary based on the
requirements of the problem and the programming language used. Queues are commonly used in applications such as scheduling tasks,
managing communication between processes, and many others.

Real-Life Applications of Queue:

 A real-world example of a queue is a single-lane one-way road, where the vehicle that enters first will exit first.
 A more real-world example can be seen in the queue at the ticket windows.
 A cashier line in a store is also an example of a queue.
 People on an escalator

Queue using Arrays:


1. #include<stdio.h>
2. #include<stdlib.h>
3. #define maxsize 5
4. void insert();
5. void delete();
6. void display();
7. int front = -1, rear = -1;
8. int queue[maxsize];
9. void main ()
10. {
11. int choice;
12. while(choice != 4)
13. {
14. printf("\n*************************Main Menu*****************************\n");
15. printf("\n============================================\n");
16. printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
17. printf("\nEnter your choice ?");
18. scanf("%d",&choice);
19. switch(choice)
20. {
21. case 1: insert();
22. break;
23. case 2: delete();
24. break;
25. case 3: display();
26. break;
27. case 4: exit(0);
28. default: printf("\nEnter valid choice??\n");
29. }
30. }
31. }
32. void insert()
33. {
34. int item;
35. printf("\nEnter the element\n");
36. scanf("\n%d",&item);
37. if(rear == maxsize-1)
38. {
39. printf("\nOVERFLOW\n");
40. }
41. if(front == -1 && rear == -1)
42. {
43. front = 0;
44. rear = 0;
45. }
46. else
47. {
48. rear = rear+1;
49. }
50. queue[rear] = item;
51. printf("\nValue inserted ");
52. }
53. void delete()
54. {
55. int item;
56. if (front == -1 || front > rear)
57. printf("\nUNDERFLOW\n");
58. else
59. {
60. item = queue[front];
61. if(front == rear)
62. {
63. front = -1;
64. rear = -1 ;
65. }
66. else
67. {
68. front = front + 1;
69. }
70. printf("\nvalue deleted ");
71. }
72. }
73. void display()
74. {
75. int i;
76. if(rear == -1)
77. printf("\nEmpty queue\n");
78. else
79. { printf("\nprinting values .....\n");
80. for(i=front;i<=rear;i++)
81. printf("\n%d\n",queue[i]);
82. }
83. }

Stack using Arrays--Given to your notebook in the class

You might also like