Collections

  • Themenstarter Themenstarter colt4ever
  • Beginndatum Beginndatum
C

colt4ever

Hallo,

Ich hätte da mal so eine Frage. Könntet Ihr (wenn es Euch nicht so viel ausmacht) ein paar Collections für Anfänger hier reinposten?
Hab gerade erfahren (vor 2 Tagen), dass es solche gibt.
Es wäre nett (von Eucht) wenn es der Binärbaum, Lineare Liste, Array Collection, AVL-Baum Collection, ... ist.
Aber nicht die vom System (wenn es geht).

Danke vorerst einmal
:suspekt:
 
Also, freut mich, dass es noch jemanden gibt, der solche dinge braucht *hihi*

Ne war n joke... also hier hast mal die lineare Liste (war ne 10 min Arbeit) objektorientiert

Das wäre mal die Header-Datei
Code:
#pragma once

class Linear_List
{
	struct node{
		void *pInfo;
		node *pNext;
	};

	node *base;

public:
	Linear_List(void);
	~Linear_List(void);
	Linear_List(Linear_List & list);
	// this function inserts any type of information into the linear list
	void * Insert(void * pInfo, int (*cmp)(void *first, void *second));
	
	void Walk(void (*action)(void *p));
	void Del(node * base);
};


Und das ist der Source (.cpp)
Code:
#include ".\linear_list.h"
#include <stdio.h>
#include <stdlib.h>

Linear_List::Linear_List(void)
{
	base = NULL;
}

Linear_List::~Linear_List(void)
{
	Del(base);
}

Linear_List::Linear_List(Linear_List & list)
{
	
}

// this function inserts any type of information into the linear list
void * Linear_List::Insert(void * pInfo, int (*cmp)(void *first, void *second))
{
	node *prev = (node *)base;
	node *after = NULL;
	node *buffer = NULL;

	while(prev != NULL && cmp(prev->pInfo, pInfo) < 0)
	{
		after = prev;
		prev = prev->pNext;
	}

	buffer = (node *)malloc(sizeof(node));

	if(buffer != NULL)
	{
		buffer->pInfo = pInfo;
		buffer->pNext = prev;

		if(after == NULL)
		{
			(node *)base = buffer;
		}

		else
		{
			after->pNext = buffer;
		}
	}

	return buffer;
}

void Linear_List::Walk(void (*action)(void *p))
{
	node *temp = NULL;
	temp = base;
	
	while(temp != NULL)
	{
		action(temp->pInfo);
		temp = temp->pNext;
	}
}

void Linear_List::Del(node * base)
{
	if(base->pNext != NULL)
	{
		Del(base->pNext);
	}

	free(base);
}

Probier's mal aus!
 
He, Mann... du möchtest durch mich deine Note in Programmieren verbessern, oder wie? Schick mir die e-mail-Adresse von deinem Professor. Dann kann ich ihm den Code direkt posten *gg*

Der Binärbaum (Headerdatei .h)
Code:
#pragma once
#include <stdio.h>


enum ORDER {preorder, inorder, postorder};

class Binary_Tree{

	struct roots{
		roots *left;
		void *pInfo;
		roots *right;
		int flag;

		roots()
		{
			left = NULL;
			right = NULL;
		}
	};

	roots *root;

	public:
		Binary_Tree() {root = NULL;}
		~Binary_Tree(){Del(root);}

		void *Tree_Insert(void *pInfo, int(* cmp)(void *first, void *second));
		void Tree_Walk(void *root, ORDER, void (*action)(void *p));
		void Del(void *root);
		void *GetRoot();
		void *Delete(void *root, void *key, int(* cmp)(void *first, void *second), void (*del)(void *del_el));

};

und der Source (.cpp)
Code:
#include <stdio.h>
#include <stdlib.h>
#include "Collection.h"

void * Binary_Tree::Tree_Insert(void *pInfo, int (* cmp)(void *first, void *second))
{
	int iCheck = 0;
	roots *newroot = new roots;
	roots *Z1 = root, *Z2 = NULL;
	
	if(newroot != NULL)
	{
		newroot->pInfo = pInfo;

		
		if(root == NULL)
		{
			root = newroot;
		}

		else
		{
			while(Z1)
			{
				if(cmp(Z1->pInfo, pInfo) >= 0)
				{
					Z2 = Z1;
					Z1 = Z1->left;
				}

				else
				{
					Z2 = Z1;
					Z1 = Z1->right;
				}
			}

			if(cmp(Z2->pInfo, pInfo) >= 0)
			{
				Z2->left = newroot;
			}

			else
			{
				Z2->right = newroot;
			}
		}

		newroot->flag = 1;
	}
	
	else
	{
		printf("Not added!");
	}
	return root;
	
}

void Binary_Tree::Tree_Walk(void *root, ORDER o, void (*action)(void *p))
{
	switch(o)
	{
	case preorder:	if(root != NULL)
					{	
						if((int)((roots *)root)->flag != 0)
						{
							action(((roots *)root)->pInfo);
						}
						Tree_Walk(((roots *)root)->left, o, action);
						Tree_Walk(((roots *)root)->right, o, action);
					}
					break;

	case inorder:	if(root != NULL)
					{
						Tree_Walk(((roots *)root)->left, o, action);
						if((int)((roots *)root)->flag != 0)
						{
							action(((roots *)root)->pInfo);
						}
						Tree_Walk(((roots *)root)->right, o, action);
					}
					break;

	case postorder:	if(root != NULL)
					{
						Tree_Walk(((roots *)root)->left, o, action);
						Tree_Walk(((roots *)root)->right, o, action);
						if((int)((roots *)root)->flag != 0)
						{
							action(((roots *)root)->pInfo);
						}
					}
					break;

	default:	printf("\nNo valid choice!");
	}
}

