#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "conio.h"
using namespace std;
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)扶風(fēng)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
struct _DOUBLE_LINK_NODE //定義一個(gè)雙鏈表結(jié)構(gòu)
{
char * data;
struct _DOUBLE_LINK_NODE* prev; //雙鏈表的前驅(qū)
struct _DOUBLE_LINK_NODE* next;//雙鏈表的后驅(qū)
} ;
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(;pNode && 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!你好~~";
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("鏈表中共有%d條數(shù)據(jù)\n",d);
printf("刪除數(shù)據(jù):");
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();
}
網(wǎng)站題目:c++學(xué)習(xí)筆記_c++實(shí)現(xiàn)雙鏈表
文章URL:http://aaarwkj.com/article40/iipgeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、企業(yè)建站、網(wǎng)站維護(hù)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)