双向链接列表_没有I/O,但不起作用? [英] bidirection linklist _ It is without I/O but it doesnt work ?
本文介绍了双向链接列表_没有I/O,但不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// list2peyvandi.cpp : Defines the entry point for the console application.
//
//delete o inserte list peyvandi 2 tarafe
#include "stdafx.h"
#include<iostream>
using namespace std;
struct node
{
int data ;
node *next ;
node *prev;
};
node *pred = NULL ;
node *insert(node * , int ) ;
void show() ;
void Delete();
//void Delete()
//{
// node *p = new node ;
// if(pred == NULL || p == NULL )
// return;
// if( p == pred )
// {
// p ->next -> prev = NULL ;
// pred = p -> next ;
// }
// else
// {
// p -> prev -> next = p -> next ;
// if(p ->next)
// p -> next -> prev = p-> prev ;
// }
//
// delete p ;
//}
//void show()
//{
//
// node *curPtr = pred ;
//
// while(curPtr)
// {
// cout<<curPtr->data<<endl;
// curPtr = curPtr->next;
//
// }
//}
int main()
{
node *pred , *cur ;
int x ;
cout << " create linked list " << endl ;
do{
cout << " enter data " << endl ;
cin >> x ;
if(pred == NULL)
pred = insert(NULL,x);
else
cur = pred ;
pred = NULL ;
while(cur != NULL && x>cur->data)
{
pred = cur ;
cur = cur ->next ;
}
if(insert(pred,x))
cout<<"inset number"<<x;
}while(x !=0);
return 0;
}
node *insert(node *pred , int data)
{
node *p = new node ;
p->data=data;
if(pred == NULL)
{
p ->next = pred ;
p ->prev = NULL ;
if(pred != NULL )
pred -> prev = p ;
pred = p ;
}
else
{
p -> prev = pred ;
pred -> next = p -> next ;
pred -> next -> prev = p ;
pred ->next = p ;
}
return p;
}
推荐答案
1.)在insert()
上,如果n> 1,则会得到一堆循环引用! 2.)在Delete()
上,您必须处理this
而不是node *p = new node ;
祝你好运.
1.) oninsert()
you get a bunch of circular references if n>1!
2.) onDelete()
you have to deal withthis
notnode *p = new node ;
Good luck.
您已经发布了此问题一次.如果需要修改,请编辑原始问题.
You already posted this question once. If you need to modify it then please edit your original question.
列表中的代码没有I/O.如果您这样做,可以考虑您的问题.请参阅有关该主题的其他答案.
—SA
No I/O in the code of list. If you do it, your Question can be considered. See my other Answer on the topic.
—SA
这篇关于双向链接列表_没有I/O,但不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文