Question

I need help with encoding a doubly-linked list, which must include a function that sorts the...

I need help with encoding a doubly-linked list, which must include a function that sorts the name field in the bubble method, it must be encoded in the c language.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The program for sorting the double linked list with the bubble method is given below,

#include <stdio.h>   
struct ddnode{
int num;
struct ddnode *prev;
struct ddnode *next;
};
struct ddnode *head, *tail = NULL;   
//insertNode() will add a ddnode to the list
void insertNode(int num) {
//Create a new ddnode
struct ddnode *newDnode = (struct ddnode*)malloc(sizeof(struct ddnode));
newDnode->num = num;

if(head == NULL) {
//Both head and tail will point to newDnode
head = tail = newDnode;
//head's prev will point to NULL
head->prev = NULL;
//tail's next will point to NULL, as it is the last ddnode of the list
tail->next = NULL;
}
else {
//newDnode will be added after tail such that tail's next will point to newDnode
tail->next = newDnode;
//newDnode's prev will point to tail
newDnode->prev = tail;
//newDnode will become new tail
tail = newDnode;
//As it is last ddnode, tail's next will point to NULL
tail->next = NULL;
}
}

//sortDoubleLinkedList() will sort the given list in ascending order
void sortDoubleLinkedList() {
struct ddnode *curr = NULL, *index = NULL;
int temp;
//Check whether list is empty
if(head == NULL) {
return;
}
else {
//Current will point to head
for(curr = head; curr->next != NULL; curr = curr->next) {
//Index will point to ddnode next to curr
for(index = curr->next; index != NULL; index = index->next) {
//If curr's num is greater than index's num, swap the num of curr and index
if(curr->num > index->num) {
temp = curr->num;
curr->num = index->num;
index->num = temp;
}
}
}
}
}

//printList() will print out the nodes of the list
void printList() {
//Node curr will point to head
struct ddnode *curr = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
while(curr != NULL) {
//Prints each ddnode by incrementing pointer.
printf("%d ",curr->num);
curr = curr->next;
}
printf("\n");
}

int main() {
//adding the nodes in thr list
insertNode(4);
insertNode(3);
insertNode(9);
insertNode(1);
insertNode(2);
   insertNode(8);
// original list
printf("Original list: \n");
printList();
//Sorting operation
sortDoubleLinkedList();
// sorted list
printf("Sorted list: \n");
printList();
return 0;
}

SCREENSHOT

#include <stdio.h> struct ddinode int num; struct ddnode prev; struct ddnode *next; : struct ddinode head, tail - NULL; //i

//If currs nu is greater than indexs num, swap the nun of curr and index if (curr- num > index>num) { temp - curr num; curr

o stdout Original list: 714 52 Sorted list: 1 24 5 7

Add a comment
Know the answer?
Add Answer to:
I need help with encoding a doubly-linked list, which must include a function that sorts the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT