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
| #include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
/* Link list node */
struct node
{
string data;
struct node* next;
};
void push(node** head_ref, string new_data)
{
/* allocate node */
struct node* new_node = new struct node;
// with C struct node* new_node=(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
int detectloop(struct node *list)
{
struct node *p = list, *q = list;
//p=q=head;
do{
p=p->next->next;
q=q->next;
}while (p!=q or p!=NULL);
if (p==q) { printf("Found Loop"); return 1;
}else printf("No Loop Found"); return 0;
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
ifstream fichier("test.txt", ios::in); // open text.txt
if(fichier) // if opened
{
string ligne;
while(getline(fichier, ligne)) // while it is possible to put in ligne
{
//int nmb = atoi(ligne.c_str());
push(&head, ligne);
// printf("%n",head);
cout << ligne << endl; // show
}
fichier.close();
}
else
// cerr << "Impossible d'ouvrir le fichier !" << endl;
detectloop(head);
getchar();
} |
Partager