0% found this document useful (0 votes)
7 views3 pages

Doubly Linked List - Attempt Review

Uploaded by

anneanitap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Doubly Linked List - Attempt Review

Uploaded by

anneanitap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Started on Sunday, 29 September 2024, 7:20 PM

State Finished
Completed on Sunday, 29 September 2024, 7:28 PM
Time taken 7 mins 48 secs
Marks 10.00/10.00
Grade 100.00 out of 100.00
Name HARSHINE S 730423243041
Question 1

Correct

Mark 10.00 out of 10.00

Write a code for doubly linked list and perform the following operations

Add a value (from user) - First node


Add another value (from user) at the end - Append node
Add another value (from user) at the end - Append node
Insert another value (from user) between first and second node
Delete the third node
display the list

In the below example


10->20->30 is created first
Then 10->40->20->30 40 is inserted between 1 and 2
Then 20 is deleted - 3rd node
Then list is displayed

For example:

Input Result

10 10 40 30
20
30
40

Answer: (penalty regime: 0 %)

Language c

1 #include <stdio.h>
2 #include <stdlib.h>
3▼ struct Node {
4 int data;
5 struct Node* next;
6 struct Node* prev;
7 };
8▼ struct Node* createNode(int data) {
9 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
10 newNode->data = data;
11 newNode->next = NULL;
12 newNode->prev = NULL;
13 return newNode;
14 }
15 ▼ void addFirst(struct Node** head_ref, int data) {
16 struct Node* newNode = createNode(data);
17 newNode->next = *head_ref;
18 ▼ if (*head_ref != NULL) {
19 (*head_ref)->prev = newNode;
20 }
21 *head_ref = newNode;
22 }
23 ▼ void append(struct Node** head_ref, int data) {
24 struct Node* newNode = createNode(data);
25 ▼ if (*head_ref == NULL) {
26 *head_ref = newNode;
27 return;
28 }
29 struct Node* last = *head_ref;
30 ▼ while (last->next != NULL) {
31 last = last->next;
32 }
33 last->next = newNode;
34 newNode->prev = last;
35 }
36 ▼ void insertBetween(struct Node** head_ref, int data) {
37 ▼ if (*head_ref == NULL || (*head_ref)->next == NULL) {
38 return;
39 }
40 struct Node* newNode = createNode(data);
41 struct Node* first = *head_ref;
42 struct Node* second = first->next;
43 first->next = newNode;
44 newNode->prev = first;
45 newNode->next = second;
46 second->prev = newNode;
47 }
48 ▼ void deleteThirdNode(struct Node**
49 ▼ head_ref) {
50 ▼ if (*head_ref == NULL || (*head_ref)->next == NULL || (*head_ref)
51 return;
52 }

Input Expected Got

 10 10 40 30 10 40 30 
20
30
40

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

You might also like