双链表的创建、插入、删除

#include < stdio.h >
#include
< malloc.h >

typedef 
struct  node  // 定义双链表
{
    
int data;
    
struct node *prior;
    
struct node *next;
}
snode;

snode 
* creat()  // 创建双链表
{
    snode 
*head, *p, *q;
    
int x;
    head 
= (snode *)malloc(sizeof(snode));
    q 
= head;
    printf(
"请输入创建双链表的值,以-1结束. ");
    printf(
"x = ");
    scanf(
"%d"&x);
    
while (x != -1)
    
{
        p 
= (snode *)malloc(sizeof(snode));
        p
->data = x;
        q
->next = p;
        p
->prior = q;
        q 
= p;
        printf(
"x = ");
        scanf(
"%d"&x);
    }

    q
->next = NULL;
    
return head;
}


void  display(snode  * head)//便利打印双链表
{
    snode 
*= head->next;
    
while (p != NULL)
    
{
        printf(
"%4d", p->data);
        p 
= p->next;
    }

    printf(
" ");
}


int  length(snode  * head) // 测链表的结点数
{
    snode 
*= head->next;
    
int i = 0;
    
while (p != NULL)
    
{
        p 
= p->next;
        i
++;
    }

    
return i;
}


void  opposite(snode  * head)
{
    snode 
*= head->next;
    
while (p->next != NULL)
       { p 
= p->next;}
    
while (p != head)
    
{
        printf(
"%4d", p->data);
        p 
= p->prior;
    }

    printf(
" ");
}


int  insnode(snode  * head,  int  x,  int  i)  // 把x插入到链表的第i的位置,即在第i个元素p之前插入,与单链表在p之后插入相区别
{
    snode 
*= head*s;
    
if(i < 1 || i > length(head) + 1||!p)
        
return 0;
    
else
    
{
         s 
= (snode *)malloc(sizeof(snode)); //分配内存
         for (int j = 0; j < i ; j++)

        {
= p->next;
}

            s->data = x;
            s
->prior = p->prior;//插入 四步完成
            s
->next = p;
            p->prior->next = s;
            p
->prior = s;

    }
    
return 1;
}


int  delnode(snode  * head,  int  i) // 删除链表中第i个结点
{
    snode 
*= head;
    
if(i < 1 || i > length(head)||!p)
        
return 0;
    
else
    
{
        
for (int j = 0; j < i; j++)
        
{
            p 
= p->next;
        }

       p->prior->next=p->next;
p->next->prior=p->prior;

        free(p);
    }
    
return 1;
}

int  main( void )
{
    snode 
*headl = creat(); //创建双链表
    printf("最初的链表如下: ");
    display(headl);
    printf(
"为了证明是双链表反向输出:  ");
    opposite(headl);
    
int num, location;
    printf(
"请分别输入您要插入到链表中的数以及想插入的位置:");
    scanf(
"%d %d"&num, &location);
    
if (insnode(headl, num, location))
    
{
        printf(
"插入新值以后的链表如下: ");
        display(headl);
        printf(
"为了证明插入新值以后仍然是双链表,反向输出如下:  ");
        opposite(headl);
    }

    
else
        printf(
"输入有误 ");
    printf(
"请输入您想删除的结点位置:");
    scanf(
"%d"&location);
    
if (delnode(headl, location))
    
{
        printf(
"删除第%d个结点后的链表如下: ", location);
        display(headl);
        printf(
"为了证明删除一个结点以后仍然是双链表,反向输出如下:  ");
        opposite(headl);
    }

    
else
        printf(
"输入有误! ");

}

转自http://blog.csdn.net/tanlingyun/article/details/1583834

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值