链表

来源:互联网 发布:淘宝商品上架教程 编辑:程序博客网 时间:2024/05/20 20:57

2016.7 6
复习
标准模板库:namespace
namespace::成员

                  using namespace std;                  std::cout

类型转换:dynamic_cast<子类指针>(父类指针)若失败返回NULL 动态类型转换
static_cast:静态类型转换:强制类型转换
字符串:char * str=“string”;
C++提供一个类string(系统已经将正常使用的运算符重载好了,空间也不需要程序员管理)
string str;
容器:
顺序容器:
vector(向量) 随机访问【】at 尾部增删元素
deque(双端队列) 随机访问【】 at 头部和尾部增删元素
list(列表) 不能随机访问(无【】,at),可在任意位置增删元素 要访问必须使用迭代器
关联容器: key value
set (key value 映射)不能有重复的value
multaste 可以有重复value
map (key value)key:一般都使用字符串,value:不能有重复key
multimap 可以重复key.
map

#include <iostream>#include<stdlib.h>#include<string>using namespace std;typedef struct Nobe{    char name[20];    int age;    struct Nobe* link;}student;/* 1.创建头节点 2.新建节点 sNode 3.使用动态指针来让节点指向新创建节点 4.让动态指针指向新创建节点 pNode=sNode */student* createList(int n){    student* head=NULL;    student* pNode=NULL;    student* sNobe=NULL;    if((head=(student*)malloc(sizeof(student)))==NULL)    {         printf("Memory allocation fail。。。");    return NULL;    }    strcpy(head->name,"headNobe");    head->age=25;    head->link=NULL;    pNode=head;    for(int i=0;i<n;i++)    {        if((sNobe=(student*)malloc(sizeof(student)))==NULL){            printf("Memory allocation fail。。。\n");            return NULL;        }    pNode->link=sNobe;    printf("please input %d person name:\n",i+1);    scanf("%s",sNobe->name);    printf("please input %d person age:\n",i+1);    scanf("%d",&(sNobe->age));    sNobe->link = NULL;    pNode=sNobe;    }    return head;}//输出链表所有节点(head:链表头节点)void showNode(student* head){    student* pNobe;//动态节点    pNobe=head;    while(pNobe!=NULL){//判断节点是否为空        printf("---------------\n");        printf("name=%s\n",pNobe->name);        printf("age=%d\n",pNobe->age);        pNobe=pNobe->link;    }}//插入节点(pNode:在pNode节点后添加节点)void insertNobe(student * pNobe){    char name[20];    int age;    student* sNobe=NULL;//新节点    if((sNobe=(student*)malloc(sizeof(student)))==NULL)//判断空间分配是否成功    {        printf("Memory allocation fail。。。\n");        return ;    }    printf("please input name\n");    scanf("%s",name);    strcpy(sNobe->name, name);    printf("please input age\n");    scanf("%d",&age);    sNobe->age=age;    sNobe->link=pNobe->link;    pNobe->link=sNobe;}//删除节点(head:链表头节点,name:删除节点名字)student* deleteNobe(student* head,char* name){    if(head==NULL)    {        return NULL;    }    student* p1Nobe=NULL;    student* p2Node=NULL;    p1Nobe=head;    p2Node=head->link;    if(strcmp(p1Nobe->name, name)==0)//判断是否删除头节点    {        head=head->link;        free(p1Nobe); //删除节点,释放节点        return head;    }    while (p2Node!=NULL) {        if(strcmp(p2Node->name, name)==0)//搜索节点name与name相同的节点        {            p1Nobe->link=p2Node->link;            free(p2Node); //删除节点,释放节点            //deleteNobe(head, name);            return head;        }        //p1Node p2Node分别向后移动    p1Nobe=p2Node;    p2Node=p1Nobe->link;    }    return head;}//查找节点student* searchNode(student* head,char* name){    student * pNode=head;    while(pNode!=NULL)    {        if(strcmp(pNode->name, name)==0)        {            return pNode;        }        pNode=pNode->link;    }    return pNode;    //}//删除整个链表void deleteAll(student * head){    student* p1Node=head;    student* p2Node=head->link;    student* p3Node=p2Node->link;    while(p2Node!=NULL)    {        p1Node->link=p3Node;        free(p2Node);        p2Node=p3Node;        if(p2Node!=NULL)        p3Node=p2Node->link;    }    head=NULL;}//找到中间节点void findMiddle(student* head){    student* p1Node=head;    student* p2Node=head;    int n=1;    while(p1Node->link!=NULL)    {        p1Node=p1Node->link;        n++;    }    for(int i=0;i<n/2;i++)    {        p2Node=p2Node->link;    }    printf("---------------\n");    printf("name=%s\n",p2Node->name);    printf("age=%d\n",p2Node->age);}//连接两个链表student* Link(student* head1,student *head2){    student* p1Node=head1;    student* p2Node=head2;    while (p1Node->link!=NULL){        p1Node=p1Node->link;    }    p1Node->link=p2Node;    return head1;}//将链表逆序student* invertedSequence(student* head){    /*student* p1Node=head;    student* p2Node=NULL;    student* p3Node=NULL;    while (p1Node!=NULL) {        p2Node=p1Node->link;        p1Node->link=p3Node;        p3Node=p1Node;        p1Node=p2Node;    }*/    student* p1Node=head;    student* p2Node=head->link;    student* p3Node=p2Node;    p1Node->link=NULL;    p2Node->link=p1Node;    while(p2Node!=NULL)    {        if(p2Node!=NULL)        p3Node=p2Node->link;        p1Node=p2Node;        p2Node=p3Node;        p2Node->link=p1Node;    }    return p3Node;}int main(int argc, const char * argv[]) {    student* head=createList(2);   // student* head1=createList(2);    showNode(head);    cout<<endl;   // showNode(head1);    //insertNobe(head);    deleteAll(head);    //deleteNobe(head,(char*)"a");    //cout<<"................"<<endl;    //findMiddle(head);    /*cout<<endl;    cout<<"---------"<<endl;    Link(head, head1);*/    //student* head2=invertedSequence(head);    //逆序    //showNode(head2);     showNode(head);    std::cout << "Hello, World!\n";    return 0;}
0 0
原创粉丝点击