zoukankan      html  css  js  c++  java
  • 数据结构笔记1

    1.线性表

    文件结构

    list.h

    1. //list.h   
    2.     
    3. #ifndef _LIST_H   
    4. #define _LIST_H //条件编译语句   
    5.     
    6. #define LIST_INIT_SIZE  10  //线性表初始大小   
    7. #define LIST_INCREMENT  10  //递增大小   
    8.     
    9. typedef struct  
    10. {   
    11.     ElemType * elem;    //线性表首地址   
    12.     int length;         //线性表当前使用长度   
    13.     int size;           //线性表当前最大长度   
    14. }LIST;   
    15.     
    16. LIST *InitList();       //初始化   
    17. void FreeList(LIST *l);   
    18. int InsertList(LIST* l,int i,ElemType *e);   
    19. int DeleteList(LIST *l,int i);   
    20.     
    21. #endif 

    list.c

    1. //list.c   
    2. #include <stdio.h>   
    3. #include<stdlib.h>   
    4. #include "stu.h"   
    5. #include "list.h"   
    6.     
    7. //初始线性表函数   
    8. LIST * InitList()   
    9. {   
    10.     LIST* l=(LIST*)malloc(sizeof(LIST));    
    11.     //在堆中动态定义一个线性表结构指针   
    12.     if(l==NULL)   
    13.     {   
    14.         exit(0);   
    15.     }   
    16.     
    17.     l->elem=(ElemType*)malloc(LIST_INIT_SIZE* sizeof(ElemType));   
    18.     //在堆中动态开辟数据空间   
    19.     if(l->elem==NULL)   
    20.     {   
    21.         free(l);   
    22.         exit(0);   
    23.     }   
    24.     l->length=0;   
    25.     l->size=LIST_INIT_SIZE;   
    26.     return l;   
    27. }   
    28.     
    29. void FreeList(LIST *l)   
    30. {   
    31.     free(l->elem);  //释放线性表的成员空间   
    32.     free(l);    //释放线性表本身   
    33. }   
    34.     
    35. int InsertList(LIST* l,int i,ElemType *e)   
    36. {   
    37.     ElemType *p=NULL,*q=NULL,*newElem=NULL;   
    38.     if(l==NULL||e==NULL)   
    39.         return 0;   
    40.     if(i<1||i>l->length+1)   
    41.         return 0;   
    42.     if(l->length>=l->size)   
    43.     {   
    44.         newElem=realloc(l->elem,   
    45.             (l->size+LIST_INCREMENT)*sizeof(ElemType));   
    46.         if(newElem==NULL)   
    47.             return 0;   
    48.         l->elem=newElem;   
    49.         l->size+=LIST_INCREMENT;   
    50.     }   
    51.     
    52.     q=&l->elem[i-1]; //要插入的位置   
    53.     for(p=&(l->elem[l->length-1]);p>=q;p--)   
    54.         *(p+1)=*p; //p往后移一位   
    55.     *q=*e; //插入   
    56.     ++l->length;   
    57.     return 1;   
    58. }   
    59.     
    60. int DeleteList(LIST *l,int i)   
    61. {   
    62.     ElemType *p=NULL,*q=NULL;   
    63.     if(l==NULL)   
    64.         return 0;   
    65.     if(i<1||i>l->length)   
    66.         return 0;   
    67.     p=&l->elem[i-1];   
    68.     q=&l->elem[l->length-1];   
    69.     for(;p<q;p++)   
    70.         *p=*(p+1);//将后面的元素向前移   
    71.     --l->length;   
    72.     return 1;   
    73. }  

    stu.h

    1. //stu.h   
    2.     
    3. #ifndef _STU_H   
    4. #define _STU_H   
    5.     
    6. typedef struct  //定义一个学生的结构体   
    7. {   
    8.     char sno[5];   
    9.     char name[21];   
    10.     char sex[3];   
    11.     int score;   
    12. }ElemType;   
    13.     
    14. #endif 

    main.c

    1. //main.c   
    2. #include <stdio.h>   
    3. #include "stu.h"   
    4. #include "list.h"   
    5.     
    6. ElemType stu[3]={   
    7.     {"s010","张三","",80},   
    8.     {"s011","张四","",81},   
    9.     {"s012","张五","",82}   
    10. };   
    11.     
    12. void main()   
    13. {   
    14.     //定义一个线性性表指针   
    15.     int i;   
    16.     LIST* list=NULL;   
    17.     list=InitList();    //初始化线性表   
    18.          
    19.     for(i=0;i<3;i++)   
    20.         InsertList(list,1,&stu[i]);   
    21.     DeleteList(list,2);   
    22.     
    23.     FreeList(list);   
    24. }  

    2.单链表

    文件结构

    list.h

    1. //list.h   
    2.     
    3. #ifndef _LIST_H   
    4. #define _LIST_H   
    5.     
    6. typedef struct _node   
    7. {   //结点类型定义   
    8.     void *data; //结点的数据域   
    9.     struct _node *next; //结点的指针域   
    10. }NODE;   
    11.     
    12. typedef struct  
    13. {   //链表类型定义   
    14.     NODE * head;//头结点   
    15.     NODE * last;//尾结点   
    16.     int length;//链表长度   
    17. }LIST;   
    18.     
    19. LIST* InitList();   
    20. int AddNode(LIST* l,void *data,int size);   
    21. NODE* FindNodeByKey(LIST *l,void *key,   
    22.                     int (*compare)(void*,void*)/*函数指针*/);   
    23. NODE* FindNode(LIST *l,void *key,   
    24.                     int (*compare)(void*,void*)/*函数指针*/,NODE **pre);   
    25. int DeleteListByKey(LIST* l,void* Key,int (*compare)(void*,void*));   
    26.     
    27. #endif 

    list.c

    1. //list.c   
    2.     
    3. #include <stdio.h>   
    4. #include <stdlib.h>   
    5. #include <string.h>   
    6. #include "list.h"   
    7.     
    8. LIST* InitList()   
    9. {   
    10.         LIST* l=(LIST*)malloc(sizeof(LIST)); //创建一个链表   
    11.         if(l==NULL)   
    12.             exit(0);   
    13.         memset(l,0,sizeof(LIST));//将链表清零   
    14.         return l;   
    15. }   
    16.     
    17. int AddNode(LIST* l,void *data,int size) //添加结点   
    18. {   
    19.     //在调试中可以这样看结点的值(struct STU*)l->head->data,(struct STU*)l->last->data   
    20.     NODE *n=NULL;   
    21.     if(l==NULL||data==NULL)   
    22.         return 0;   
    23.     n=(NODE*)malloc(sizeof(NODE)); //创建一个结点   
    24.     if(n==NULL)   
    25.         return 0;   
    26.     n->data=data;   
    27.     n->data=malloc(size);   
    28.     if(n->next=NULL)   
    29.     {   
    30.         free(n);   
    31.         return 0;   
    32.     }   
    33.     memcpy(n->data,data,size);//把数据拷到目标结点中去   
    34.     
    35.     /*//方法一  
    36.     if(l->head==NULL)  
    37.     {  
    38.         l->head=n;  
    39.         l->last=n;  
    40.         l->length=1;  
    41.     }  
    42.     else  
    43.     {  
    44.         l->last->next=n;//挂在尾结点后面  
    45.         l->last=n; //改变尾结点的指向  
    46.         l->length++;  
    47.     }  
    48.     */  
    49.     //方法二   
    50.     if(l->head==NULL)   
    51.         l->head=n;   
    52.     else  
    53.         l->last->next=n;   
    54.     l->last=n;   
    55.     l->length++;   
    56.     return 1;   
    57. }   
    58.     
    59. NODE* FindNodeByKey(LIST *l,void *key,   
    60.                     int (*compare)(void*,void*)/*函数指针*/)   
    61. {   //查找结点   
    62.     NODE *p=NULL;   
    63.     if(l==NULL||key==NULL||compare==NULL)   
    64.         return NULL;   
    65.     p=l->head;//p指向第一个结点   
    66.     while(p)   
    67.     {   
    68.         if(compare(p->data,key)==1)//如果返回为1说明找到了   
    69.             return p;   
    70.         p=p->next;//否则继续往下找   
    71.     }   
    72.     return NULL;//都没找到,返回空   
    73. }   
    74.     
    75.     
    76. NODE* FindNode(LIST *l,void *key,   
    77.                     int (*compare)(void*,void*)/*函数指针*/,NODE **pre)   
    78. {   //查找结点,并传入前一个结点的参数   
    79.     NODE *p=NULL;   
    80.     if(l==NULL||key==NULL||compare==NULL||pre==NULL)   
    81.         return NULL;   
    82.     p=l->head;//p指向第一个结点   
    83.     *pre=NULL;   
    84.     while(p)   
    85.     {   
    86.         if(compare(p->data,key)==1)//如果返回为1说明找到了   
    87.             return p;   
    88.         *pre=p;//找到的结点的前一个结点   
    89.         p=p->next;//否则继续往下找   
    90.     }   
    91.     return NULL;//都没找到,返回空   
    92. }   
    93.     
    94. int DeleteListByKey(LIST* l,void* key,int (*compare)(void*,void*))   
    95. {   //删除结点   
    96.     NODE *p=NULL,*q=NULL;   
    97.     p=FindNode(l,key,compare,&q/*前一个结点的地址*/);   
    98.     if(p==NULL)   
    99.         return 0;   
    100.     if(q==NULL)//如果前一个结点为空   
    101.         l->head=p->next;//那么将头指针指向要删除的结点的后一个结点   
    102.     else  
    103.         q->next=p->next;   
    104.     if(p==l->last)   
    105.             l->last=q;   
    106.         free(p->data);   
    107.         free(p);   
    108.         l->length--;   
    109.         return 1;   
    110. }  

    main.c

    1. //main.c   
    2.     
    3. #include<string.h>   
    4. #include<stdio.h>   
    5. #include "list.h"   
    6.     
    7. struct STU   
    8. {   
    9.     char sno[5];   
    10.     char name[21];   
    11.     int age;   
    12.     float score;   
    13. }s[3]={   
    14.     {"s001","lin wu",12,90},   
    15.     {"s002","xiao ming",13,91},   
    16.     {"s003","wang wu",11,100}   
    17. };   
    18.     
    19. int CompareByName(void *info,void *key)   
    20. {   
    21.     struct STU* stu=(struct STU*)info;   
    22.     char *name=(char *)key;   
    23.     return strcmp(stu->name,name)==0?1:0;   
    24. }   
    25.     
    26. int CompareBySno(void *info,void *key)   
    27. {   
    28.     struct STU* stu=(struct STU*)info;   
    29.     char *sno=(char *)key;   
    30.     return strcmp(stu->sno,sno)==0?1:0;   
    31. }   
    32.     
    33. void main()   
    34. {   
    35.     int i;   
    36.     NODE *res=NULL;//存放找到的结果   
    37.     char name[]="xiao ming",sno[]="s001";   
    38.     LIST* list=InitList();   
    39.     for(i=0;i<3;i++)   
    40.         AddNode(list,&s[i],sizeof(s[i]));   
    41.     
    42.     res=FindNodeByKey(list,name,CompareByName);   
    43.     if(res==NULL)   
    44.         printf("NOT Find\n");   
    45.     else  
    46.         printf("Find it\n");   
    47.     
    48.     if(DeleteListByKey(list,sno,CompareBySno))   
    49.         printf("delete success\n");   
    50.     //在调试中可以这样看结点的值(struct STU*)list->head->data,(struct STU*)list->last->data   
    51.     else  
    52.         printf("delete fail\n");   
    53. }

    3.双链表

    文件结构

    list.h

    1. //list.h   
    2.     
    3. #ifndef _LIST_H   
    4. #define _LIST_H   
    5.     
    6. typedef struct _node   
    7. {   
    8.     void *data;   
    9.     struct _node *pior;   
    10.     struct _node *next;   
    11. }NODE;   
    12.     
    13. typedef struct  
    14. {   
    15.     NODE *head;   
    16.     NODE *last;   
    17.     int length;   
    18. }LIST;   
    19.     
    20. LIST *InitList();   
    21. int InsertNode(LIST *l,void *data,int size);   
    22. int DeletNode(LIST* l,int n);   
    23. void PrintList(LIST *l,int page,int perp,void(*printNode)(void*));   
    24. void ClearList(LIST* l);   
    25. void DestroyList(LIST **l);   
    26.     
    27.     
    28. #endif  

    list.c

    1. //list.c   
    2.     
    3. #include <stdio.h>   
    4. #include <stdlib.h>   
    5. #include <string.h>   
    6. #include "list.h"   
    7.     
    8. LIST *InitList()//初始化链表   
    9. {   
    10.     LIST *l=(LIST*)malloc(sizeof(LIST));   
    11.     if(l==NULL) exit(0);   
    12.     l->head=(NODE*)malloc(sizeof(NODE));   
    13.     if(l->head==NULL) exit(0);   
    14.     memset(l->head,0,sizeof(NODE));   
    15.     l->last=(NODE*)malloc(sizeof(NODE));   
    16.     if(l->last==NULL) exit(0);   
    17.     memset(l->last,0,sizeof(NODE));   
    18.     
    19.     //l->head->pior==NULL;   
    20.     l->head->next=l->last;//将头结点的后续指向尾结点   
    21.     l->last->pior=l->head;//将尾结点的前驱指向头结点   
    22.     
    23.     l->length=0;   
    24.     return l;   
    25. }   
    26.     
    27. int InsertNode(LIST *l,void *data,int size) //插入结点   
    28. {   
    29.     NODE *n=NULL; //定义一个空结点   
    30.     if(l==NULL||data==NULL||size<=0)//判断传入的参数是否合法   
    31.         return 0;   
    32.     n=(NODE*)malloc(sizeof(NODE));//为结点分配空间   
    33.     if(n==NULL)    
    34.         return 0;   
    35.     n->data=malloc(size);//为结点的数据域分配空间   
    36.     if(n->data==NULL)   
    37.     {   
    38.         free(n);   
    39.         return 0;   
    40.     }   
    41.     memcpy(n->data,data,size);//将传入的数据拷贝到结点的数据域当中   
    42.     n->next=l->last;        //它的后继指针指向尾结点   
    43.     n->pior=l->last->pior;  //它的前驱指针指向尾结点的前一个   
    44.     
    45.     l->last->pior->next=n;  //把尾结点的前一个的后继指针指向它   
    46.     l->last->pior=n;        //把尾结点前驱指针指向它   
    47.     l->length++;            //链表的长度加1   
    48.     return 1;   
    49. }   
    50.     
    51. int DeletNode(LIST* l,int n) //删除结点   
    52. {   
    53.     NODE *p=NULL;   
    54.     int i=0;   
    55.     if(l==NULL||n<1||n>l->length)//判断传入的参数是否合法   
    56.         return 0;   
    57.     p=l->head->next;//p指向头结点的下一个   
    58.     while(i<l->length)//查找第n个结点   
    59.     {   
    60.         i++;   
    61.         if(i==n)   
    62.             break;   
    63.         p=p->next;   
    64.     }   
    65.     p->pior->next=p->next;//p的前一个的后继指针指向p的后一个   
    66.     p->next->pior=p->pior;//p的后一个的前驱指针指向p的前一个   
    67.     free(p->data);//释放p结点的数据域   
    68.     free(p);//释放p结点   
    69.     l->length--;   
    70.     return 1;   
    71. }   
    72.     
    73. void PrintList(LIST *l,int page,int perP,void(*printNode)(void*))//分布打印链表中的数据   
    74. {   //page为页的编号,perP为每页的结点数   
    75.     int start,end,i;   
    76.     NODE *p=NULL;   
    77.     if(l==NULL||printNode==NULL)//判断传入的参数是否有效   
    78.         return;   
    79.     start=(page-1)*perP+1;//将在某页面第一条要打印的数据所对映的结点所在链表中的位置   
    80.     end=page*perP;//将在某页面最后一条要打印的数据所对映的结点所在链表中的位置   
    81.     p=l->head->next;//p指向第一个数据结点   
    82.     i=0;   
    83.     while(i<l->length && p->next!=NULL)   
    84.     {   
    85.         i++;   
    86.         if(i==start) break;//定位到某页面的第一条要打印的数据   
    87.         p=p->next;   
    88.     }   
    89.     for(;i<=end && p->next!=NULL;i++)//打印某一页面的数据,直到最后一条   
    90.     {   
    91.         printNode(p->data);   
    92.         p=p->next;   
    93.     }   
    94. }   
    95.     
    96. void ClearList(LIST* l)//清空链表   
    97. {   
    98.     NODE *p=NULL;   
    99.     if(l==NULL)   
    100.         return;   
    101.     /*  
    102.     //方法一  
    103.     while(l->length)  
    104.         DeleteNode(l,1);  
    105.     */  
    106.     
    107.     //方法二   
    108.     while(l->length)   
    109.     {   
    110.         p=l->head->next;//p指向第一数据结点   
    111.         l->head->next=p->next;//把头结点的前驱指针指向p的下一个(第一个数据结点)   
    112.         p->next->pior=l->head;//p的下一个的前驱指针指向头结点   
    113.         free(p->data);//释放p指向的结点的数据域   
    114.         free(p);//释放p结点   
    115.         l->length--;//链表长度减1   
    116.     }   
    117. }   
    118.     
    119. void DestroyList(LIST **l)//删除链表   
    120. {   
    121.     LIST *p=*l;   
    122.     //定义一个指向链表lp指针,并将传入的指向链表的指针变量的值*l,(*&list)赋给它   
    123.     //这里指向链表的指针变量的值*l是链表的地址   
    124.     if(p==NULL) return;   
    125.     ClearList(p);//清空链表   
    126.     free(p->head);//释放头结点   
    127.     free(p->last);//释放尾结点   
    128.     free(p);//释放指针变量   
    129.     //p=NULL;   
    130.     *l=NULL;//将指向链表的指针的值赋成空值,即将指向已经销毁了的链表的指针指向空值   

    main.c

    1. //main.c   
    2.     
    3. #include<stdio.h>   
    4. #include "list.h"   
    5.     
    6. double d[5]={10.23,34.23,54.65,122,35.5};   
    7.     
    8. void PrintData(void *data)//打印数据   
    9. {   
    10.     double *d=(double*)data;   
    11.     printf("d=%lf\n",*d);   
    12. }   
    13.     
    14. void main()   
    15. {   
    16.     int i;   
    17.     LIST *list=InitList();   
    18.     for(i=0;i<5;i++)   
    19.         InsertNode(list,&d[i],sizeof(d[i]));   
    20.     PrintList(list,1,3,PrintData);   
    21.     
    22.     DestroyList(&list);//删除链表   
    23.     //&list 传递指向链表的指针变量地址给DestroyList函数   
    24.     if(list==NULL)   
    25.         printf("list is NULL\n");   
    26.     else  
    27.         printf("list is not NULL\n");   
    28. }
  • 相关阅读:
    opencv学习之米粒分割 #201906121549
    opencv学习之hsv通道分解 #201906101704
    opencv学习之图像滤波预处理 #201906101646
    opencv学习之addWeighted图片打水印 #201906061030
    alpha channel
    rm git commit history
    git 使用学习
    排序算法的c++实现
    leetcode 246 中心对称数问题
    大数打印问题
  • 原文地址:https://www.cnblogs.com/luowei010101/p/2078874.html
Copyright © 2011-2022 走看看