雙向鏈表插入刪除基本應(yīng)用介紹
更新時(shí)間:2012年11月23日 16:05:27 作者:
本文將詳細(xì)介紹建立雙向鏈表,實(shí)現(xiàn)對(duì)雙向鏈表的插入,刪除操作,需要了解的朋友可以參考下
雙鏈表其實(shí) 也沒什么 只是多了一個(gè)前置鏈而已
雙鏈表的定義
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);
}
雙鏈表的定義
復(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)文章
C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
C++類與對(duì)象的基礎(chǔ)知識(shí)點(diǎn)詳細(xì)分析
類和對(duì)象是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語(yǔ)言的合稱。對(duì)象是對(duì)客觀事物的抽象,類是對(duì)對(duì)象的抽象。類是一種抽象的數(shù)據(jù)類型;變量就是可以變化的量,存儲(chǔ)在內(nèi)存中—個(gè)可以擁有在某個(gè)范圍內(nèi)的可變存儲(chǔ)區(qū)域2023-02-02
C++11關(guān)于auto關(guān)鍵字的使用示例
今天小編就為大家分享一篇關(guān)于C++11關(guān)于auto關(guān)鍵字的使用示例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
C語(yǔ)言深入探究動(dòng)態(tài)規(guī)劃之線性DP
線性動(dòng)態(tài)規(guī)劃,是較常見的一類動(dòng)態(tài)規(guī)劃問(wèn)題,其是在線性結(jié)構(gòu)上進(jìn)行狀態(tài)轉(zhuǎn)移,這類問(wèn)題不像背包問(wèn)題、區(qū)間DP等有固定的模板,線性動(dòng)態(tài)規(guī)劃的目標(biāo)函數(shù)為特定變量的線性函數(shù),約束是這些變量的線性不等式或等式,目的是求目標(biāo)函數(shù)的最大值或最小值2022-04-04
C++深入探索內(nèi)聯(lián)函數(shù)inline與auto關(guān)鍵字的使用
本篇文章主要包括內(nèi)聯(lián)函數(shù)和auto關(guān)鍵字。其中,內(nèi)斂函數(shù)包括概念,特性等;auto關(guān)鍵字的使用規(guī)則,使用場(chǎng)景等,接下來(lái)讓我們深入了解2022-05-05

