Single linked list of data structure (c + + implementation)

Source: Internet
Author: User

I have long wanted to use C + + to implement those commonly used data structures.

Today is a beginning. Or that sentence, see more than to write their own hands.

Write according to your own ideas. First you have to be familiar with the characteristics of that structure before you can talk about implementation.

A linked list is a very common data structure. Support for the deletion of data at any place.

However, random access is not supported. So the complexity is a little high. There was a problem in writing.

Finally the memory was viewed several times. It's not the problem of the node that holds the data. But in the linked list transpose

The int *p = new int (length) is used. There is a problem. Because I'm used to the malloc of C,

So that's what it says. Finally found that this did not apply to the required lenght of the length of an int memory.

It would be better to change () to []. So it's only a lot of use to find more mistakes. Or stay on the theoretical level forever,

Without practice, it seems to be nonsense.

Link doesn't have much function, that's all. Of course you can expand.

GCC and vc6.0 through. A. cpp file that corresponds to a. h file is a better specification.

The function inside the. h can be written if it is very small, if it is larger, it is written in the. cpp file.

Later will land continued to implement those commonly used data structure of C + + version.

Your visits and comments are a powerful force for me to move forward. So, kiss, try to spend a few more seconds writing your questions on the keyboard.

's view.

Reprint Please indicate the source, thank you.

Link.h

the definition of the #ifndef link_h #define LINK_H//struct body
struct mylink{
	int value;	Data domain
	struct myLink *next;	Pointer field
};
Class link{
private:
	struct myLink *head;	Head pointer
	struct MyLink *cur;	The pointer int length of the current position
	;	Link's length is public
:
	link () {//default constructor head
		= cur =null;	The default link is empty
		length = 0;
	}
	Link (const link &l) {//Prohibit copy constructor
		throw "not allowed";
	}
	link& operator= (const Link &l) {//Prohibit assignment constructor throw
		' not allowed ';
	}
	void Add (int data);//add element to list, default to tail add
	int getData (int pos);//Get the element
	int remove (int pos) of POS position;//remove element from a location
	void Sort ()//To sort link
	int insert (int pos, int data);//at POS position data inserted into link
	void reverse ();// Turn
	void display () on link to display
	int modify (int pos, int data) on the element of link;//change the data int getlength in POS position
	( {//Gets the length of link
		;
	~link ();	destructor
};
#endif	

Link.cpp

#include <iostream> #include "Link.h" void link::add (int data) {struct MyLink *u = new struct myLink;
	U->value = data;
	U->next = NULL;
		The IF (NULL = head) {//Header node also stores the data heads = u;
	cur = u;
		}else {cur->next = u;	cur = u;
The current pointer changes to u} length++; int link::getdata (int pos) {if (length >= pos && length > 0) {//list non-empty and no bounds int count = 0;//count while (p
				OS!= count) {count++; 
	if (1 = count) cur = head;//header pointer becomes current pointer else cur = cur->next;//pointer after move} return cur->value;	} return-1; Other return-1} int link::remove (int pos) {if (length >= pos && length > 0) {//list non-empty and no bounds int count = 1;//Count W
			Hile (POS!= count) {//Get the first pointer to POS position count++;
		if (2 = count) cur = head;//header pointer becomes current pointer else cur = cur->next;//pointer post} struct MyLink *temp; 
			if (1 = count) {//If the first data is remove, it should be handled specifically because the header node also stores the data temp = head;
			Head = temp->next;
		Delete temp;
			}else {temp = cur->next; Cur->next = temp->next;
		Delete temp;	} length--;
	Don't forget to return the length--to 0;
		return-1;//other return-1} int link::insert (int pos, int data) {if (length >= pos && length > 0) {//list non-empty and no bounds
			int count = 1;//count while (POS!= count) {//Find the previous pointer to the current position count++; if (2 = count) cur = head;//header pointer becomes current pointer else cur = cur->next;//pointer after move} struct myLink *u = new struct Mylin
		K
		U->value = data;
			if (1 = count) {U->next = head; Head = u;//current node becomes the header node}else {u->next = cur->next;//the next node to the current node Cur->next = u;//The current node points to the new add node} le	ngth++; 
	Don't forget to return 0;
		return-1;//other return-1} void Link::sort () {if (length > 1) {int count = 0;
			while (Length-1!= count) {int i = count + 1;
			struct MyLink *temp = head;	cur = head; The current node points to the head node while (length!= i) {if (Cur->value > Cur->next->value) {cur->value = Cur->nex
					t->value; Cur->next->value = cur->value-cur->next->value;
					Cur->value-= cur->next->value; 
				} cur = cur->next;
			i++;
			\ temp = temp->next;
		count++;
		}} void Link::reverse () {//is more efficient by changing the pointer field to a higher efficiency if (length > 0) {int count = 0;	int *p = new int[length];//do not write new int (length), finally I looked for 2 hours to find out that there was a mistake//new int (length) did not apply for length int memory, cur = head;
			The current node points to the header node while (length!= count) {P[count] = (int) cur;//stores the pointer in P[i] count++;
		Cur = cur->next;
		Count = 0;	struct MyLink *temp = head;
			The secondary pointer while (length!= count) {count++;
			cur = (struct MyLink *) p[length-count];//reverse if (1 = count) {head = cur;//the last node becomes the node temp = heads; 
		else {temp->next = cur;//current node becomes the successor node of the last node temp = temp->next;//temp post} cur->next = NULL;
	delete []p;
		} int link::modify (int pos, int data) {if (length >= pos && length > 0) {//list non-empty and no bounds int count = 0;//count
			while (POS!= count) {count++; if (1 = count) cur = head;//header pointer becomes current pointer
			else cur = cur->next;//pointer moves after} cur->value = data; 
	return 0; return-1;//other return-1} void Link::d isplay () {if (length > 0) {std::cout<< ' data in the ' link is: "<<std::e
		Ndl
		int count = 0;	cur = head;
			The current node points to the header node while (length!= count) {count++;
			std::cout<< "The" <<count<< "Data is" <<cur->value<<std::endl;
		Cur = cur->next; }} link::~link () {if (NULL!= head) {cur = heads;//start of Delete while (0!= length) {header = cur->next;//Headers
			After the point move delete cur;
			cur = head;
		length--; }
	}
}



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.