summaryrefslogtreecommitdiffstats
path: root/3.01.singlelinkedList.cc
blob: 73f765b1dfebd3ca19cc3a5052e312df9884c5e4 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * =====================================================================================
 *
 *       Filename:  3.01.singlelinkedList.cc
 *
 *    Description:  Single linked list
 *
 *        Version:  1.0
 *        Created:  23.04.2014 12:13:13
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de
 *   Organization:  FH Südwestfalen, Iserlohn
 *
 * =====================================================================================
 */

#include	<cstdlib>
#include 	<iostream>
#include	<iomanip>

using namespace std;

struct Element{
	long key;				// Schluessel des Listenelements
	long info;				// die im Listenelement zu verwaltende Information
	struct Element *next;	// Zeiger auf nächste Element
};

// ===  FUNCTION  ======================================================================
//         Name:  main
//  Description:  Simple program no functions needed
// =====================================================================================
int main ( int argc, char *argv[] ){
	// Create pointer for start and search
	struct Element* L = NULL, *cursor = NULL;

	// Create actual elements
	struct Element x1, x2, x3, x4;

	// Initialize structs
	x1.key  = 1;
	x1.info = 11;
	x2.key  = 2;
	x2.info = 22;
	x3.key  = 3;
	x3.info = 33;
	x4.key  = 4;
	x4.info = 44;

	// Now create the linked list
	L       = &x1;
	x1.next = &x2;
	x2.next = &x3;
	x3.next = &x4;
	x4.next = NULL;

	// Set cursor to starting point
	cursor = L;

	// Go over the List until the list ends or the key is found
	while(cursor != NULL && cursor->key != 3){
		cursor = cursor->next;
	}

	// Check if value was found
	if(cursor != NULL){
		cout << "key   = " << setw(15) << cursor->key  << endl
			 << "info  = " << setw(15) << cursor->info << endl
			 << "next  = " << setw(15) << cursor->next << endl;
	}
	else{
		cout << "Value not found!!!";
	}

	return EXIT_SUCCESS;
}		// ----------  end of function main  ----------