searching for link list samples

Anyone can give me a link where there's a sample program of link list inserting? (e.g, u insert some numbers in the link list, and the link list will display the set of numbers you enter in some specific order).
I have an example which I created from my C++ book for exercise:

atom.h (declares basic class for namespace)
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once

class atom {
private:
	atom* ptrSlijedeci, *ptrPrethodni;
public:
	//geters:
	atom* getSlijedeci () {return ptrSlijedeci;}
	atom* getPrethodni () {return ptrPrethodni;}
	//seters:
	void setSlijedeci (atom* ptr) {ptrSlijedeci = ptr;}
	void setPrethodni (atom* ptr) {ptrPrethodni = ptr;}
};


lvektor.h (objects fo this class will be included to put into link list)
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include "Atom.h"

class lvektor : public atom {
private:
	float ax, ay;
public:
	lvektor (float a = 0, float b = 0) : ax (a), ay (b) {}
	void postaviXY (float x, float y) {ax = x; ay = y;}
	float dajX () {return ax;}
	float dajY () {return ay;}
};


lista.h (this is clas of linked list)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include "Atom.h"
#include <iostream>
using namespace std;

class lista
{
private:
//members:
	atom *glava, *rep;
public:
//constructors:
	lista(void);
	~lista(void);
//geters:
	atom *getGlava ();
	atom *getRep ();
//seters:
	void insert (atom* ptr, atom* izakojeg = NULL);
	void remove (atom* ptr);
};


lista.cpp (this is implemetation of the lista class)
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
#include "lista.h"


lista::lista(void) : glava (NULL), rep (NULL)
{
}
lista::~lista(void)
{
}
//geters:
atom* lista::getGlava () {
	return glava;
}
atom* lista::getRep () {
	return rep;
}
//seters:
void lista::insert (atom *ptr, atom *izakojeg) {
	if (izakojeg != NULL) {
		if (izakojeg->getSlijedeci () != NULL)
			izakojeg->getSlijedeci ()->setPrethodni (ptr);
		else
			rep = ptr;
		ptr->setSlijedeci (izakojeg->getSlijedeci ());
		izakojeg->setSlijedeci (ptr);
	}
	else {
		ptr->setSlijedeci (glava);
	if (glava != NULL)
		glava->setPrethodni (ptr);
	glava = ptr;
	}
	ptr->setPrethodni (izakojeg);
}
void lista::remove (atom* ptr) {
	if (ptr->getSlijedeci () != NULL)
		ptr->getSlijedeci()->setPrethodni (ptr->getSlijedeci ());
	else
		rep = ptr->getPrethodni ();
	if (ptr->getPrethodni () != NULL)
		ptr->getPrethodni ()->setSlijedeci (ptr->getSlijedeci ());
	else
		glava = ptr->getSlijedeci ();
}

Main.cpp (show how this work)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Atom.h"
#include "Lista.h"
#include "lvektor.h"

short main () {
	lista lis;
	lis.insert (new lvektor (24.34, 43.345));
	lis.insert (new lvektor (43.34, 3452.234));
	lis.insert (new lvektor (435.345, 435.23456));

	lvektor* superPtr = static_cast<lvektor*>(lis.getGlava ());
	while (superPtr) {
		cout << "(" << superPtr->dajX () << ", " << superPtr->dajY () << ")" << endl;
		superPtr = static_cast<lvektor*>(superPtr->getSlijedeci ());
	}
	return 0;
}


variable names are croatian words :/ this may confuse you.
Last edited on
er... the whole thing confuse me lol i just want a simple example of link list.
Last edited on
closed account (D80DSL3A)
nevermind
Last edited on
ok it's a unary link list let's say link list is 45, 23, 12,5 decending order and if i put in 34 it will become
45,34,23,12,5 that's all.
Topic archived. No new replies allowed.