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 Comprehensive Guide with Code Examples, Summaries of Advanced Computational Complexity

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 charactersWrite a title that briefly explains what it is about. Between 20 and 90 characters

Typology: Summaries

2022/2023

Uploaded on 05/11/2023

avshaabdalla
avshaabdalla 🇮🇳

2 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
// C program to implement Linked List operations
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *nextPtr;
};
typedef struct node Node;
typedef Node *NodePtr;
/*************
* Add a new element to the beginning of the list
*/
void addFirst(NodePtr *sPtr, NodePtr *ePtr, int value);
/*************
* Add a new element to the end of the list
*/
void addLast(NodePtr *sPtr, NodePtr *ePtr, int value);
/*************
* Print the list in the reverse order
*/
void printReverseList(NodePtr sPtr);
/*************
* Reverse the list, in-place
*/
void reverseList(NodePtr *sPtr, NodePtr *ePtr);
/*************
* Safely make a linked list empty.
*/
void emptyList(NodePtr *sPtr, NodePtr *ePtr);
/*************
* Merge two lists into a new ordered list
*/
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Linked List Operations in C: A Comprehensive Guide with Code Examples and more Summaries Advanced Computational Complexity in PDF only on Docsity!

// C program to implement Linked List operations #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *nextPtr; }; typedef struct node Node; typedef Node *NodePtr; /*************

  • Add a new element to the beginning of the list */ void addFirst(NodePtr *sPtr, NodePtr *ePtr, int value); /*************
  • Add a new element to the end of the list */ void addLast(NodePtr *sPtr, NodePtr *ePtr, int value); /*************
  • Print the list in the reverse order */ void printReverseList(NodePtr sPtr); /*************
  • Reverse the list, in-place */ void reverseList(NodePtr *sPtr, NodePtr *ePtr); /*************
  • Safely make a linked list empty. */ void emptyList(NodePtr *sPtr, NodePtr *ePtr); /*************
  • Merge two lists into a new ordered list */

void merge(NodePtr list1, NodePtr list2, NodePtr *l3StartPtr, NodePtr *l3EndPtr); // Check if the list is empty (Already developed) int isEmpty(NodePtr sPtr); // Print out the list (Already developed) void printList(NodePtr sPtr); // Selection menu (Already developed) void menu(void); int main() { // Two pointers for the beginning and ending of the linked list generated. NodePtr startPtr = NULL; NodePtr endPtr = NULL; // Three pairs of pointers for beginning and ending of the three linked lists used in the merge selection NodePtr l1StartPtr = NULL; NodePtr l1EndPtr = NULL; NodePtr l2StartPtr = NULL; NodePtr l2EndPtr = NULL; NodePtr l3StartPtr = NULL; NodePtr l3EndPtr = NULL; int item; menu(); unsigned int choice; scanf("%u", &choice); while (choice != 6) { switch (choice) { case 1: /*************

  • Prompt the user to input an integer number.

break; case 5: /*************

  • 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 linked list with some ordered elements (Already developed) for (unsigned int i = 1; i <= 20; i+=3) addLast(&l1StartPtr, &l1EndPtr, i); // Create another linked list with some ordered elements (Already developed) for (unsigned int i = 2; i <= 30; i+=7) addLast(&l2StartPtr, &l2EndPtr, 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); puts("List 1: "); printList(l1StartPtr); puts("List 2: "); printList(l2StartPtr); puts("Merged List: "); printList(l3StartPtr); break; default: puts("Invalid choice.\n"); break; } menu(); scanf("%u", &choice);

return 0; } // 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", "? "); } // return 1 if the list is empty, 0 otherwise (Already developed) int isEmpty(NodePtr sPtr) { return sPtr == NULL; } // print the list from the beginning (Already developed) void printList(NodePtr sPtr) { if (isEmpty(sPtr)) puts("List is empty.\n"); else { puts("The list is:"); while (sPtr != NULL) { printf("%d --> ", sPtr->data); sPtr = sPtr->nextPtr; } puts("NULL\n"); } } /*************

  • Add a new element to the beginning of the list */

printReverseList(sPtr->nextPtr); // call printReverseList with next pointer of sPtr printf("%d --> ",sPtr->data); // display sPtr } } /*************

  • Reverse the list, in-place */ void reverseList(NodePtr *sPtr, NodePtr ePtr) { NodePtr curr = (sPtr); // set curr to start pointer NodePtr next, prev = NULL; // set prev to null // loop till the end of list while(curr != NULL) { next = curr->nextPtr; // set next to node next to curr curr->nextPtr = prev; // set next of curr to prev if(prev == NULL) // if prev is null, set end pointer to curr *ePtr = curr; prev = curr; // set prev to curr curr = next; // set curr to next } // update start pointer to prev *sPtr = prev; } /*************
  • Safely make a linked list empty. */ void emptyList(NodePtr *sPtr, NodePtr ePtr) { // loop over the end of linked list while(sPtr != NULL) { NodePtr temp = *sPtr; // set temp to start pointer sPtr = (sPtr)->nextPtr; // set start pointer to node next to start pointer

free(temp); // delete temp } // set start and end pointer to null *ePtr = NULL; *sPtr = NULL; } /*************

  • Merge two lists into a new ordered list */ void merge(NodePtr list1, NodePtr list2, NodePtr *l3StartPtr, NodePtr *l3EndPtr) { NodePtr l1Curr = list1; // set l1Curr to list NodePtr l2Curr = list2; // set l2Curr to list // loop over list1 and list while((l1Curr != NULL) && (l2Curr != NULL)) { // if list1 data < list2 data, insert list1 data into list3's end of list if(l1Curr->data < l2Curr->data) { addLast(l3StartPtr, l3EndPtr, l1Curr->data); l1Curr = l1Curr->nextPtr; } else // if list2 data < list1 data, insert list2 data into list3's end of list { addLast(l3StartPtr, l3EndPtr, l2Curr->data); l2Curr = l2Curr->nextPtr; } } // add remaining data of list1 into list while(l1Curr != NULL) { addLast(l3StartPtr, l3EndPtr, l1Curr->data); l1Curr = l1Curr->nextPtr; }