Vous êtes sur la page 1sur 10

// STACK USING CLASS

#include <iostream>

using namespace std;


class node
{
public:
int data;
node *next;
};
class stack
{
private:
node *top;
public:
// constructor
stack()
{
top = nullptr;
}
// main fuction
void push(int x);
int pop();
void display();
};
void stack::push(int x)
{
node *t = new node;
if( t==nullptr ) cout << "stack is full" << '\n';
else
{
t->data = x;
t->next = top;
top = t;
}
}
int stack::pop()
{
if(top == nullptr)
{
cout << "stack is empty" << '\n';
}
else
{
int x = top->data;
node *p = new node;
p = top;
top = top->next;
delete p;
return x;
}
}
void stack::display()
{
node *p = top;
while(p!=nullptr)
{
cout << p->data << " ";
p = p->next;
}
cout << '\n';
}
int main()
{
stack st;
st.push(10);
st.push(1);
st.push(0);
st.push(110);
st.push(100);
st.display();
cout << st.pop();
}

// GPS READING
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main() {
string gps_code =
"$GPGGA,182431.000,3453.1256,N,06939.7292,W,1,06,1.5,119.1,M,-
34.0,M,,*6E";

// Split the GPS code into individual fields


stringstream ss(gps_code);
string field;
vector<string> fields;
while (getline(ss, field, ',')) {
fields.push_back(field);
}

// Extract fields from the split vector


string time = fields[1];
string latitude = fields[2];
string latitude_dir = fields[3];
string longitude = fields[4];
string longitude_dir = fields[5];
string fix_status = fields[6];
string num_satellites = fields[7];
string hdop = fields[8];
string altitude = fields[9];
string altitude_units = fields[10];
string geoid_height = fields[11];
string geoid_height_units = fields[12];

// Output the extracted fields


cout << "Time: " << time << endl;
cout << "Latitude: " << latitude << " " << latitude_dir << endl;
cout << "Longitude: " << longitude << " " << longitude_dir <<
endl;
cout << "Fix status: " << fix_status << endl;
cout << "Number of satellites: " << num_satellites << endl;
cout << "HDOP: " << hdop << endl;
cout << "Altitude: " << altitude << " " << altitude_units << endl;
cout << "Geoid height: " << geoid_height << " " <<
geoid_height_units << endl;

return 0;
}

// MAILBOX USING SINGLY LINKED LIST


#include <iostream>
#include <string>
#include "list"
using namespace std;

struct MessageItem
{
string subject;
string content;
MessageItem *pNext;
};

struct MessageList
{
MessageItem *pHead;
};
void initMessageList(MessageList &l);
void addMessage(MessageList &l,const string &sj,const string &ct);
void removeMessageBySubject(MessageList &l,const string &sj);
void removeAllMessages(MessageList &l);
int main()
{
MessageList myMailBox;
initMessageList(myMailBox);
addMessage(myMailBox,"Hi"," my friend!");
addMessage(myMailBox,"Test","Test my mailbox");
addMessage(myMailBox,"Lecture Notes","Programming Techniques");
removeMessageBySubject(myMailBox,"Test");
MessageItem* pItem = myMailBox.pHead;
while (pItem != 0)
{
cout << pItem->subject << ":" << pItem->content << '\n';
pItem = pItem->pNext;
}
removeAllMessages(myMailBox);
}
void initMessageList(MessageList &l)
{
l.pHead = 0;
}
void addMessage(MessageList &l,const string &sj,const string &ct)
{
MessageItem *pItem = new MessageItem;
pItem->subject = sj;
pItem->content = ct;
pItem->pNext = l.pHead;
l.pHead = pItem;
}
void removeMessageBySubject(MessageList &l,const string &sj)
{
MessageItem *pItem = l.pHead;
MessageItem *pItemBefore;
while(pItem != NULL)
{
if(pItem->subject != sj)
{
pItemBefore = pItem;
pItem = pItem->pNext;
}
else
{
pItemBefore->pNext = pItem->pNext;
break;
}
}
l.pHead = pItemBefore;
}
void removeAllMessages(MessageList &l)
{
MessageItem *pItem = l.pHead;
while(pItem != NULL)
{
MessageItem *pItemNext;
pItemNext = pItem->pNext;
delete pItem;
pItem = pItemNext;
}
l.pHead = NULL;
}

