Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
tynnp authored Jun 16, 2024
1 parent c490c49 commit 77ebdb2
Showing 1 changed file with 52 additions and 29 deletions.
81 changes: 52 additions & 29 deletions UPCODER/Cấu trúc dữ liệu/3. Danh sách liên kết/DSLK1.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,75 @@
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct Node {
char data;
Node* next;
Node(char c): data(c), next(nullptr) {}
Node *next;
Node(char c): data(c), next(nullptr) {}
};

struct LinkedList {
Node* head;
Node *head;
LinkedList(): head(nullptr) {}

void add(char c) {
Node* node = new Node(c);
Node* tmp = this->head;
if (tmp == nullptr)
this->head = node;
else {
while (tmp->next != nullptr)
tmp = tmp->next;
tmp->next = node;

~LinkedList() {
Node *cur = head;
while (cur != nullptr) {
Node *tmp = cur;
cur = cur->next;
delete tmp;
}
return;
}

void check() {
Node* tmp = this->head;
while (tmp != nullptr && tmp->next != nullptr) {
if (tmp->data > tmp->next->data)
tmp->next = tmp->next->next;
else tmp = tmp->next;
void push_back(char c) {
Node *node = new Node(c);

if (head == nullptr) {
head = node;
return;
}

Node *last = head;
while (last->next != nullptr)
last = last->next;
last->next = node;
}

void print() {
Node* tmp = this->head;
Node *tmp = head;
while (tmp != nullptr) {
cout << tmp->data << " ";
tmp = tmp->next;
}
}

void solve() {
if (head == nullptr) return;

Node *prev = head;
Node *cur = head->next;

while (cur != nullptr) {
if (prev->data > cur->data) {
Node *tmp = cur;
prev->next = cur->next;
cur = cur->next;
delete tmp;
} else {
prev = cur;
cur = cur->next;
}
}
}
};

int main() {
LinkedList list;
char c;
while (cin >> c) list.add(c);
list.check();
list.print();
char tmp;
LinkedList a;

while (cin >> tmp)
a.push_back(tmp);

a.solve();
a.print();
return 0;
}

0 comments on commit 77ebdb2

Please sign in to comment.