C + + Learning Note _c++ Implementation of doubly linked list

Source: Internet
Author: User

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "conio.h"
using namespace Std;

struct _double_link_node//define a doubly linked list structure
{
char * data;
struct _double_link_node* prev; The precursor of a doubly linked list
Rear drive of struct _double_link_node* next;//doubly linked list
} ;

Class Doublelist
{
Private
Public
struct _double_link_node * Head;
Doublelist ()
{
Head = (struct _double_link_node *) malloc (sizeof (_double_link_node));
memset (Head, 0, sizeof (_double_link_node));
}
~doublelist ()
{
}
struct _double_link_node * Create_double_link_node (char * value)
{
struct _double_link_node* pnode;
Pnode = (_double_link_node *) malloc (sizeof (_double_link_node));
memset (pnode,0,sizeof (_double_link_node));
Pnode->data= (char *) malloc (strlen (value) +1);
memset (Pnode->data,0,strlen (value) +1);
memcpy (Pnode->data,value,strlen (value));

pnode->prev= (struct _double_link_node *) malloc (sizeof (_double_link_node));
memset (pnode->prev,0,sizeof (_double_link_node));
memcpy (pnode->prev,pnode,sizeof (_double_link_node));
return pnode;
}

_double_link_node* Find_data_in_double_link (char * data)
{
_double_link_node* pnode = Head;
int Count=count_number_in_double_link ();
For (;p node && pnode->next;pnode = Pnode->next)
if (data==null)
{
return NULL;
}
Else
{
for (int i=0;i<count;i++)
{
if (Pnode->data && strcmp (pnode->data, data) = = 0)
{
return pnode;
}
Else
{
Pnode = pnode->next;
}
}
}
return NULL;
}

BOOL Insert_data_into_double_link (_double_link_node *node,char * data)
{
_double_link_node * PNODE=HEAD;
_double_link_node * findnode= NULL;
int Count=count_number_in_double_link ();
if (Find_data_in_double_link (data)!=null)
{
Findnode=find_data_in_double_link (data);
}
Else
{
for (int i=0;i<count;i++)
{
if (pnode->next==null)
{
Findnode=pnode;
}
Else
{
Pnode = pnode->next;
}
}
}
if (pnode->data==null && pnode->next ==null)
{
pnode->next=node->prev;
node->prev=pnode;
}
Else
{
if (findnode->next==null)
{
pnode->next=node->prev;
node->prev=pnode;
}
Else
{
node->next=findnode->next->prev;
findnode->next=node;
node->prev=findnode->prev;
node->next->prev=node;
}
}
return true;
}


BOOL Delete_data_from_double_link (char * data)
{
_double_link_node* Pnode;
Pnode=find_data_in_double_link (data);

*pnode->next->prev = *pnode->prev;
if (pnode->next!=null)
{
pnode->next->prev=pnode->prev;
Pnode->prev->next = pnode->next;
}
Else
{
Pnode->prev->next = pnode->next;
}
Free (pnode);
return true;
}

void Print_double_link_node ()
{
_double_link_node *doublelist =head;
while (NULL! = doublelist) {
printf ("%s\n", doublelist->data);
Doublelist = Doublelist->next;
}
}

    int Count_number_in_double_link ()
    {
         int count = 0;
        _double_link_node *doublelist =head;

        while (NULL! = doublelist) {
             count + +;
            doublelist = doublelist->next;
       }
        return count;
   }
};

int _tmain (int argc, _tchar* argv[])
{
    doublelist *list=new doublelist ();
    char * str= "Hello word! Hello ~ ~ ";
    char * dd= "JSGW";
    _double_link_node *node= list->create_double_link_node (str);
    _double_link_node * Node1=list->create_double_link_node (DD);
    List->insert_data_into_double_link (node,null);
    List->insert_data_into_double_link (node1,null);
    node= list->create_double_link_node ("Hello world!");

List->insert_data_into_double_link (node, "ADF");

int D=list->count_number_in_double_link ();
List->print_double_link_node ();
printf ("The list has%d data \ n", d);

printf ("Delete data:");
char * str1= "Hello world!";

int A;
cin>>a;
if (a==0)
{
List->delete_data_from_double_link (STR1);
List->print_double_link_node ();
}
Cin.get ();
}

This article from the "11413038" blog, reproduced please contact the author!

C + + Learning Note _c++ Implementation of doubly linked list

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.