#include <iostream>
#include <iomanip>
using namespace std;
typedef struct DuLNode
{
int data;
struct DuLNode* prior;
struct DuLNode* next;
}DuLNode,*DuLinkList;
DuLinkList Creat_Dulinklist()
{
DuLinkList L=(DuLinkList)malloc(sizeof(DuLNode));
DuLinkList p=L;//用於指向當前鏈表的最後一個節點
if(NULL==L)
{
cout<<"Failed to malloc a new DuLNode!"<<endl;
}
//對頭結點進行初始化
L->next=NULL;
L->prior=NULL;
int cycle=1;//用於檢測數據輸入是否結束
while (cycle)
{
cout<<"請輸入數據(0作爲停止符):"<<endl;
int data;//用於接收接盤輸入的數據
cin>>data;
if (0!=data)
{
DuLinkList s=(DuLinkList)malloc(sizeof(DuLNode));//用於接收新插入的節點
s->data=data;
s->next=NULL;
//按順序插入數據
p->next=s;
s->prior=p;
p=s;
}
else
{
cycle=0;
}
}
return L;
}
void Print_DuLinklist(DuLinkList L)
{
if(NULL==L)
{
cout<<"There is no node!"<<endl;
}
if (NULL==L->next)
{
cout<<"The linklist is empty!"<<endl;
}
DuLinkList temp;
cout<<"雙向鏈表的打印:"<<endl;
for (temp=L->next;NULL!=temp->next;temp=temp->next)
{
cout<<setw(3)<<temp->data<<"->";
}
cout<<setw(3)<<temp->data;
cout<<endl;
}
int main()
{
DuLinkList L=Creat_Dulinklist();
Print_DuLinklist(L);
return 0;
}
雙向鏈表的建立
面試寶典172頁面試例題1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.