1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include <iostream>
using namespace std;
struct Node { int data; Node* next; };
Node* createNode(int value) { Node* newNode = new Node; newNode->data = value; newNode->next = nullptr; return newNode; }
void insertNodeAtEnd(Node*& head, int value) { Node* newNode = createNode(value);
if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } }
int findNodePosition(Node* head, int value) { int position = 1; Node* current = head; while (current != nullptr) { if (current->data == value) { return position; } current = current->next; position++; } return -1; }
bool deleteNodeAtPosition(Node*& head, int position) { if (position < 1) { return false; }
if (position == 1) { Node* temp = head; head = head->next; delete temp; return true; }
Node* current = head; int currentPosition = 1; while (currentPosition < position - 1 && current != nullptr) { current = current->next; currentPosition++; }
if (current == nullptr || current->next == nullptr) { return false; }
Node* temp = current->next; current->next = current->next->next; delete temp; return true; }
Node* mergeSortedLists(Node* list1, Node* list2) { Node* mergedList = nullptr; Node* current = mergedList;
while (list1 != nullptr && list2 != nullptr) { if (list1->data < list2->data) { insertNodeAtEnd(current, list1->data); list1 = list1->next; } else { insertNodeAtEnd(current, list2->data); list2 = list2->next; }
if (mergedList == nullptr) { mergedList = current; } }
while (list1 != nullptr) { insertNodeAtEnd(current, list1->data); list1 = list1->next; }
while (list2 != nullptr) { insertNodeAtEnd(current, list2->data); list2 = list2->next; }
return mergedList; }
void printList(Node* head) { Node* current = head; while (current != nullptr) { cout << current->data << " -> "; current = current->next; } cout << "nullptr" << endl; }
int main() { Node* list1 = nullptr; Node* list2 = nullptr;
for (int i = 1; i <= 5; i++) { insertNodeAtEnd(list1, i * 2); insertNodeAtEnd(list2, i * 2 - 1); }
cout << "List 1: "; printList(list1);
cout << "List 2: "; printList(list2);
Node* mergedList = mergeSortedLists(list1, list2);
cout << "Merged List: "; printList(mergedList);
int searchValue = 6; int position = findNodePosition(mergedList, searchValue); if (position != -1) { cout << "Found " << searchValue << " at position " << position << endl; } else { cout << searchValue << " not found in the merged list." << endl; }
int deletePosition = 3; if (deleteNodeAtPosition(mergedList, deletePosition)) { cout << "Deleted node at position " << deletePosition << endl; cout << "Updated List: "; printList(mergedList); } else { cout << "Unable to delete node at position " << deletePosition << endl; }
while (mergedList != nullptr) { Node* temp = mergedList; mergedList = mergedList->next; delete temp; }
return 0; }
|