// *******************************双链表部分
/*
//题目:程序员面试宝典 实现双链表构建,显示。
//思路: 节点的构造;
#include "CodeTest.h"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;
typedef int ElemType ;
typedef struct student
{
ElemType data;
struct student * prior;
struct student * next;
}dnode;
// 测试主函数
int main()
{
Status status;
dnode * create();
void print(dnode*);
Status del(dnode* head, int i , ElemType* e);// 函数声明
dnode *p;
ElemType x=0;
p=create();// 创建链表
if(p)
{
print(p); // 打印链表
}
status=del(p, 2 , &x);
if(status)
{
printf("\nThe delete data is:%d\n",x);// 打印删除的数据x
print(p); // 打印链表
}
printf("\nmain END\n");
return 0;
}/// main
// 双向链表的建立
// 思路:
dnode * create()
{
// 有头结点
dnode* head = new dnode;
if(!head) return head;
head->prior=head;
head->next=head; // 仅有头结点的双向链表
dnode* pnod = head;// 游标
int val;
cout << "输入数据(int类型,以0结束): ";
cin >> val;
while (val != 0) // 输入数据以0结束
{
dnode* curr = new dnode;
curr->data = val;// 新节点
curr->next = NULL;
curr->prior= pnod;//回指向前一节点
pnod->next = curr; //
pnod = pnod->next;// 更新前一节点游标,当前节点成为下一个节点的前一节点
cin >> val;
}
return head;
}
// 双向链表的打印同单链表
void print(dnode* head)
{
cout << "链表数据为:\n";
for (dnode* pre = head->next;pre!=NULL;pre=pre->next)
{
cout << pre->data <<" ";
}
cout <<endl;
cout <<"print End!"<<endl;
}
// 双向链表的插入与删除与单链表不同
// 思路:
Status del(dnode* head, int i , ElemType* e)
{
// 入口参数合法性判定
if(!head) return ERROR;
dnode *p;
int j=0;
p=head; // 有头结点
while(p->next && j<i) // 搜索到第i个节点 j=i的时候停止
{
p=p->next;
++j;
}
// 参数i 合法性检查: 0<i<length(L)
// 如果删除位置i>表长 则会导致p->next==NUll; // 如果删除位置小于等于0,则导致j>i-1;
if(!p ||j>i-1) return ERROR;//删除位置不合理。
// 删除处理,
*e=p->data;
p->prior->next =p->next;
p->next->prior =p->prior;
free(p);
return OK;
}
*/

被折叠的 条评论
为什么被折叠?



