[TUT] Linked Lists

Discussion in 'Programming General' started by KANEL, Feb 12, 2007.

[TUT] Linked Lists
  1. Unread #1 - Feb 12, 2007 at 8:48 PM
  2. KANEL
    Joined:
    Aug 10, 2005
    Posts:
    163
    Referrals:
    0
    Sythe Gold:
    0

    KANEL Active Member

    [TUT] Linked Lists

    If you have just now learned about pointers, they probably seem absolutely useless. They point to another value...yeah...well so do variables.
    BUT!
    There are some wonderful uses for these pointers!
    For one thing, they are very useful for memory management! But you can learn that once you learn about
    Code:
    new
    and
    Code:
    delete
    So what other uses can there be?
    Organizing information, of course!
    Using recursive structures, you can finally put pointers to some use! And a wonderful example of that is....pum.pum-PUM!...Linked Lists!
    "What is a linked list," you might ask!
    Well I might respond, "A Linked List is a class that make use of such things as pointers! It consists of a "root" Node (that's what I call the structure that consists of one value and one or more pointers). The pointer part of that root Node points to the next Node in the series! Here is a simple example!
    Code:
    #include <iostream>
    using namespace std;
    
    struct Node
    {
    	int value; //a value
    	Node *next; //and a pointer pointing to the next node!
    };
    
    class linkedList
    {
    public:
    	linkedList(int value);
    	void insert(int value);
    	void remove(int value);
    	bool search(int value);
    	void print();
    private:
    	Node *root;
    	void insertX(int value, Node *node);
    	void removeX(int value, Node *node);
    	void deleteNode(Node *node);
    	bool searchX(int value, Node *node);
    	void printX(Node *node);
    };
    
    linkedList::linkedList(int value)
    {
    	root = new Node;
    	root->value = value;
    	root->next = new Node;
    	root->next->value = NULL;
    };
    
    void linkedList::insertX(int value, Node *node)
    {
    	if(node->value == NULL)
    	{
    		node->value = value;
    		node->next = new Node;
    		node->next->value = NULL;
    	}
    	else
    		insertX(value, node->next);
    };
    
    void linkedList::insert(int value)
    {
    	insertX(value, root->next);
    };
    
    bool linkedList::searchX(int value, Node *node)
    {
    	if(node->value == value)
    		return true;
    	else
    	{
    		if(node->next->value != NULL)
    			return searchX(value, node->next);
    	};
    	return false;
    };
    
    bool linkedList::search(int value)
    {
    	return searchX(value, root);
    };	
    
    void linkedList::deleteNode(Node *node)
    {
    	Node *temp = new Node;
    	temp = node->next;
    	delete node;
    	deleteNode(temp);
    	delete temp;
    };
    
    void linkedList::removeX(int value, Node *node)
    {
    	if(node->value == value)
    	{
    		node->value = NULL;
    	}
    	else 
    	{
    		if(node->next->value != NULL)
    			removeX(value, node->next);
    		else 
    			deleteNode(node->next);
    	}
    };
    
    void linkedList::remove(int value)
    {
    	removeX(value, root);
    };
    
    void linkedList::printX(Node *node)
    {
    	cout << node->value << endl;
    	if(node->next->value != NULL)
    		printX(node->next);
    };
    
    void linkedList::print()
    {
    	printX(root);
    };
    
    
    int main()
    {
    	linkedList myList(5);
    
    	myList.insert(4);
    	myList.insert(7);
    	myList.insert(1);
    	
    	cout << "The list thus far:\n";
    	myList.print();
    	cout << "\n\n";
    
    	if(myList.search(1))
    		cout << "#1 found\n\n";
    	else
    		cout << "#1 not found\n\n";
    
    	myList.remove(1);
    	cout << "value of 1 removed\n\n";
    	if(myList.search(1))
    		cout << "#1 found\n\n";
    	else
    		cout << "#1 not found\n\n";
    	cout << "The List Thus Far:\n";
    	myList.print();
    
    	return 0;
    	
    };
    Here we have an organized listed of value, I made them all integers. Note that the Node contains an integer and a pointer *Node. And so every time you create a new variable Node, this structure creates the next *Node(!) into which you may continuously store information!
    Now the people trying to argue might say, "uh...ya....that's what array are for....".
    I might respond, "Well, what if you had another pointer within that node?"
    Well now you would be able to create something like a binary "tree"!
    ----A binary tree is a more organized Linked List. You can decide into which *Node the new information will be stored: *Node1 or *Node2. But instead of numbers, I would have something like *NodeHigher and *NodeLower so new information can be classified by importance or value! Binary tree are incredibly efficient because it allows one to look through sorted information rather than scattered information. If anyone wants to know, I'll explain more----
     
< WMP control | Random number >

Users viewing this thread
1 guest


 
 
Adblock breaks this site