Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct node {
- int val;
- node* next;
- };
- void Add(node*&, double);
- void Show(node*);
- void LoadList(node*&);
- bool isEmpty(node*&);
- void Swap(node*&);
- void bubblesort(node*&);
- int main()
- {
- node *H = NULL;
- LoadList(H);
- //Sortuj(H);
- bubblesort(H);
- Show(H);
- return 0;
- }
- void Add(node*& Head, double x) {
- node* p = new node;
- p->val = x;
- p->next = NULL;
- if (Head == NULL) {
- Head = p;
- }
- else {
- node *q = Head;
- while (q->next != NULL) {
- q = q->next;
- }
- q->next = p;
- }
- }
- void Show(node * H) {
- node* p = H;
- while (p != NULL) {
- cout << p->val << " ";
- p = p->next;
- }
- }
- void LoadList(node *&H) {
- double tmp;
- fstream plik;
- plik.open("G:\\Liczby.txt", std::ios::in | std::ios::out);
- if (plik.good() == true)
- {
- while (!plik.eof()) {
- plik >> tmp;
- Add(H, tmp);
- }
- plik.close();
- }
- }
- bool isEmpty(node *&H) {
- return(H->next == NULL);
- }
- void Swap(node *&Head) {
- node* p = Head;
- node* q = p->next;
- p->next = q->next;
- q->next = Head;
- Head = q;
- p = q;
- q = Head;
- }
- void bubblesort(node *&Head) {
- if (Head != NULL && Head->next != NULL) {
- node *p = Head;
- node *q = Head->next;
- node *PP = Head;
- node *zderzak = NULL;
- while(Head!=zderzak) {
- while (q != zderzak) {
- if (p->val > q->val) {
- if (p == Head) {
- p->next = q->next;
- q->next = Head;
- Head = q;
- p = q;
- q = p->next;
- }
- else {
- PP->next = q;
- p->next = q->next;
- q->next = p;
- q = p;
- p = PP->next;
- }
- }
- PP = p;
- PP->val;
- p = p->next;
- q = q->next;
- }
- zderzak = p;
- //cout << zderzak->val;
- p = Head;
- q = Head->next;
- PP = Head;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement