大家好,今天更新的是“ 双向循环链表 ”
首先,必须感谢 mr.chen 没有他,我还找不到这段程序错在那里
好吧,现在大家有兴趣就看看把!(代码可能有错,请见谅)
///////////////////////////// haed.h ///////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
struct DLNode
{
char data;
DLNode *prior, *next;
};
typedef DLNode* DLinkList;
bool Create( DLinkList &D , int n )
{
int i=0;
DLinkList p, newNode;
D=(DLinkList) malloc ( sizeof(DLNode) );
if( !D )
{
return false;
}
D->next=NULL;
D->prior=NULL;
p=D;
for( i=0; i<n; i++ )
{
newNode=( DLinkList ) malloc ( sizeof(DLNode) );
if( !newNode )
{
return false;
}
cout<<"请输入新结点的信息:";
cin>>newNode->data;
p->next=newNode;
newNode->prior=p;
newNode->next=NULL;
p=newNode;
//p从头结点指向下一个结点,以便下面的工作
}
D->prior=newNode;
p->next=D;
//这里是做了一个循环,就是说这个是“双向循环链表”
return true;
}
void ShowDLinkList( DLinkList D )
{
DLinkList p;
p=D->next;
cout<<"所有结点:";
while( p!=D )
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
///在第i个位置插入
void Insert( DLinkList &D, int i )
{
int j=1;
DLinkList p, newNode;
p=D->next;
//让p指向第一个结点
while ( p!=D && j<i )
//找到第i个节点
{
p=p->next;
j++;
}
newNode=(DLinkList) malloc ( sizeof(DLNode) );
cout<<"输入该插入节点的信息:";
cin>>newNode->data;
newNode->prior=p->prior;
//把新的结点进行连接
p->prior->next=newNode;
newNode->next=p;
p->prior=newNode;
}
/////////////////////////////// main.cpp ///////////////////////////////////////////////////////////////
#include "head.h"
bool Create( DLinkList &D , int n );
void ShowDLinkList( DLinkList D );
void Insert( DLinkList &D, int i );
void main()
{
DLinkList D;
Create(D,3);
ShowDLinkList(D);
Insert(D,4);
ShowDLinkList(D);
}