过完春节,手生了,写个双向非循环链表

//doubleLinkList.c

 

 

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

/************************************************************************/
/* 双向非循环链表                                                       */
/* 头节点前驱指针为空,尾节点后继指针为空                               */
/* 头节点数据域为空,可存储链表节点个数                                 */
/************************************************************************/

typedef struct DulNode
{
 int datafield; //数据域
 struct DulNode *plink; //左指针
 struct DulNode *nlink; //右指针

}DulNode, *DulLinkList;


/*  初始化链表
 nodesize:链表的节点个数*/

DulLinkList init(int nodesize)
{

 int i;
 DulNode *prev, *head, *next;
 if((head = (DulNode *)malloc(sizeof(DulNode))) == NULL)
 {
  printf("Malloc memory failure!");
  exit(0); 
 }
 head->datafield = 0;
 head->plink = NULL;
 head->nlink = NULL;
 prev = head;

 for (i = 0; i < nodesize; i++)
 {
  if ((next = (DulNode *)malloc(sizeof(DulNode))) == NULL)
  {
   printf("Malloc memory failure!");
   exit(0);
  }
  prev->nlink = next;
  printf("Please enter the %d node value:/t", i+1);
  scanf("%d", &(next->datafield));
  next->plink = prev;
  next->nlink = NULL; 
  prev = next;
 }
 printf("/n");
 return(head);
}


/*检索链表*/
DulLinkList retrieve(DulNode *head, int nodevalue)
{
 DulNode *p;
 p = head->nlink;
 while(p != NULL)
 {
  if(p->datafield == nodevalue)
   return(p);
  else p = p->nlink;
 }
 printf("Can't retrieve any data!/n");
 return NULL;
}

/*插入节点, p为检索后返回的节点*/
void insert(DulNode *p, int insertvalue)
{
 DulNode *temp;
 if ((temp = (DulNode *)malloc(sizeof(DulNode))) == NULL)
 {
  printf("Malloc memory failure!");
  exit(0);
 }
 if(p->nlink == NULL)//如果插入的是尾节点
 {
  temp->datafield = insertvalue;
  temp->nlink=NULL;
  p->nlink=temp;
  temp->plink=p;
 }
 else
 {
  temp->datafield = insertvalue;
  temp->nlink=p->nlink;
  p->nlink=temp;
  temp->plink=p;
  (temp->nlink)->plink=temp;
 }

}

/*删除节点, p为检索后返回的节点*/
void delete(DulNode *p)
{
 if (p->nlink == NULL)//如果删除的是尾节点
 {
  (p->plink)->nlink = NULL;
  p->plink = NULL;
 }
 else
 {
  (p->nlink)->plink = p->plink;
  (p->plink)->nlink = p->nlink;
 }
 free(p);
}

/*打印链表*/
void printNode(DulNode *head)
{
 DulNode *p;
 p = head->nlink;
 printf("Nodes are:/t");
 while (p != NULL)
 {
  printf("%d/t", p->datafield);
  p = p->nlink;
 }
 printf("/n");
}

int main()
{
 int a,b,c;
 DulNode *head, *pretrieve;
 //初始化
 head = init(5);
 //打印
 printNode(head);
 //检索
 printf("Please enter the value you want to retrieve:/t");
 scanf("%d",&a);
 pretrieve = retrieve(head,a);
 if(pretrieve != NULL)
 {
  printf("The value retrieved is :%d/n", pretrieve->datafield);

  //插入
  printf("Please enter the value you want to insert:/t");
  scanf("%d",&b);
  insert(pretrieve, b);
  printf("The nodes after insert: /n");
  printNode(head);
 }

 //删除
 printf("/n");
 printf("Please enter the value you want to delete:/t");
 scanf("%d",&c);  
 pretrieve = retrieve(head,c);
 if(pretrieve != NULL)
 {
  delete(pretrieve);
 }
 printf("The nodes after delete: /n");
 printNode(head);
 return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值