双向链表数据输出异常
双向链表数据输出错误
[code=C/C++][/code]
#include "iostream"
#include "stdlib.h"
#define OK 1
#define ERROR -1
typedef int Elemtype;
typedef int Stutaus;
using namespace std;
typedef struct DuLNode
{
int data;
struct DuLNode *next,*prior;
}DuLNode,*DuLinkList;
void Create_L(DuLinkList &L,int n)
{
L=(DuLinkList)malloc(sizeof(DuLNode));
L->next=L->prior=L;
for(int i=n;i>0;--i)
{
DuLNode *p=(DuLinkList)malloc(sizeof(DuLNode));
cin>>(p->data);
//p->next=L->next;
//L->next=p;
p->prior=L->next;
p=L->prior;
L->prior=p->next;
L=p->prior;
//p=L->next->prior;
//L->next->prior=p;
}
}
void Print_L(DuLinkList &L,int n)
{
DuLinkList p;
int i=0;
p=L;
while (i<n)
{
++i;
//p=p->next;
cout<<p->data;
}
cout<<endl;
}
void main()
{
int n;
DuLinkList La;
cout<<"please input data num: "<<endl;
cin>>n;
Create_L(La,n);
cout<<"data is:"<<endl;
Print_L(La,n);
}
------解决方案--------------------
[code=C/C++][/code]
#include "iostream"
#include "stdlib.h"
#define OK 1
#define ERROR -1
typedef int Elemtype;
typedef int Stutaus;
using namespace std;
typedef struct DuLNode
{
int data;
struct DuLNode *next,*prior;
}DuLNode,*DuLinkList;
void Create_L(DuLinkList &L,int n)
{
L=(DuLinkList)malloc(sizeof(DuLNode));
L->next=L->prior=L;
for(int i=n;i>0;--i)
{
DuLNode *p=(DuLinkList)malloc(sizeof(DuLNode));
cin>>(p->data);
//p->next=L->next;
//L->next=p;
p->prior=L->next;
p=L->prior;
L->prior=p->next;
L=p->prior;
//p=L->next->prior;
//L->next->prior=p;
}
}
void Print_L(DuLinkList &L,int n)
{
DuLinkList p;
int i=0;
p=L;
while (i<n)
{
++i;
//p=p->next;
cout<<p->data;
}
cout<<endl;
}
void main()
{
int n;
DuLinkList La;
cout<<"please input data num: "<<endl;
cin>>n;
Create_L(La,n);
cout<<"data is:"<<endl;
Print_L(La,n);
}
------解决方案--------------------
- C/C++ code
void Create_L(DuLinkList &L,int n) { L=(DuLinkList)malloc(sizeof(DuLNode)); L->next=L->prior=L; for(int i=n;i>0;--i) { DuLNode *p=(DuLinkList)malloc(sizeof(DuLNode)); cin>>p->data; p->next=L->next; L->next->prior=p; p->prior=L; L->next=p; } } void Print_L(DuLinkList &L,int n) { DuLinkList p; int i=0; p=L; while (i<n) { ++i; p=p->next; cout<<p->data<<" "; } cout<<endl; }
------解决方案--------------------
链表需要自己多琢磨,多用笔画画.
不好说清楚的.
- C/C++ code
#include "iostream" #include "stdlib.h" #define OK 1 #define ERROR -1 typedef int Elemtype; typedef int Stutaus; using namespace std; typedef struct DuLNode { int data; struct DuLNode* next, *prior; } DuLNode, *DuLinkList; void Create_L(DuLinkList& L, int n) { L = (DuLinkList)malloc(sizeof(DuLNode)); cin>>L->data; L->next = L->prior = L; for(int i = n-1; i > 0; --i) { DuLNode* p = (DuLinkList)malloc(sizeof(DuLNode)); cin >> (p->data); //p->next=L->next; //L->next=p; p->next=L; L->prior->next=p; p->prior=L->prior; L->prior=p; // p->prior = L->next; // p = L->prior; // L->prior = p->next; // L = p->prior; //p=L->next->prior; //L->next->prior=p; } } void Print_L(DuLinkList& L, int n) { DuLinkList p; int i = 0; p = L; while(i < n) { ++i; //p=p->next; cout << p->data<<" "; p=p->next; } cout << endl; } int main() { int n; DuLinkList La; cout << "please input data num: " << endl; cin >> n; Create_L(La, n); cout << "data is:" << endl; Print_L(La, n); return 0; }