欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

雙向鏈表插入刪除基本應(yīng)用介紹

 更新時(shí)間:2012年11月23日 16:05:27   作者:  
本文將詳細(xì)介紹建立雙向鏈表,實(shí)現(xiàn)對(duì)雙向鏈表的插入,刪除操作,需要了解的朋友可以參考下
雙鏈表其實(shí) 也沒什么 只是多了一個(gè)前置鏈而已
雙鏈表的定義
復(fù)制代碼 代碼如下:

struct DNode
{
int data;
struct DNode *next;
struct DNode *pre;
};

單鏈表的定義
復(fù)制代碼 代碼如下:

view plaincopy
struct DNode
{
int data;
struct DNode *next;
};

其他的可以看上一篇博客 大致相同
復(fù)制代碼 代碼如下:

#ifndef HEAD_H
#define HEAD_H
#include <iostream>
using namespace std;
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <fstream>
#include <string>
#include <algorithm>
#include <list>
#include <queue>
#include <vector>
#include <deque>
#include <stack>
#include <bitset>
#include <set>
#include <map>
#endif
struct DNode
{
int data;
struct DNode *next;
struct DNode *pre;
};
DNode *Creat()

DNode *head,*p,*s;
head=(DNode *)malloc(sizeof(DNode));
p=head;
int temp;
while (cin>>temp&&temp)
{
s=(DNode *)malloc(sizeof(DNode));
s->data=temp;
p->next=s;
s->pre=p;
p=s;
}
head=head->next;
p->next=NULL;
head->pre=NULL;
return (head);
}
DNode *Insert(DNode *&head,int num)
{
DNode *p,*s;
s=(DNode *)malloc(sizeof(DNode));
s->data=num;
p=head;
while (NULL!=p->next&&num>p->data)
{
p=p->next;
}
if (num<=p->data)
{
if (NULL==p->pre)
{
s->next=head;
head->pre=s;
head=s;
head->pre=NULL;
}
else
{
s->pre=p->pre;
p->pre->next=s;
s->next=p;
p->pre=s;
}
}
else
{
p->next=s;
s->pre=p;
s->next=NULL;
}
return(head);
}
DNode *Del(DNode *&head,int num)
{
DNode *p;
p=head;
while (NULL!=p->next&&num!=p->data)
{
p=p->next;
}
if (num==p->data)
{
if (NULL==p->pre)
{
head=p->next;
p->next->pre=head;
free(p);
}
else if (NULL==p->next)
{
p->pre->next=NULL;
free(p);
}
else
{
p->pre->next=p->next;
p->next->pre=p->pre;
free(p);
}
}
else
{
cout<<num<<" cound not be found"<<endl;
}
return head;
}
void Display(DNode *head)
{
DNode *p;
p=head;
while (NULL!=p)
{
cout<<(p->data)<<" ";
p=p->next;
}
cout<<endl;
}

復(fù)制代碼 代碼如下:

#include "head.h"
int main()
{
DNode *head;
head=Creat();
Display(head);
#ifndef DEBUG
cout<<"please input an num to insert:";
#endif
int insert_num;
while (cin>>insert_num&&insert_num)
{
Insert(head,insert_num);
Display(head);
}
#ifndef DEBUG
cout<<"please input an number to delete:";
#endif
int delete_num;
while (cin>>delete_num&&delete_num)
{
Del(head,delete_num);
Display(head);
}
return (EXIT_SUCCESS);
}

相關(guān)文章

  • 淺談QT打包的兩種方式

    淺談QT打包的兩種方式

    本文主要介紹了淺談QT打包的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • STL中的string你了解嗎

    STL中的string你了解嗎

    這篇文章主要為大家詳細(xì)介紹了STL中的string,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C語言基于單鏈表實(shí)現(xiàn)通訊錄功能

    C語言基于單鏈表實(shí)現(xiàn)通訊錄功能

    這篇文章主要為大家詳細(xì)介紹了C語言基于單鏈表實(shí)現(xiàn)通訊錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • C++ 指向類成員的指針

    C++ 指向類成員的指針

    指向類成員的指針總的來講可以分為兩大類四小類(指向數(shù)據(jù)成員還是成員函數(shù),指向普通成員還是靜態(tài)成員)
    2020-03-03
  • 使用c語言輸出楊輝三角形的簡(jiǎn)單方法

    使用c語言輸出楊輝三角形的簡(jiǎn)單方法

    這篇文章主要給大家介紹了關(guān)于如何使用c語言輸出楊輝三角形的簡(jiǎn)單方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • C語言一篇精通鏈表的各種操作

    C語言一篇精通鏈表的各種操作

    鏈表是一種常見的重要的數(shù)據(jù)結(jié)構(gòu)。它是動(dòng)態(tài)地進(jìn)行存儲(chǔ)分配的一種結(jié)構(gòu),是根據(jù)需要開辟內(nèi)存單元,鏈表這種數(shù)據(jù)結(jié)構(gòu),必須利用指針變量才能實(shí)現(xiàn),即一個(gè)結(jié)點(diǎn)中應(yīng)包含一個(gè)指針變量,用它存放下一結(jié)點(diǎn)的地址
    2022-04-04
  • 深入探討:宏、內(nèi)聯(lián)函數(shù)與普通函數(shù)的區(qū)別

    深入探討:宏、內(nèi)聯(lián)函數(shù)與普通函數(shù)的區(qū)別

    本篇文章是對(duì)宏、內(nèi)聯(lián)函數(shù)與普通函數(shù)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 詳解C/C++中new?A與new?A()的區(qū)別

    詳解C/C++中new?A與new?A()的區(qū)別

    這篇文章主要通過一些簡(jiǎn)單的示例為大家詳細(xì)介紹一下C/C++中new?A與new?A()的區(qū)別,文中的示例代碼簡(jiǎn)潔易懂,快跟隨小編一起學(xué)習(xí)起來吧
    2023-07-07
  • 最新評(píng)論