面试题解(六)之普天C++笔试题

题目:
    实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数
题解: 
  1. #include <stdio.h>
  2. typedef struct tmpNode{
  3.     struct tmpNode *prev;
  4.     struct tmpNode *next;
  5.     int value;
  6. }TNode;
  7. TNode *root=NULL;
  8. int main(void){
  9.     int value;
  10.     root=(TNode *)malloc(sizeof(TNode));
  11.     root->next=root;
  12.     root->prev=root;
  13.     root->value=0;
  14.     addNode();
  15.     addNode();
  16.     addNode();
  17.     addNode();
  18.     addNode();
  19.     addNode();
  20.     addNode();
  21.     addNode();
  22.     scanf("%d",&value);
  23.     insertNode(value);
  24.     viewNode();
  25.     scanf("%d",&value);
  26.     deleteNode(value);
  27.     viewNode();
  28.     scanf("%d",&value);
  29.     deleteNode(value);
  30.     viewNode();
  31.     return 0;
  32. }
  33. void addNode(){
  34.     TNode *temp;
  35.     TNode *newNode;
  36.     newNode=(TNode *)malloc(sizeof(TNode));
  37.     newNode->prev=newNode;
  38.     newNode->next=newNode;
  39.     newNode->value=0;
  40.     temp=root;
  41.     while(temp->next!=NULL&&temp->next!=temp){
  42.         temp=temp->next;
  43.     }
  44.     temp->next=newNode;
  45.     newNode->prev=temp;
  46.     newNode->value=temp->value+1;
  47. }
  48. void viewNode(){
  49.     TNode *temp;
  50.     temp=root;
  51.     while(1){
  52.         printf("this node addr is %p,and the value is %d/n",temp,temp->value);
  53.         if(temp->next==NULL||temp->next==temp)
  54.             break;  
  55.         temp=temp->next;
  56.     }
  57. }
  58. void insertNode(int value){
  59.     TNode *temp;
  60.     TNode *newNode;
  61.     temp=root;
  62.     newNode=(TNode *)malloc(sizeof(TNode));
  63.     newNode->next=newNode;
  64.     newNode->prev=newNode;
  65.     newNode->value=value;
  66.     while(temp->value<newNode->value&&temp->next!=NULL&&temp->next!=temp){
  67.         temp=temp->next;
  68.     }
  69.     if(temp->next!=NULL&&temp->next!=temp){
  70.         TNode *tmpNode=temp->next;
  71.         tmpNode->prev=newNode;
  72.         newNode->next=tmpNode;
  73.     }
  74.     newNode->prev=temp;
  75.     temp->next=newNode;
  76. }
  77. void deleteNode(int value){
  78.     TNode *temp;
  79.     temp=root;
  80.     while(temp->value!=value&&temp->next!=NULL&&temp->next!=temp){
  81.         temp=temp->next;
  82.     }
  83.     if(temp->value!=value)
  84.         return;
  85.     if(temp->prev!=NULL&&temp->prev!=temp){
  86.         TNode *prev;
  87.         prev=temp->prev;
  88.         if(temp->next!=NULL&&temp->next!=temp){
  89.             TNode *next;
  90.             next=temp->next;
  91.             prev->next=next;
  92.             next->prev=prev;        
  93.         }else{
  94.             prev->next=prev;
  95.         }
  96.     }else{
  97.         if(temp->next!=NULL&&temp->next!=temp){
  98.             TNode *next;
  99.             next=temp->next;
  100.             next->prev=next;
  101.             root=next;  
  102.         }else{
  103.             printf("can't delete root!/n");
  104.             return
  105.         }
  106.     }
  107.     free(temp);
  108.     temp=NULL;  
  109. }
编译环境: 
        gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

运行过程:
[explover@Explover 20081101]$ gcc -g pl.c -o pl
pl.c:34: warning: type mismatch with previous implicit declaration
pl.c:15: warning: previous implicit declaration of `addNode'
pl.c:34: warning: `addNode' was previously implicitly declared to return `int'
pl.c:49: warning: type mismatch with previous implicit declaration
pl.c:25: warning: previous implicit declaration of `viewNode'
pl.c:49: warning: `viewNode' was previously implicitly declared to return `int'
pl.c:59: warning: type mismatch with previous implicit declaration
pl.c:24: warning: previous implicit declaration of `insertNode'
pl.c:59: warning: `insertNode' was previously implicitly declared to return `int'
pl.c:78: warning: type mismatch with previous implicit declaration
pl.c:27: warning: previous implicit declaration of `deleteNode'
pl.c:78: warning: `deleteNode' was previously implicitly declared to return `int'
[explover@Explover20081101]$ ./pl
9
this node addr is 0x80499a8,and the value is 0
this node addr is 0x80499b8,and the value is 1
this node addr is 0x80499c8,and the value is 2
this node addr is 0x80499d8,and the value is 3
this node addr is 0x80499e8,and the value is 4
this node addr is 0x80499f8,and the value is 5
this node addr is 0x8049a08,and the value is 6
this node addr is 0x8049a18,and the value is 7
this node addr is 0x8049a28,and the value is 8
this node addr is 0x8049a38,and the value is 9
5
this node addr is 0x80499a8,and the value is 0
this node addr is 0x80499b8,and the value is 1
this node addr is 0x80499c8,and the value is 2
this node addr is 0x80499d8,and the value is 3
this node addr is 0x80499e8,and the value is 4
this node addr is 0x8049a08,and the value is 6
this node addr is 0x8049a18,and the value is 7
this node addr is 0x8049a28,and the value is 8
this node addr is 0x8049a38,and the value is 9
9
this node addr is 0x80499a8,and the value is 0
this node addr is 0x80499b8,and the value is 1
this node addr is 0x80499c8,and the value is 2
this node addr is 0x80499d8,and the value is 3
this node addr is 0x80499e8,and the value is 4
this node addr is 0x8049a08,and the value is 6
this node addr is 0x8049a18,and the value is 7
this node addr is 0x8049a28,and the value is 8

(本人水平有限,不足之处,请大家多多指正,谢谢!)    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值