Φροντιςτήριο Linked-List 1
Linked List Μια linked list είναι μια ακολουθία από ςυνδεδεμένουσ κόμβουσ Κάθε κόμβοσ περιέχει τουλάχιςτον Μια πληροφορία (ή ένα struct) Δείκτη ςτον επόμενο κόμβο τησ λίςτασ Head : δείκτησ ςτο πρώτο κόμβο Ο τελευταίοσ κόμβοσ δείχνει ςτο null 2
Linked List Linkedlist Node { data // Οι πληροφορίεσ που αποθηκεύουμε ςτον κόμβο next //Αναφορά ςτον επόμενο κόμβο } 3
Οριςμόσ κόμβου typedef struct node { int data; // will store information node *next; // the reference to the next node }; node A data pointer 4
Ειςαγωγή κόμβου ςτην αρχή node *head = NULL; node *temp; //create a temporary node temp = (node*)malloc(sizeof(node)); //allocate space for node --------------------------------------------------------- temp->data = info; // store data(first field) temp->next=head; // store the address of the pointer head(second field) head = temp; // transfer the address of 'temp' to 'head' 5
Ειςαγωγή ςτο τέλοσ node *temp1; // create a temporary node temp1=(node*)malloc(sizeof(node)); // allocate space for node temp1 = head; // transfer the address of 'head' to 'temp1' while(temp1->next!=null) // go to the last node temp1 = temp1->next; //tranfer the address of 'temp1->next' to 'temp1 node *temp; // create a temporary node temp = (node*)malloc(sizeof(node)); // allocate space for node temp->data = info; // store data(first field) temp->next = NULL; // second field will be null(last node) temp1->next = temp; // 'temp' node will be the last node 6
Διάςχιςη Λίςτασ(Print) while( temp1!=null ) { cout<< temp1->data<<" "; // show the data in the linked list temp1 = temp1->next; //tranfer the address of 'temp->next' to 'temp } 7
Διαγραφή ςτην αρχή node *temp; // create a temporary node temp = (node*)malloc(sizeof(node)); // allocate space for node temp = head; // transfer the address of 'head' to 'temp' head = temp->next; // transfer the address of 'temp->next' to 'head free(temp); 8
Διαγραφή ςτο τέλοσ // create a temporary node node *temp1; temp1 = (node*)malloc(sizeof(node)); // allocate space for node temp1 = head; //transfer the address of head to temp1 node *old_temp; // create a temporary node old_temp = (node*)malloc(sizeof(node)); // allocate space for node while(temp1->next!=null) // go to the last node { old_temp =temp1; // transfer the address of 'temp1' to 'old_temp' temp1 = temp1->next; // transfer the address of 'temp1->next' to 'temp1' } old_temp->next = NULL; // previous node of the last node is null free(temp1); 9
Διαγραφή ςυγκεκριμένου κόμβου node *temp1; // create a temporary node temp1 = (node*)malloc(sizeof(node)); // allocate space for node temp1 = head; // transfer the address of 'head' to 'temp1' node *old_temp; // create a temporary node old_temp =(node*)malloc(sizeof(node)); // allocate space for node old_temp = temp1; // transfer the address of 'temp1' to 'old_temp' 10
Συνέχεια cout<<"enter THE NODE NUMBER:"; cin>>node_number; // take location for( int i = 1 ; i < node_number ; i++ ) { old_temp = temp1; // store previous node temp1 = temp1->next; // store current node } old_temp->next = temp1->next; // transfer the address of 'temp1->next' to 'old_temp->next' free(temp1); 11
Sort Linked List node *temp1; // create a temporary node temp1 = (node*)malloc(sizeof(node)); // allocate space for node node *temp2; // create a temporary node temp2 = (node*)malloc(sizeof(node)); // allocate space for node int temp = 0; // store temporary data value for( temp1 = head ; temp1!=null ; temp1 = temp1->next ) { for( temp2 = temp1->next ; temp2!=null ; temp2 = temp2->next ) { if( temp1->data > temp2->data ) { temp = temp1->data; temp1->data = temp2->data; temp2->data = temp; } } } 12
Διάβαςμα αρχείου #include <iostream> #include <fstream> #include <string> using namespace std; int main() { c infile( test.txt ); if (infile.fail()) { cerr << "Unable to open file for reading." << endl; exit(1); } string nexttoken; while (infile >> nexttoken) { cout << "Token: " << nexttoken << endl; } infile.close(); return 0; } 13
int main ( int argc, char *argv[] ) #include <fstream> #include <iostream> using namespace std; int main ( int argc, char *argv[] ) { if ( argc!= 2 ) // argc should be 2 for correct execution // We print argv[0] assuming it is the program name cout<<"usage: "<< argv[0] <<" <filename>\n"; else { // We assume argv[1] is a filename to open ifstream the_file ( argv[1] ); // Always check to see if file opening succeeded if (!the_file.is_open() ) cout<<"could not open file\n"; else { char x; // the_file.get ( x ) returns false if the end of the file // is reached or an error occurs while ( the_file.get ( x ) ) cout<< x; } // the_file is closed implicitly here } } 14
Tέλοσ 15