Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
8 views
17 pages
1
Data structure 2
Uploaded by
gopalkr9958
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save 1 (2) For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
8 views
17 pages
1
Data structure 2
Uploaded by
gopalkr9958
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save 1 (2) For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save 1 (2) For Later
You are on page 1
/ 17
Search
Fullscreen
Ql) What is the difference between array and linked list how can you represent a linked list in money. explain the insertion and deletion operations of linked list by giving suitable example. Ans:- Difference Between Array and Linked List Representation of a Linked List A linked list is a collection of nodes, where each nede contains: |. Data: The actual value stored in the node. 2. Pointer: A reference to the next nede in the list. Example of a Node in C struct Node { int data; struct Node* next; Te >> Types of Linked Lists |. Singly Linked List; Each nede points to the next nede,2. Doubly Linked List: Each nede points to both he next and previous nodes. 3. Circular Linked List: The last nede points back o the first nede. Insertion in_a Linked Lis: Insertion can eccur_at: Beginning: eate a new node. Point the new nede to the current head. pdate the head pointer to the new node. 2. End: eate a new node. averse to the last node int the last nede to the new nede. pecitic Position: averse to the desired posi jOn. Pdare pojnte © include the new_nede. ample: Inserting a Node a e Beginning #include
#include
s Node int data; s Node* next; Insert at the beginning void insertAtBeginning(struct Node** head, in s Node* newNode = (struct Node*) malloc(sizeof(struct Node); newNede->data = data; newNode->next = *head; *head = newNode; SSS // Print the list void printlist(struct Node* head) { ___struct Nede* temp = head; ______while (temp != NULL) { _printf('%d -> "| temp->data); e = Te —>next; eeprintf("NULL\n"); in ainQ) s Node* head = NU! insertAtBeainning(&head, !0); insertAtBeginning(&head, 20); insertA+Beginning(&head, 30); printlist(head); // Output: 30 -> 20 -> 10 -> eturn_ 0; Deletion ina Linked Lis Deletion can eccur at: Bedinnind: pdate the head pointer to the second node. ee The memory o je Original head 2. End:Traverse to the second last node. Set its pointer to NULL Free the last node. 3. Specific Position: Traverse to the node before the one to be deleted. Update its pointer to skip the deleted node. Free the memory of the deleted node. Example: Deleting a Node at the Beginning #include
#include
struct Node { int data; struct Node* next 3; // Delete at the beginning void _deleteAtBeginning(struct Node** head) { if (*head == NULL) { printt'List is empty \n"); return;cS Node* temp = *head; *head = (*head)->next; ee(temp); Print the lis: void printlist(struct Nede* head sct Node* temp = head; while (temp != NU printt'%d -> " temp->data); emp = temp->next; printf(N n"): in ain) Node* head = NULL; Manually create a |i Or demonstration Node* nodel = (struc lode*) malloc(sizeof{s Node)); s Node* node2 = (strucNode*) malloc(sizeof(struct Node); nodel—->data = 10; nodel—>next = node2; node2->data = 20: node2->next = NULL; head = nedel; printlist(head); // Output: 10 -> 20 -> NULL deleteAtBeainnina(&head); printList(head); // Output: 20 -> NULL eturn_0; Q2) What is queue? What are the operations on queue? How to implement queue as linked £ Explain and alse discuss its applications ee Ans:- What is a Queue? A queue is a linear data structure that follows e FIFO (First In, First Out) principle, meaninghat the first element added +o the queue will be the first one to be removed. I+ is analogous © a real-life queue, like a line at a ticket counte Basic Operations on a Queue | Enq jeue: Adds an element to the rear (end) of the queue. ample: Adding a customer to the back of a ine. 2. Dequeue: Removes an element from the front of the queue. Example: Serving the first customer in the line. 3. Peek/Front: Retrieves the element at the front of the queue without removing. i Example: Checking whe is next in line. p ec] i e eue iS em a _S. IsFull (for fixed-size queues): Checks if the queue is full.Implementation of a Queue Using a Linked List >> A linked list-based implementation of a queue consists of: ** A Node structure containing data and a pointer to the next node. | front - Points to the first node (head) of the linked _list. 2. rear - Points te the last nede (tail) of the __Steps to Implement a Queue as a Linked List |, Enqueue Operation: Create a new node with the given value. Tf the queue is empty, set both front and rear __to the new nede. Otherwise, add the new nede to the end and update the rear pointer. 2 Dequeue Operation: Check if the queue is oe Tf itis, return anerror. Otherwise, remove the nede pointed to by front _______and update the front pointer. 3. Peek Operation: Return the data of the nede pointed to by frent. plementation of a Queue Using Linked List #include
Hinclude
Define the s e of a Node Ss Node int_data; Node* ne ct Node* front: ict Node* rear;// Function to create a new nede struct Node* createNode(int data) { ___struct Nede* newNode = (struct Node*) malloc(sizeof(struct Node); ne Node->da a = data; newNode->next = NULL; eturn_newNode; nction tO create a queue s Queue™ createQueue() { struct Queue* q = (s Queue™) malloc(sizeof(struct Queue)); q->frent = q->rear = NULL; nqueue cperation * jd enqueue A Queue sct Node* newNode = createNede(data); q->rear ==q->fron = q->rear = newNode, eturn; q->rear->next = newNode; q->rear = newNode; Dequeue operation act Q eue* q = NU orintf('Queue is eme \n"); n_-l; esd.) — oto, int data = temp->data: g—>front = q->front—>next: q— q->'Peek operation int_peek(s Queue” q Pacer een ie printf{('Queue is empty!\n"); € n -l; return q->front->data; Print the queue void printQueue(struct Queue” q s Node* a q->frent; ile (temp != Hl printf("%d -> ", temp->data); emp = temp->next; printf('NULL\n"); // Main function to demonstrate the queue _int mainO { struct Queue™ q = createQueue();enqueue(q, !0); cenqueve(y 20); ______enqueue(q, 30); _____printf("'Queue after enqueues:."); PrintQueuelq; printf("Dequeued: %d\n", dequeue(q)); printf("Queue after dequeue: "); PrintQueuelq; printt'Front element: %d\n", peek(q));until the printer is ready to process the 3. Data Streaming eaming services use queues TO Na incoming data packets. 4. Breadth-Firs+ Search (BFS) BFS in graphs uses a queue to explore nodes evel by Jevel. S. Customer Service Systems Used to manage customer requests, such as in call centers. Example Application: BFS Usi eue #include
#include
#define MAX 100 struct Queue { _int items[MAX]; int front, rear; }struct Queue* createQueue() { struct Queue™ q = (struct Queue”) malloc(sizeof(struct Queue); q->front = -|; qcerear = -l; return q } int isEmpty(struct Queue* q) { return q->front == -I; } void enqueue(struct Queue* q int value) { if (q->rear == MAX - I) { printf('Queue Overflow\n'); return; } if (q->front == -I) q->frent = 0; qz>reart++; qczitems[q->rear] = value;int dequeue(struct Queue* isEmpty(q printf("Queue Underflow\n"); e n-l; int item = q->itemslq->fro if (q->front >= q->rear, q->front = q->rear = -I; e q->front++; return item; sa eue™ q = createQueuel); enqueue(q,_!); enqueue(y 2); ____enqueue(y, 3); printf Dequened: % d\n dequeue(q)); equeued: %: equeue(q)); return 0; 0
You might also like
Lectures Queue
PDF
No ratings yet
Lectures Queue
56 pages
DS Chapter 3 Stacks - Queues and Recursion-Part Iii
PDF
No ratings yet
DS Chapter 3 Stacks - Queues and Recursion-Part Iii
67 pages
DS Unit 1 Ece
PDF
No ratings yet
DS Unit 1 Ece
52 pages
Module 3 Queues
PDF
No ratings yet
Module 3 Queues
27 pages
Doubly Linked List
PDF
No ratings yet
Doubly Linked List
26 pages
Queue
PDF
No ratings yet
Queue
6 pages
L05B - Queues - Updated Slides
PDF
No ratings yet
L05B - Queues - Updated Slides
40 pages
E-Notes 1216 Content SolutionDocument 20240924025300PM
PDF
No ratings yet
E-Notes 1216 Content SolutionDocument 20240924025300PM
35 pages
Unit 4 Queues
PDF
No ratings yet
Unit 4 Queues
29 pages
Unit Iii - Queue Adt
PDF
No ratings yet
Unit Iii - Queue Adt
30 pages
W9L2 Linked Queue
PDF
No ratings yet
W9L2 Linked Queue
26 pages
Queue Data Structure
PDF
No ratings yet
Queue Data Structure
36 pages
10 Chapter-V (CSR) - 062bd70ed99f739.07176228
PDF
No ratings yet
10 Chapter-V (CSR) - 062bd70ed99f739.07176228
14 pages
Module 3 LINKED LIST-1
PDF
No ratings yet
Module 3 LINKED LIST-1
33 pages
3.3queue Using Linked List
PDF
No ratings yet
3.3queue Using Linked List
24 pages
18-Linked List1
PDF
No ratings yet
18-Linked List1
21 pages
Queue DS
PDF
No ratings yet
Queue DS
29 pages
ds-2 Removed
PDF
No ratings yet
ds-2 Removed
19 pages
CLL
PDF
No ratings yet
CLL
20 pages
RE - Lab Program 1 To 4
PDF
No ratings yet
RE - Lab Program 1 To 4
7 pages
7-Queue and Linked List-03-06-2024
PDF
No ratings yet
7-Queue and Linked List-03-06-2024
48 pages
2.4 Queue Uing Array & Linked List.
PDF
No ratings yet
2.4 Queue Uing Array & Linked List.
28 pages
280 - DS Complete-2
PDF
No ratings yet
280 - DS Complete-2
24 pages
Queues
PDF
No ratings yet
Queues
18 pages
Assignment - DADS303 - MBA 3 - Set 1 and 2
PDF
No ratings yet
Assignment - DADS303 - MBA 3 - Set 1 and 2
9 pages
Unit 5 DSU
PDF
No ratings yet
Unit 5 DSU
16 pages
Queue ADT What Is A Queue?: Out) Principle
PDF
No ratings yet
Queue ADT What Is A Queue?: Out) Principle
21 pages
Module-2 DS 2024
PDF
No ratings yet
Module-2 DS 2024
52 pages
Queue Using Linked List
PDF
No ratings yet
Queue Using Linked List
5 pages
Ds - Question Bank Solutions-Output
PDF
No ratings yet
Ds - Question Bank Solutions-Output
37 pages
DSU Microproject
PDF
No ratings yet
DSU Microproject
19 pages
DS SE III QP 24-25 Sol
PDF
No ratings yet
DS SE III QP 24-25 Sol
9 pages
Queue
PDF
No ratings yet
Queue
33 pages
5) Implementation of Queue
PDF
No ratings yet
5) Implementation of Queue
8 pages
2.2 Queue Uing Array & Linked List
PDF
No ratings yet
2.2 Queue Uing Array & Linked List
28 pages
Linked List
PDF
No ratings yet
Linked List
13 pages
Experiment 7
PDF
No ratings yet
Experiment 7
6 pages
Queue Part1 - 241007 - 144154
PDF
No ratings yet
Queue Part1 - 241007 - 144154
8 pages
Queue Representation
PDF
No ratings yet
Queue Representation
10 pages
Linked List
PDF
No ratings yet
Linked List
19 pages
5 Queues
PDF
No ratings yet
5 Queues
33 pages
Dbms-1 - Database Management System Important Questions Dbms-1 - Database Management System Important Questions
PDF
No ratings yet
Dbms-1 - Database Management System Important Questions Dbms-1 - Database Management System Important Questions
7 pages
Linked Lists Are Dynamic in Nature"
PDF
No ratings yet
Linked Lists Are Dynamic in Nature"
10 pages
Dsu Pr22 Main
PDF
No ratings yet
Dsu Pr22 Main
7 pages
8032experiment No-8
PDF
No ratings yet
8032experiment No-8
6 pages
DS U (1+3+5) Slips
PDF
No ratings yet
DS U (1+3+5) Slips
18 pages
Queus Implementation Using Linked List
PDF
No ratings yet
Queus Implementation Using Linked List
17 pages
FALLSEM2024-25 ISWE102L TH VL2024250102844 2024-08-12 Reference-Material-I
PDF
No ratings yet
FALLSEM2024-25 ISWE102L TH VL2024250102844 2024-08-12 Reference-Material-I
7 pages
DS Assignment
PDF
No ratings yet
DS Assignment
6 pages
BCS304 - Data
PDF
No ratings yet
BCS304 - Data
17 pages
Handout 3.3 - Queues
PDF
No ratings yet
Handout 3.3 - Queues
5 pages
DS Unit - 4 R23
PDF
No ratings yet
DS Unit - 4 R23
9 pages
11-Stack and Queue Using Linked List-03!04!2023
PDF
No ratings yet
11-Stack and Queue Using Linked List-03!04!2023
7 pages
Data Structures - Unit-3
PDF
No ratings yet
Data Structures - Unit-3
19 pages
Data Structure Rograms
PDF
No ratings yet
Data Structure Rograms
50 pages
2023 - Linked Lists Course Notes
PDF
No ratings yet
2023 - Linked Lists Course Notes
4 pages
1
PDF
No ratings yet
1
12 pages
Queue Operations Using Linked List - The Code Cracker
PDF
No ratings yet
Queue Operations Using Linked List - The Code Cracker
7 pages
1
PDF
No ratings yet
1
19 pages
C - DS - Unit - 3
PDF
No ratings yet
C - DS - Unit - 3
17 pages
Queue Basics
PDF
No ratings yet
Queue Basics
13 pages
Queue Operation of Link List: Lab Report No.13
PDF
No ratings yet
Queue Operation of Link List: Lab Report No.13
4 pages
Linked List Implementation of Stack & Queue
PDF
No ratings yet
Linked List Implementation of Stack & Queue
17 pages