亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

雙向鏈表插入刪除基本應用介紹

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

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

單鏈表的定義
復制代碼 代碼如下:

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

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

#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;
}

復制代碼 代碼如下:

#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);
}

相關文章

  • C++的QT項目打包成獨立可執(zhí)行和發(fā)布的exe文件(項目構建過程)

    C++的QT項目打包成獨立可執(zhí)行和發(fā)布的exe文件(項目構建過程)

    這篇文章主要介紹了C++的QT項目打包成獨立可執(zhí)行和發(fā)布的exe文件(項目構建過程),本文通過實例圖文相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-11-11
  • MFC程序對文件的處理方法

    MFC程序對文件的處理方法

    這篇文章主要介紹了MFC程序對文件的處理方法,需要的朋友可以參考下
    2014-08-08
  • 淺談QT打包的兩種方式

    淺談QT打包的兩種方式

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

    STL中的string你了解嗎

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

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

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

    C++ 指向類成員的指針

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

    使用c語言輸出楊輝三角形的簡單方法

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

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

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

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

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

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

    這篇文章主要通過一些簡單的示例為大家詳細介紹一下C/C++中new?A與new?A()的區(qū)別,文中的示例代碼簡潔易懂,快跟隨小編一起學習起來吧
    2023-07-07

最新評論