0% found this document useful (0 votes)
5 views17 pages

1

Data structure 2

Uploaded by

gopalkr9958
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
0% found this document useful (0 votes)
5 views17 pages

1

Data structure 2

Uploaded by

gopalkr9958
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
You are on page 1/ 17
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; ee printf("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 = (struc Node*) 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, meaning hat 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 an error. 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