// *******************************双链表部分

// *******************************双链表部分
/*
//题目:程序员面试宝典 实现双链表构建,显示。
//思路: 节点的构造;


  #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;
 
}

*/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值