#include<iostream> #include<cstdlib>// For dynamic memory management
typedefint ElemType; // Assuming element type is int
// Define the node structure for a singly linked list structiNode { ElemType data; // Data field iNode *next; // Pointer field };
typedef iNode* LinkList; // Definition for the linked list type
// Initialize the singly linked list with a head node boolInitList(LinkList &L){ L = newiNode(); // Allocate memory for the head node using new if (!L) returnfalse; // Check if memory allocation was successful L->next = nullptr; // Set the next pointer of head to nullptr returntrue; }
// Append a new node at the end of the linked list voidAppendNode(LinkList &L, ElemType value){ iNode *newNode = newiNode(); // Create a new node using new newNode->data = value; newNode->next = nullptr;
iNode *current = L; while (current->next != nullptr) { current = current->next; } current->next = newNode; }
// Print all elements in the linked list voidPrintList(LinkList L){ iNode *current = L->next; // Skip the head node while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; }
// Compute the length of the linked list intLength(LinkList L){ int len = 0; iNode *p = L->next; // Start from the first data node, skipping the head node while (p != nullptr) { len++; p = p->next; } return len; }
// Free the linked list and release memory voidDeleteList(LinkList &L){ iNode *current = L; while (current != nullptr) { iNode *next = current->next; delete current; // Use delete instead of free current = next; } L = nullptr; }
// Function to insert an element at the i-th position in the linked list boolListInsert(LinkList &L, int i, ElemType e){ iNode *p = L; // Start from the head node int j = 0; // Index to track the position
// Find the (i-1)-th node while (p != nullptr && j < i - 1) { p = p->next; j++; }
// Check if the position is valid if (p == nullptr) { returnfalse; }
// Create a new node iNode *s = newiNode(); if (!s) returnfalse; // Check if memory allocation was successful s->data = e; s->next = p->next; p->next = s;
returntrue; }
// Get the pointer to the i-th element in the linked list iNode *GetElem(LinkList L, int i){ iNode *p = L; // Start from the head node int j = 0; while (p != nullptr && j < i) { p = p->next; j++; } // Return the pointer to the i-th node or nullptr return p; }
// Locate a node by its value in the linked list iNode *LocateElem(LinkList L, ElemType e){ iNode *p = L->next; // Start from the first data node while (p != nullptr && p->data != e) { p = p->next; } // Return the pointer to the node if found, otherwise return nullptr return p; }
#include<iostream> #include<cstdlib>// For dynamic memory allocation
structLNode { int data; LNode *next; };
typedef LNode* LinkList;
// Function to initialize the linked list with a dummy head node LinkList InitList() { LinkList L = (LNode*)malloc(sizeof(LNode)); // Allocate memory for the head node if (L == nullptr) { std::cerr << "Memory allocation failed." << std::endl; exit(EXIT_FAILURE); } L->next = nullptr; // Initially, the list is empty return L; }
// Function to insert elements into the list using head insertion LinkList HeadInsert(LinkList &L) { int x; std::cout << "Enter numbers to insert into the linked list (9999 to end): "; std::cin >> x; // Read the first element while (x != 9999) { LNode *s = (LNode*)malloc(sizeof(LNode)); // Allocate memory for a new node if (s == nullptr) { std::cerr << "Memory allocation failed." << std::endl; continue; // If allocation fails, skip to the next iteration } s->data = x; // Set the node's data LinkList HeadInsert(LinkList &L) { int x; std::cout << "Enter numbers to insert into the linked list (9999 to end): "; std::cin >> x; // Read the first element while (x != 9999) { LNode *s = (LNode*)malloc(sizeof(LNode)); // Allocate memory for a new node if (s == nullptr) { std::cerr << "Memory allocation failed." << std::endl; continue; // If allocation fails, skip to the next iteration } s->data = x; // Set the node's data s->next = L->next; // Insert the new node at the beginning L->next = s; std::cin >> x; // Read the next element } return L; } // Insert the new node at the beginning L->next = s; std::cin >> x; // Read the next element } return L; }
// Function to print the list elements voidPrintList(LinkList L) { LNode *current = L->next; // Start from the first actual element while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; }
intmain() { LinkList myList = InitList(); // Initialize the linked list myList = HeadInsert(myList); // Perform head insertion of elements std::cout << "The elements in the linked list are: "; PrintList(myList); // Print the elements in the linked list return0; }