

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Write a title that briefly explains what it is about. Between 20 and 90 charactersWrite a title that briefly explains what it is about. Between 20 and 90 charactersWrite a title that briefly explains what it is about. Between 20 and 90 characters
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!
#include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node* nextPtr; } Node; /*************
merge(l1StartPtr, l2StartPtr, &l3StartPtr, &l3EndPtr); printf("%s\n", "List 1:"); printList(l1StartPtr); printf("%s\n", "List 2:"); printList(l2StartPtr); printf("%s\n", "Merged list:"); printList(l3StartPtr); break; default: puts("Invalid choice.\n"); break; } menu(); scanf("%u", &choice); } }
// display menu to the user (Already developed) void menu(void) { puts("Enter your choice:\n" " 1 to add a new element to the beginning of the list.\n" " 2 to add a new element to the end of the list.\n" " 3 to print the list in the reverse order.\n" " 4 to reverse the list, in-place.\n" " 5 to merge two ordered lists into a new ordered list.\n" " 6 to end."); printf("%s", "? "); }
void addFirst(Node** sPtr, Node** ePtr, Node* newPtr) { if (*sPtr == NULL) { // LinkedList is empty *sPtr = newPtr; *ePtr = newPtr; } else { // If the linkedList is not empty newPtr->nextPtr = *sPtr; *sPtr = newPtr; } }
void addLast(Node** sPtr, Node** ePtr, Node* newPtr) { if (*sPtr == NULL) { // If the linkedList is empty, same as addFirst *sPtr = newPtr; ePtr = newPtr; } else { // If the linkedList is not empty (ePtr)->nextPtr = newPtr; *ePtr = newPtr; } }
// return 1 if the list is empty, 0 otherwise (Already developed) int isEmpty(Node* sPtr) { return sPtr == NULL; }
// print the list from the beginning (Already developed) void printList(Node* sPtr) { if (isEmpty(sPtr)) puts("List is empty.\n"); else { Node* currentPtr = sPtr; puts("The list is:");
while (currentPtr != NULL) { printf("%d --> ", currentPtr->data); currentPtr = currentPtr->nextPtr; } puts("NULL\n"); } }
// print the list in the reverse order void printReverseList(Node* sPtr) { if (sPtr == NULL) { printf("%s", "The list in reverse order is:\nNULL --> "); return; } else printReverseList(sPtr->nextPtr); printf("%d --> ", sPtr->data); }
// Reverse the list, in-place, without using any other data structure like another list, stack or queue. void reverseList(Node** sPtr, Node** ePtr) { // If linkedList is empty or has only one node, do nothing. if (isEmpty(sPtr) || (sPtr)->nextPtr == NULL) return; else { ePtr = sPtr; // Move end pointer to the start pointer. Node t1 = sPtr; // A temporary pointer, initially points to the start Node t2 = (sPtr)->nextPtr; // A temporary pointer, initially points to the start->next int first = 1; // A tag to indicate we are in the very first step, initialized to true while (t2 != NULL) { // Repeat until we reach to the end of the linkedlist. sPtr = t2; // Update the starting point to the next node t2 = t2->nextPtr; // Go forward for the second temprary pointer (sPtr)->nextPtr = t1; // Reverse the next pointer to the previous node if (first) { // If we are in the first node t1->nextPtr = NULL; // Change the next to NULL (As the new end node) first = 0; } t1 = *sPtr; // Go forward for the first temprary pointer } }}
void emptyList(Node* sPtr, Node* ePtr) { while (sPtr != NULL) { Node* temp = sPtr; sPtr = sPtr->nextPtr; free(temp);} ePtr = NULL;}
void merge(Node* list1, Node* list2, Node** l3StartPtr, Node** l3EndPtr) { while (!isEmpty(list1) || !isEmpty(list2)) { Node* newPtr = (Node*)malloc(sizeof(Node)); // create node if (newPtr != NULL) { if (isEmpty(list1)) { addLast(l3StartPtr, l3EndPtr, newPtr, list2->data); list2 = list2->nextPtr; } else if (isEmpty(list2)) { addLast(l3StartPtr, l3EndPtr, newPtr, list1->data); list1 = list1->nextPtr; } else if (list1->data <= list2->data) { addLast(l3StartPtr, l3EndPtr, newPtr, list1->data); list1 = list1->nextPtr;
} else { addLast(l3StartPtr, l3EndPtr, newPtr, list2->data); list2 = list2->nextPtr; } } else { puts("No memory available.");