91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

c++學習筆記_c++實現雙鏈表

發布時間:2020-06-11 14:39:36 來源:網絡 閱讀:419 作者:smile_晶 欄目:編程語言

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

struct _DOUBLE_LINK_NODE  //定義一個雙鏈表結構
{
    char * data;
    struct _DOUBLE_LINK_NODE* prev; //雙鏈表的前驅
    struct _DOUBLE_LINK_NODE* next;//雙鏈表的后驅
} ; 

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條數據\n",d);

    printf("刪除數據:");
    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();
}

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

天峻县| 交口县| 新龙县| 阳春市| 长春市| 柘城县| 苏尼特右旗| 都匀市| 商水县| 宁波市| 商南县| 永顺县| 威海市| 乐至县| 景东| 湖北省| 迭部县| 肥乡县| 夏津县| 信丰县| 谷城县| 肥西县| 石城县| 东海县| 滕州市| 四子王旗| 修水县| 二连浩特市| 塔河县| 门源| 霸州市| 科技| 荃湾区| 偃师市| 曲阳县| 株洲县| 和田市| 合山市| 温泉县| 齐齐哈尔市| 陆丰市|