删除带头结点的双循环链表p中第i个结点
2009-01-08 13:39
190 查看
是否是最近编写链表的程序写多了,今天写的这个《删除带头结点的双循环链表p中第i个结点》一次就成功,呵呵!值得小小的奖励下!不过,删除双向链表中的某个节点是较简单点,写出来与大家分享分享
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct DuNode{
char date;
struct DuNode *prior;
struct DuNode *next;
}*head;
struct DuNode *creatDuNode(void)
{
struct DuNode *p,*q;
int i;
if(!(head=(struct DuNode *)malloc(sizeof(struct DuNode))))
exit(0);
head->date='/0';
head->prior=NULL;
head->next=NULL;
p=head;
for(i=0;i<5;i++)
{
if(!(q=(struct DuNode*)malloc(sizeof(struct DuNode))))
exit(0);
printf("please input date:/n");
scanf("%c",&q->date);
getchar();
q->prior=p;
q->next=NULL;
p->next=q;
p=q;
}
return (head);
}
//删除带头结点的双循环链表p中第i个结点
struct DuNode *DelDuNode(struct DuNode *p,int i)
{
int j=0,flg=0;
if(p==NULL)
exit(0); //如果p为空的话就退出
while(p!=NULL) //在p没有到表尾时,查找i的位置
{
if(j!=i)
{
p=p->next;
j++;
continue;
}
flg=1;
break;
}
if(flg==1)
{
p->prior->next=p->next;
p->next->prior=p->prior;
}
else
{
printf("error del/n");
}
return (head->next);
}
int main()
{
struct DuNode *p;
int i;
char e;
creatDuNode();
p=head->next;
printf("please input number:/n");
scanf("%d",&i);
p=DelDuNode(p,i);
while(p!=NULL)
{
printf("p->date=%c/n",p->date);
p=p->next;
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct DuNode{
char date;
struct DuNode *prior;
struct DuNode *next;
}*head;
struct DuNode *creatDuNode(void)
{
struct DuNode *p,*q;
int i;
if(!(head=(struct DuNode *)malloc(sizeof(struct DuNode))))
exit(0);
head->date='/0';
head->prior=NULL;
head->next=NULL;
p=head;
for(i=0;i<5;i++)
{
if(!(q=(struct DuNode*)malloc(sizeof(struct DuNode))))
exit(0);
printf("please input date:/n");
scanf("%c",&q->date);
getchar();
q->prior=p;
q->next=NULL;
p->next=q;
p=q;
}
return (head);
}
//删除带头结点的双循环链表p中第i个结点
struct DuNode *DelDuNode(struct DuNode *p,int i)
{
int j=0,flg=0;
if(p==NULL)
exit(0); //如果p为空的话就退出
while(p!=NULL) //在p没有到表尾时,查找i的位置
{
if(j!=i)
{
p=p->next;
j++;
continue;
}
flg=1;
break;
}
if(flg==1)
{
p->prior->next=p->next;
p->next->prior=p->prior;
}
else
{
printf("error del/n");
}
return (head->next);
}
int main()
{
struct DuNode *p;
int i;
char e;
creatDuNode();
p=head->next;
printf("please input number:/n");
scanf("%d",&i);
p=DelDuNode(p,i);
while(p!=NULL)
{
printf("p->date=%c/n",p->date);
p=p->next;
}
return 0;
}
相关文章推荐
- 编写算法实现建立一个带头结点的含n个元素的双向循环链表H,并在链表H中的第i个位置插入一个元素e
- 双向循环链表的建立,插入,删除(不带头结点)
- 双链表的建立、求长、定位、插入、删除、输出和释放(带头结点且成环状)
- 线性表学习归纳总结五:不带头结点的循环链表
- 带头结点的循环链表基本操作
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- 用模板实现顺序表和带头结点的双向循环链表
- 带头结点的链表头插,尾插,中间插以及删除节点
- C语言实现单链表节点的删除(带头结点)
- 单链表在不知头结点的情况下对第i个元素的删除
- 带头结点的循环单向链表
- 数据结构(二)——单链表 、带头结点的单链表、循环链表 及其实现
- 带头结点的双向循环链表
- 《数据结构进行曲》 带头结点单循环链表的合并
- 假设在长度大于1的循环链表中,即无头结点也无头指针,s为指向链表中的某个结点的指针,试编写算法删除结点s的前驱结点
- 不带头结点的单链表递归删除元素为X的结点
- 不带头结点的单链表删除任意一个节点
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 删除链表中(不带头结点)值为value的结点,返回删除的个数
- 假设以带头结点的循环链表表示队列, 并且只设一个指针指向队尾元素结点(注意不设头指针) 试编写相应的队列初始化,入队列和出队列的算法