Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Linked List Operations in C: A Practical Guide with Exercises, Exercises of Computer Science

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

2022/2023

Uploaded on 05/11/2023

avshaabdalla
avshaabdalla 🇮🇳

2 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* nextPtr;
} Node;
/*************
* Develop this function at the bottom of this program
* Add a new element to the beginning of the list
*/
void addFirst(Node** sPtr, Node** ePtr, Node* newPtr);
/*************
* Develop this function at the bottom of this program
* Add a new element to the end of the list
*/
void addLast(Node** sPtr, Node** ePtr, Node* newPtr);
/*************
* Develop this function at the bottom of this program
* Print the list in the reverse order
*/
void printReverseList(Node* sPtr);
/*************
* Develop this function at the bottom of this program
* Reverse the list, in-place
*/
void reverseList(Node** sPtr, Node** ePtr);
/*************
* Develop this function at the bottom of this program
* Merge two lists into a new ordered list
*/
void merge(Node* list1, Node* list2, Node** l3StartPtr, Node** l3EndPtr);
/*************
* Develop this function at the bottom of this program
* Safely make a linked list empty.
*/
void emptyList(Node* sPtr, Node* ePtr);
// Check if the list is empty (Already developed)
int isEmpty(Node* sPtr);
// Print out the list (Already developed)
void printList(Node* sPtr);
// Selection menu (Already developed)
void menu(void);
int main(void) {
// Two pointers for the beginning and ending of the linked list generated.
Node* startPtr = NULL;
Node* endPtr = NULL;
// Three pairs of pointers for beginning and ending of the three linked lists used in the merge selection
Node* l1StartPtr = NULL;
Node* l1EndPtr = NULL;
Node* l2StartPtr = NULL;
Node* l2EndPtr = NULL;
Node* l3StartPtr = NULL;
Node* l3EndPtr = NULL;
Node* currentPtr = NULL;
Node* newPtr = NULL;
int item;
menu();
unsigned int choice;
scanf("%u", &choice);
while (choice != 6) {
switch (choice) {
case 1:
/*************
* Complete this part:
* Prompt the user to input an integer number.
* Read the user's input.
* Then, add the input to the BEGINNING of the linked list with the two pointers startPtr and endPtr.
* Then, print the linked list.
*/
printf("%s", "Enter an integer number: ");
scanf("\n%d", &item);
newPtr = (Node*)malloc(sizeof(Node));
newPtr->data = item;
newPtr->nextPtr = NULL;
addFirst(&startPtr, &endPtr, newPtr);
printList(startPtr);
break;
case 2:
/*************
* Complete this part:
* Prompt the user to input an integer number.
* Read the user's input.
* Then, add the input to the END of the linked list with the two pointers startPtr and endPtr.
* Then, print the linked list.
*/
printf("%s", "Enter an integer number: ");
scanf("\n%d", &item);
newPtr = (Node*)malloc(sizeof(Node));
newPtr->data = item;
newPtr->nextPtr = NULL;
addLast(&startPtr, &endPtr, newPtr);
printList(startPtr);
break;
case 3:
/*************
* Complete this part:
* Print the list in the reverse order.
*/
currentPtr = startPtr;
printReverseList(currentPtr);
puts("");
break;
case 4:
/*************
* Complete this part:
* Reverse the list, in-place.
* Then, print the linked list.
*/
reverseList(&startPtr, &endPtr);
printList(startPtr);
break;
case 5:
/*************
* Complete this part:
* Empty the three lists with the pair of pointers [l1StartPtr,l1EndPtr], [l2StartPtr,l2EndPtr] and [l3StartPtr,l3EndPtr]
*/
emptyList(l1StartPtr, l1EndPtr);
emptyList(l2StartPtr, l2EndPtr);
emptyList(l3StartPtr, l3EndPtr);
// Create a new linked list with some ordered elements (Already developed)
for (unsigned int i = 1; i <= 20; i+=3) {
newPtr = (Node*)malloc(sizeof(Node));
addLast(&l1StartPtr, &l1EndPtr, newPtr, i);
}
// Create another new linked list with some ordered elements (Already developed)
for (unsigned int i = 2; i <= 30; i+=7) {
newPtr = (Node*)malloc(sizeof(Node));
addLast(&l2StartPtr, &l2EndPtr, newPtr, i);
}
/*************
* Complete this part:
* Merge the two above lists into a new ordered list. Use l3StartPtr, l3EndPtr for the new list
* Then, print the two original lists and the new merged list.
pf3

Partial preview of the text

Download Linked List Operations in C: A Practical Guide with Exercises and more Exercises Computer Science in PDF only on Docsity!

#include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node* nextPtr; } Node; /*************

  • Develop this function at the bottom of this program
  • Add a new element to the beginning of the list / void addFirst(Node* sPtr, Node** ePtr, Node* newPtr); /*************
  • Develop this function at the bottom of this program
  • Add a new element to the end of the list / void addLast(Node* sPtr, Node** ePtr, Node* newPtr); /*************
  • Develop this function at the bottom of this program
  • Print the list in the reverse order / void printReverseList(Node sPtr); /*************
  • Develop this function at the bottom of this program
  • Reverse the list, in-place / void reverseList(Node* sPtr, Node** ePtr); /*************
  • Develop this function at the bottom of this program
  • Merge two lists into a new ordered list / void merge(Node list1, Node* list2, Node** l3StartPtr, Node** l3EndPtr); /*************
  • Develop this function at the bottom of this program
  • Safely make a linked list empty. / void emptyList(Node sPtr, Node* ePtr); // Check if the list is empty (Already developed) int isEmpty(Node* sPtr); // Print out the list (Already developed) void printList(Node* sPtr); // Selection menu (Already developed) void menu(void); int main(void) { // Two pointers for the beginning and ending of the linked list generated. Node* startPtr = NULL; Node* endPtr = NULL; // Three pairs of pointers for beginning and ending of the three linked lists used in the merge selection Node* l1StartPtr = NULL; Node* l1EndPtr = NULL; Node* l2StartPtr = NULL; Node* l2EndPtr = NULL; Node* l3StartPtr = NULL; Node* l3EndPtr = NULL; Node* currentPtr = NULL; Node* newPtr = NULL; int item; menu(); unsigned int choice; scanf("%u", &choice); while (choice != 6) { switch (choice) { case 1: /*************
  • Complete this part:
  • Prompt the user to input an integer number.
  • Read the user's input.
  • Then, add the input to the BEGINNING of the linked list with the two pointers startPtr and endPtr.
  • Then, print the linked list. / printf("%s", "Enter an integer number: "); scanf("\n%d", &item); newPtr = (Node)malloc(sizeof(Node)); newPtr->data = item; newPtr->nextPtr = NULL; addFirst(&startPtr, &endPtr, newPtr); printList(startPtr); break; case 2: /*************
  • Complete this part:
  • Prompt the user to input an integer number.
  • Read the user's input.
  • Then, add the input to the END of the linked list with the two pointers startPtr and endPtr.
  • Then, print the linked list. / printf("%s", "Enter an integer number: "); scanf("\n%d", &item); newPtr = (Node)malloc(sizeof(Node)); newPtr->data = item; newPtr->nextPtr = NULL; addLast(&startPtr, &endPtr, newPtr); printList(startPtr); break; case 3: /*************
  • Complete this part:
  • Print the list in the reverse order. */ currentPtr = startPtr; printReverseList(currentPtr); puts(""); break; case 4: /*************
  • Complete this part:
  • Reverse the list, in-place.
  • Then, print the linked list. */ reverseList(&startPtr, &endPtr); printList(startPtr); break; case 5: /*************
  • Complete this part:
  • Empty the three lists with the pair of pointers [l1StartPtr,l1EndPtr], [l2StartPtr,l2EndPtr] and [l3StartPtr,l3EndPtr] / emptyList(l1StartPtr, l1EndPtr); emptyList(l2StartPtr, l2EndPtr); emptyList(l3StartPtr, l3EndPtr); // Create a new linked list with some ordered elements (Already developed) for (unsigned int i = 1; i <= 20; i+=3) { newPtr = (Node)malloc(sizeof(Node)); addLast(&l1StartPtr, &l1EndPtr, newPtr, i); } // Create another new linked list with some ordered elements (Already developed) for (unsigned int i = 2; i <= 30; i+=7) { newPtr = (Node*)malloc(sizeof(Node)); addLast(&l2StartPtr, &l2EndPtr, newPtr, i); } /*************
  • Complete this part:
  • Merge the two above lists into a new ordered list. Use l3StartPtr, l3EndPtr for the new list
  • Then, print the two original lists and the new merged list.

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.");