首页 > 代码库 > 20140502
20140502
1、static_cast和dynamic_cast的类型检查
- static_cast的类型检查:只检查无关类之间的转换
CBaseY* pY1 = static_cast<CBaseY*>(pX); // 错误,编译未通过, 类型指向是无关的 (解释:指针变量PX是CBaseX类,现在要将其转换为CBaseY类(和CBasex毫无关系))
- dynamic_cast的类型检查时全面的,包括无关类。
CBaseY* pY1 = dynamic_cast<CBaseY*>(pX); // pY最终等于0(编译通过,但是转换未成功),前提是pX必须包含虚函数
2、双链表建立,删除,打印
#include<iostream> #include<stdio.h> #include <string> #include<conio.h> typedef struct student { int data; struct student *next; struct student *pre; }dnode; dnode *create() //尾插法建立带头结点的双链表 { dnode *head,*p,*s; int x=0; head=(dnode *)malloc(sizeof(dnode)); head->data=http://www.mamicode.com/0; head->pre=NULL; head->next=NULL; p=head; printf("please input data (end by 0):"); scanf("%d",&x); while(x!=0) { s=(dnode *)malloc(sizeof(dnode)); s->data=http://www.mamicode.com/x; s->pre=p; s->next=p->next; p->next=s; p=s; printf("please input data (end by 0):"); scanf("%d",&x); } return head; }; void delnode(dnode *head,int num) { dnode *p=head->next; while(p!=NULL) { if(num==p->data) { p->pre->next=p->next; if(p->next!=NULL) //最后一个节点 p->next->pre=p->pre; free(p); break; } p=p->next; } printf("could not find the node\n"); } void display1(dnode *head) //正向打印 { dnode *p=head->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } printf("\n"); } void display2(dnode *head)//反向打印 { dnode *p=head; while(p->next!=NULL) { p=p->next; } while(p->pre!=NULL) { printf("%d ",p->data); p=p->pre; } printf("\n"); } void main() { dnode *head=create(); delnode(head,4); display1(head); display2(head); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。