双向链表,大家能帮我看看有什么问题吗,还有什么可以改进的
好久没写链表了,今天写了个双向链表,不知道自己写得怎样,也不知道有什么潜在的问题,求各位指教,插入暂时还没写,插入跟删除差不多就不写了。
代码如下:
#include <iostream>
using namespace std;
typedef int ElmType;
const int length=10;
typedef struct node
{
ElmType i;
struct node* next;
struct node* back;
}Node;
void init(Node*&,int); //初始化双链表
bool traverse(Node*); //前后遍历双链表
void insert(Node*&); //在节点p后插入
void del(Node*&); //删除节点p
void collect_space(Node* head);
void main()
{
Node *head,*p; //生成一个未初始化的Node类型头指针
init(head,length);//建立双向链表,其中包括初始化head指针
p=head->next->next;
del(p); //删除节点p
traverse(head); //遍历
getchar(); //按任意键退出
collect_space(head); //收回链表空间
}
void init(Node* &head,int max_num)
{
head=(Node*)malloc(sizeof(Node));
head->i=0;
head->back=NULL;
head->next=NULL;
Node *p,*temp;
p=head;
for(int i=1;i<max_num;i++)
{
temp=(Node*)malloc(sizeof(Node));
temp->i=i;
temp->back=p;
temp->next=NULL;
p->next=temp;
p=temp;
}
}
bool traverse(Node* head)
{
if(head==NULL)
{
cout<<"输入的head为空"<<endl;
return false;
}
while(head!=NULL)
{
cout<<head->i<<" ";
head=head->next;
}
return true;
}
void del(Node* &p)
{
if(p==NULL)
{
cout<<"你输入的指针为空"<<endl;
return;
}
Node *temp=p;
if(p->next==NULL)
{
//如果删除的是尾指针,那么删除p后,p所指向位置自动前移一位
if(p->back==NULL)
{
cout<<endl<<"链表已清空"<<endl;
free(p);
p=NULL;
return;
}
else
{
p=temp->back;
p->next=NULL;
free(temp);
}
}
else if(p->back==NULL)
{
//如果删除的是头指针,那么删除p后,p所指向位置向后移一位
p=temp->next;
p->back=NULL;
free(temp);
}
else
{
//如果删除的是中间位置的,那么删除p后,p指向位置向前移一位
p=temp->next;
temp->back->next=p;
p->back=temp->back;
free(temp);
}
}
void insert(Node* &p)
{
}
void collect_space(Node* head)
{
while(head!=NULL)
{
del(head);
}
}
先谢谢大家了
[解决办法]
双向链表的建立
// * ======================================== */
// * 程式实例: 4_3_1.c */
// * 双向链结串列的建立 */
// * ======================================== */
#include <stdlib.h>
struct dlist // * 双向串列结构宣告 */
{
int data; // * 节点资料 */
struct dlist *front; // * 指向下一节点的指标 */
struct dlist *back; // * 指向前一节点的指标 */
};
typedef struct dlist dnode; // * 双向串列新型态 */
typedef dnode *dlink; // * 双向串列指标新型态 */
// * ---------------------------------------- */
// * 使用阵列值建立双向链结串列 */
// * ---------------------------------------- */
dlink createdlist(int *array,int len)
{
dlink head; // * 双向串列的指标 */
dlink before; // * 前一节点的指标 */
dlink new_node; // * 新节点的指标 */
int i;
// * 建立第一个节点 */
// * 配置节点记忆体 */
head = ( dlink ) malloc(sizeof(dnode));
if ( !head ) // * 检查记忆体指标 */
return NULL;
head->data = array[0]; // * 建立节点内容 */
head->front = NULL; // * 设定指标初值 */
head->back = NULL; // * 设定指标初值 */
before = head; // * 指向第一个节点 */
for ( i = 1; i < len; i++ ) // * 用回路建立其它节点 */
{
// * 配置节点记忆体 */
new_node = ( dlink ) malloc(sizeof(dnode));
if ( !new_node ) // * 检查记忆体指标 */
return NULL;
new_node->data = array[i];
new_node->front = NULL; // * 设定指标初值 */
new_node->back = before; // * 将新节点指向前节点 */
before->front = new_node; // * 将前节点指向新节点 */
before = new_node; // * 新节点成为前节点 */
}
return head; // * 传回串列起始指标 */
}
// * ---------------------------------------- */
// * 双向链结串列的列印 */
// * ---------------------------------------- */
void printdlist(dlink head,dlink now)
{
while ( head != NULL ) // * 串列走访回路 */
{
if ( head == now ) // * 印出目前节点资料 */
printf("#%d#",head->data); // * 列印节点资料 */
else
printf("[%d]",head->data); // * 列印节点资料 */
head = head->front; // * 指向下一个节点 */
}
printf("\n"); // * 换行 */
}
// * ---------------------------------------- */
// * 主程式: 模拟双向串列的移动 */
// * 使用选项来移动节点, 且将值和节点编号印 */
// * 出. */
// * ---------------------------------------- */
void main()
{
dlink head; // * 双向链结串列指标 */
dlink now = NULL; // * 目前节点指标 */
int list[6] = { 1, 2, 3, 4, 5, 6 }; // * 阵列内容 */
int select; // * 选择项1,2或3 */
head = createdlist(list,6); // * 建立双向链结串列 */
if ( head == NULL )
{
printf("记忆体配置失败! \n"); // * 串列建立失败 */
exit(1); // * 结束程式 */
}
now = head; // * 目前指向第一节点 */
while ( 1 )
{
printf("链表内容是: ");
printdlist(head,now); // * 列印出内容 */
printf("[1]往下移动 [2]往回移动 [3]离开 ==> ");
scanf("%d",&select); // * 读入选项 */
switch ( select )
{
// * 往下移动 */
case 1: if ( now->front != NULL )
now = now->front; // * 指向下一节点 */
break;
// * 往回移动 */
case 2: if ( now->back != NULL )
now = now->back; // * 指向前一节点 */
break;
// * 离开 */
case 3: exit(1);
}
}
}
[解决办法]
程序中很多c语言的东西,malloc struct ...
如果不是看到一个#include<iostream>我都认为这是c源文件了
我能问一下为什么用
#include<iostream>
后用的全是c语言的东西