Skip to content

Commit

Permalink
get difference
Browse files Browse the repository at this point in the history
  • Loading branch information
imashiqe committed Oct 14, 2024
1 parent 638740e commit a5d0cb3
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
61 changes: 61 additions & 0 deletions datastucture/assignment01/GetDifference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <limits>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};

void insert(Node*& head, int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
} else {
Node* t = head;
while (t->next != nullptr) {
t = t->next;
}
t->next = newNode;
}
}

int computeDifference(Node* head) {
if (head == nullptr) return 0;
int maxVal = std::numeric_limits<int>::min();
int mval = std::numeric_limits<int>::max();
Node* current = head;
while (current != nullptr) {
if (current->data > maxVal) {
maxVal = current->data;
}
if (current->data < mval) {
mval = current->data;
}
current = current->next;
}
return maxVal - mval;
}
void deleteList(Node*& head) {
Node* current = head;
Node* nextNode;
while (current != nullptr) {
nextNode = current->next;
delete current;
current = nextNode;
}
head = nullptr;
}
int main() {
Node* head = nullptr;
int value;
while (true) {
cin >> value;
if (value == -1) break;
insert(head, value);
}
int difference = computeDifference(head);
cout << difference << endl;
deleteList(head);
return 0;
}
66 changes: 66 additions & 0 deletions datastucture/assignment01/sametosame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};

void insert(Node*& head, int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

bool areIdentical(Node* head1, Node* head2) {
while (head1 != nullptr && head2 != nullptr) {
if (head1->data != head2->data) {
return false;
}
head1 = head1->next;
head2 = head2->next;
}
return head1 == nullptr && head2 == nullptr;
}
void deleteList(Node*& head) {
Node* current = head;
Node* nextNode;
while (current != nullptr) {
nextNode = current->next;
delete current;
current = nextNode;
}
head = nullptr;
}
int main() {
Node* head1 = nullptr;
Node* head2 = nullptr;
int value;
while (true) {
cin >> value;
if (value == -1) break;
insert(head1, value);
}
while (true) {
cin >> value;
if (value == -1) break;
insert(head2, value);
}
if (areIdentical(head1, head2)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
deleteList(head1);
deleteList(head2);

return 0;
}
62 changes: 62 additions & 0 deletions datastucture/assignment01/search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};

void insert(Node*& head, int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
} else {
Node* t = head;
while (t->next != nullptr) {
t = t->next;
}
t->next = newNode;
}
}
int findIndex(Node* head, int X) {
int index = 0;
Node* c = head;
while (c != nullptr) {
if (c->data == X) {
return index;
}
c = c->next;
index++;
}
return -1;
}
void deleteList(Node*& head) {
Node* c = head;
Node* nextNode;
while (c != nullptr) {
nextNode = c->next;
delete c;
c = nextNode;
}
head = nullptr;
}
int main() {
int T;
cin >> T;
while (T--) {
Node* head = nullptr;
int value;
while (true) {
cin >> value;
if (value == -1) break;
insert(head, value);
}
int X;
cin >> X;
int index = findIndex(head, X);
cout << index << endl;
deleteList(head);
}
return 0;
}

0 comments on commit a5d0cb3

Please sign in to comment.