// SIGLY LINKED LIST


#include <iostream>
#include <unordered_set>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};

class LinkedList {
public:
Node* head;

LinkedList() {
head = NULL;
}

void insertAtBeginning(int val) {


Node* newNode = new Node(val);
newNode->next = head;
head = newNode;
}

void insertAtEnd(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
}
else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

void insertAtPosition(int val, int pos) {


Node* newNode = new Node(val);
if (pos == 1) {
newNode->next = head;
head = newNode;
}
else {
Node* temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}

void deleteAtBeginning() {
if (head == NULL) {
cout << "List is empty\n";
}
else {
Node* temp = head;
head = head->next;
delete temp;
}
}

void deleteAtEnd() {
if (head == NULL) {
cout << "List is empty\n";
}
else if (head->next == NULL) {
delete head;
head = NULL;
}
else {
Node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
}
}

void deleteAtPosition(int pos) {


if (head == NULL) {
cout << "List is empty\n";
}
else if (pos == 1) {
Node* temp = head;
head = head->next;
delete temp;
}
else {
Node* temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
Node* temp2 = temp->next;
temp->next = temp2->next;
delete temp2;
}
}

void deleteList(Node* head)


{
Node* current = head;
Node* next;
while (current != NULL) {
next = current->next;
delete current;
current = next;
}
}

void deleteDuplicates(Node* head)


{
unordered_set<int> seen;
Node* current = head;
Node* prev = NULL;
while (current != NULL) {
if (seen.find(current->data) != seen.end()) {
prev->next = current->next;
delete current;
current = prev->next;
} else {
seen.insert(current->data);
prev = current;
current = current->next;
}
}
}

void displayList() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

Node* search(int val) {


Node* temp = head;
while (temp != NULL) {
if (temp->data == val) {
return temp;
}
temp = temp->next;
}
return NULL;
}
void reverseList() {
Node* prev = NULL;
Node* current = head;
Node* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}

void concatenateLists(Node* head1, Node* head2) {


if (head1 == NULL) {
head1 = head2;
return;
}
Node* current = head1;
while (current->next != NULL) {
current = current->next;
}
current->next = head2;
}

Node* mergeLists(Node* head1, Node* head2) {


if (head1 == NULL) {
return head2;
}
if (head2 == NULL) {
return head1;
}
Node* result = NULL;
if (head1->data <= head2->data) {
result = head1;
result->next = mergeLists(head1->next, head2);
} else {
result = head2;
result->next = mergeLists(head1, head2->next);
}
return result;
}

};

int main() {
LinkedList myList;

myList.insertAtBeginning(5);
myList.insertAtBeginning(4);
myList.insertAtBeginning(3);

myList.displayList();
}

// PARATHESIS MATCHING
#include <iostream>
#include <stack>
#include <string>
bool isBalanceParanthesis(std::string str)
{
// pc stands for paranthesis_container
std::stack<char> pc;
for(char c:str )
{
if(c == '('
|| c == '['
|| c == '{')
{
pc.push(c);
}
if(c == ')' || c == ']' || c == '}' )
{
if(pc.empty())
{
return false;
}
else if(pc.top() != c)
{
return false;
}
else
{
pc.pop();
}
}
}
if(pc.empty()) return true;
else return false;
}
int main()
{

// intial string
std::string str;
std::cout << "Nhap chuoi: " ;
std::getline(std::cin,str);
if(isBalanceParanthesis(str))
{
std::cout << "Correct!" ;
}
else std::cout << "Incorrect" ;
}

Vous aimerez peut-être aussi