验证可遍历可随便找点
本人为新手,此为数据结构课作业,思考并不全面,对与诸多情况的等等验证与反馈并未在代码上有所呈现,如有错误希望得到改正。
#include<iostream>
using namespace std;
typedef struct node
{
int data;
struct node* next;
struct node* pre;
}lnode;
void creatcircle(lnode* h);
void twowaylist(lnode* h);
int print(lnode* h);
int main()//主函数
{
lnode* h;
h = (lnode*)malloc(sizeof(lnode*));
creatcircle(h);
twowaylist(h);
print(h);
}
void creatcircle(lnode* h)//建立单循环链表
{
lnode* p,*a;
p = h;
int n;
cout << "请输入循环链表长度" << endl;
cin >> n;
cout << "请输入链表数据" << endl;
cin >> h->data;
for (int i =1 ; i < n; i++)
{
a = (lnode*)malloc(sizeof(lnode*));
cin >> a->data;
a->next = NULL;
p->next = a;
p = a;
}
p->next = h;
}
void twowaylist(lnode* h)//创立双向循环链表
{
lnode* p,*s;
p = h;
while (1)
{
s= p->next;
s->pre=p;
if (p->next == h)
break;
p = p->next;
}
}
int print(lnode* h)//验证
{
cout << h->pre->data;
return 0;
}