void * Binary_Tree::GetRoot()
{
	return root;
}

void Binary_Tree::Del(void *root)
{
	if(root != NULL)
	{
		Del(((roots *)root)->left);
		Del(((roots *)root)->right);
		
		//delete infos
		if(((roots *)root)->flag != 0)
		{
			free(((roots *)root)->pInfo);
		}
		
		delete root;
	}
}

void *Binary_Tree::Delete(void *root, void *key, int(* cmp)(void *first, void *second), void (*del)(void *del_el))
{
	int iCheck = 0;
	void *help = NULL;
	
	if(root != NULL)
	{
		help = ((roots *)root)->pInfo;
		iCheck = cmp(((roots *)root)->pInfo, key);

		if(iCheck != 0)
		{
			if(iCheck < 0)
			{
				help = Delete(((roots *)root)->right, key, cmp, del);
			}

			else
			{
				help = Delete(((roots *)root)->left, key, cmp, del);
			}
		}

		else
		{
			if(help != NULL)
			{
				del(help);
				((roots *)root)->flag = 0;
			}
		}
	}

	return help;
}

Und damit das ganze läuft brauchst noch die Stringklasse.
String-Header (.h)
Code:
#pragma once

class String{
	char *str;   //the pointer to store the adress of a text

public:
	String();
	/* 
	 * first constructor:
	 * initialises the variable str with NULL
	 */
	String(char *);
	/*
	 * second constructor:
	 * initialises the variable str with the adress of a text
	 */
	String(const String &);
	/*
	 * third constructor:
	 * initialises the variable str with an other object of this class
	 */
	~String();
	/*
	 *
	 */
	String operator = (const String &);
	/*
	 * = operator
	 * copies all values from an object into the actual object
	 */
	String operator + (const String &);
	/*
	 * + operator
	 * concatanates one string (object) to another
	 */
	bool operator < (const String &);
	/*
	 *
	 */
	bool operator > (const String &);
	/*
	 *
	 */
	bool operator == (const String &);
	/*
	 *
	 */
	operator char *();
	/*
	 * first cast-operator (char *)
	 * casts an object to char *
	 */
	operator unsigned char *();
	/*
	 * second cast-operator (unsigned char *)
	 * casts an object to unsigned char *
	 */
	char & operator [] (int n);
	/*
	 * index-operator
	 * allows to access onto the object like on an array
	 */
};

String-Source (.cpp)
Code:
#include "String.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Exception.h"

String::String()
{
	str = NULL;
}

String::String(char *text)
{
	if(text)
	{
		str = strdup(text);
	}

	else
	{
		str = NULL;
	}
}

String::String(const String & s)
{
	if(s.str)
	{
		str = strdup(s.str);
	}

	else
	{
		str = NULL;
	}
}

String::~String()
{
	if(str)
	{
		free(str);
	}
}

String String::operator = (const String & s)
{
	if(s.str)
	{
		str = (char *)realloc(str, strlen(s.str) + 1);

		if(str)
		{
			strcpy(str,s.str);
		}
	}

	return *this;
}

String String::operator +(const String & s)
{
	String temp(*this);

	if(s.str)
	{
		if(temp.str)
		{
			temp.str = (char *)realloc(temp.str, strlen(temp.str) + strlen(s.str) + 1);
			strcat(temp.str, s.str);
		}

		else
		{
			temp.str = (char *)malloc(strlen(s.str) + 1);
		}
	}

	return temp;
}

bool String::operator < (const String & s)
{
	if(strcmp(str, s.str) < 0)
	{
		return true;
	}

	else
	{
		return false;
	}
}

bool String::operator > (const String & s)
{
	if(strcmp(str, s.str) > 0)
	{
		return true;
	}

	else
	{
		return false;
	}
}

bool String::operator == (const String & s)
{
	return !strcmp(str, s.str);
}

String::operator char *()
{
	return str;
}

String::operator unsigned char *()
{
	return (unsigned char *)str;
}

char & String::operator [](int n)
{
	if(n >= (int)strlen(str) || n < 0)
	{
		throw new Exception("Indexueberlauf");
	}

	else
	{
		return str[n];
	}
}
 
He Danke

PS: Was ist das:

#include "Exception.h"

Hast du diese Auch noch?
 
Ich will ja nichts sagen, aber kannst du C++ überhaupt? Die letzten Codes die ich öffentlich stelle, sag deinen Klassenkameraden, dass so gut wie das ganze Programm online ist *gg*

Die Header
Code:
#pragma once
#include "String.h"

class Exception
{
	String message;
public:
	Exception(String);
	~Exception(void);
	void ReportError(void);
};


Der Source
Code:
#include ".\exception.h"
#include <stdio.h>


Exception::Exception(String str)
{
	message = str;
}

Exception::~Exception(void)
{
}

void Exception::ReportError(void)
{
	printf("%s", (char *)message);
	//::MessageBox(NULL, (char *)message, "", MB_OK);
}

Du weißt aber schon was eine Exception ist, oder?
 
Bin gerade dabei es herauszufinden.
Sollte ich es wissen:confused: ?

Scherz:-) :
Vor Tagen (1 oder 2) haben wir das im Unterrich durchgemacht,
(aber ich muss es erst nochmal lernen).

Ach ja, hast du vielleicht eine Array Collection auch noch?
Das wäre voll coooooooool :-) und abgefahren.

Danke schon mal im vorraus.

PS: Kennen wir uns irgendwo her? Ich glaube ich kenn diesen Programmierstiel?
 
Also, ich werde dir die Arraycollection nicht online stellen, denn schließlich sollst du ja Programmieren lernen. Solltest du jedoch Hilfe brauchen, werde ich dir helfen (einzelne Codezeilen nicht das ganze Programm!)
 
Zurück