• 指针,函数指针,结构体,链表


    • 指针

    指针数组:很多指针的数组

    数组指针:指向数组的指针

    int arr [10];

    arr 首元素地址

    &arr  数组地址

    • 函数指针
     1 #include <stdio.h>
     2 
     3 void Func();
     4 void Func2(void (*p)());
     5 int main()
     6 {
     7     void (*p)() = &Func;
     8     void (*pp)(void (*c)()) = Func2;
     9     (*p)();
    10     (*pp)(*p);
    11 }
    12 void Func()
    13 {
    14     printf("%s
    ","qwertyuio");
    15 }
    16 void Func2(void (*p)())
    17 {
    18     (*p)();
    19 }

    应用函数指针实现对数组既可以升序也可以降序

     1 #include <stdio.h>
     2 #define LENGTH(arr) sizeof(arr)/sizeof(arr[0])
     3 
     4 void BubbleSort(int* arr, int nLength,int(*fp)(int , int ));
     5 int bigger(int , int );
     6 int smaller(int ,int );
     7 int Find(int* arr, int nLength,int num);
     8 
     9 int main()
    10 {
    11     int arr[10] = {1,2,3,4,5,6,7,8,9};
    12     int len = LENGTH(arr);
    13     //BubbleSort(arr,10,bigger);
    14     printf("%d
    ",len);
    15     printf("%d
    ", Find(arr,9,8));
    16     return 0;
    17 }
    18 
    19 void BubbleSort(int* arr, int nLength,int(*fp)(int , int ))
    20 {
    21     int i;
    22     int j;
    23     int temp;
    24      for(i = 0 ; i < nLength - 1;i++)
    25      {
    26         for(j = i + 1; j < nLength; j++)
    27         {
    28             if(   (*fp)( arr[i] , arr[j])  )
    29             {
    30                 temp = arr[j];
    31                 arr[j] = arr[i];
    32                 arr[i] = temp;
    33         }
    34         }
    35      }
    36 
    37     /*for(i=0;i<nLength-1;i++)
    38     {
    39         for(j=0;j<nLength-i-1;j++)
    40         {
    41             if(    (*fp)(arr[j] , arr[j+1])    )
    42             {
    43                 temp = arr[j];
    44                 arr[j] = arr[j+1];
    45                 arr[j+1] = temp;
    46             }
    47         }
    48     }*/
    49 }
    50 int bigger(int a , int  b)
    51 {
    52     return a > b;
    53 }
    54 int smaller(int a ,int b)
    55 {
    56     return a < b;
    57 }
    58 int Find(int* arr, int nLength,int num)
    59 {
    60     int left = 0;
    61     int right =nLength -1;
    62     int p = (left + right ) / 2;
    63     while(arr[p] != num)
    64     {
    65         if(arr[p] > num)
    66             right = p;
    67         else 
    68             left = p;
    69         p = (left + right ) / 2;
    70     }
    71     return p;
    72 }
    冒泡和二分
    • 结构体

    第一种

    struct NODE
    {
        int xh;
        char* name;
        char* phone;
    };
    typedef struct NODE Node ;//起别名

    第二种:

    struct NODE
    {
        int xh;
        char* name;
        char* phone;
    }Node;

    可通过结构体地址修改其成员变量

    Node c = {3,"ccc","119"};
    Node *p = &c;
    p->name = "ppp";
    printf("%s
    ", p->name);

     结构体对齐

    • 链表
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 typedef struct NODE
      5 {
      6     int xh;
      7     struct NODE  * pnext;//这里不能用别名,因为还没起。
      8 }node;
      9 void Addnode(node **, node**);
     10 void Addindexnode(node **, node **,int );
     11 void Traverse(node *);
     12 void Delete(node** ,node** , int);
     13 int getXh();
     14 int main()
     15 {
     16     node *pHead =    NULL;
     17     node *pEnd = NULL;
     18     Addnode(&pHead, &pEnd);
     19     Addnode(&pHead, &pEnd);
     20     Addnode(&pHead, &pEnd);
     21     Addnode(&pHead, &pEnd);
     22     Addindexnode(&pHead, &pEnd,4);
     23     Delete(&pHead ,&pEnd , 4);
     24     Traverse(pHead);
     25     return 0;
     26 }
     27 void Addnode(node **pHead,node **pEnd)
     28 {
     29     //申请新节点
     30     node* temp = (node *)malloc(sizeof(node));
     31     temp->pnext = NULL;
     32     temp->xh = getXh();
     33     //第一次添加
     34     if(*pHead == NULL)
     35     {
     36         *pHead = temp;
     37         *pEnd = temp;
     38     }
     39     else 
     40     {
     41         (*pEnd)->pnext = temp;
     42         *pEnd = temp;
     43     }
     44 }
     45 void Addindexnode(node **pHead, node **pEnd,int index)
     46 {
     47     node *bj = *pHead;
     48     node* temp = (node *)malloc(sizeof(node));
     49     temp->xh = getXh();
     50     temp->pnext = NULL;
     51 
     52     //
     53     if((*pHead)->xh == index)
     54     {
     55         temp->pnext = *pHead;
     56         *pHead = temp;
     57         return ;
     58     }
     59     //中间
     60     //while(bj->pnext->xh != index &&bj->pnext != *pEnd)
     61     //{
     62     //    bj = bj->pnext;
     63     //}
     64     //if(bj->pnext->xh == index)
     65     //{
     66     //    temp->pnext = bj->pnext;
     67     //    bj->pnext = temp;
     68     //    return;
     69     //}
     70 
     71 
     72     //while(bj->pNext->xh != i)
     73     //{
     74     //    bj = bj->pNext;
     75     //    if(bj == *pEnd)            //尾添加
     76     //    {
     77     //        (*pEnd)->pNext = temp;
     78     //        *pEnd = temp;
     79     //        return;
     80     //    }
     81     //}
     82     //temp->pNext = bj->pNext;    //中间添加
     83     //bj->pNext = temp;
     84     //return;
     85 
     86 
     87     while(bj->pnext != NULL) //中间
     88     {
     89         if(bj->pnext->xh == index)
     90         {
     91             temp->pnext = bj->pnext;
     92             bj->pnext = temp;
     93             return;
     94         }
     95         bj = bj->pnext;
     96     }
     97     //
     98         (*pEnd)->pnext = temp;
     99         *pEnd = temp;
    100         return;
    101 }
    102 void Delete(node **pHead ,node** pEnd, int xh)
    103 {
    104     node* bj = *pHead;
    105     node* p; 
    106     if((*pHead)->xh == xh)
    107     {
    108         *pHead = (*pHead)->pnext;
    109         bj = *pHead;
    110         return;
    111     }
    112     while(bj->pnext->pnext != NULL )
    113     {
    114         if(bj->pnext->xh == xh)
    115         {
    116             p = bj->pnext;
    117             bj->pnext = bj->pnext->pnext;    
    118             free(p);
    119             return;
    120         }
    121         bj = bj->pnext;
    122     }
    123     p = bj->pnext;
    124     free(p);
    125     bj->pnext = NULL;
    126     *pEnd = bj;
    127 }
    128 void Traverse(node *pHead)
    129 {
    130     while(pHead != NULL)
    131     {
    132         printf("%d
    ",pHead->xh);
    133         pHead = pHead->pnext;
    134     }
    135 }
    136 int getXh()
    137 {
    138     static int i = 1;
    139     return i++;
    140 }
    链表的基本操作
  • 相关阅读:
    Beetl 3中文文档 转载 http://ibeetl.com/guide/
    Beetl模板引擎入门教程
    Spring+Stomp+ActiveMq实现websocket长连接
    5672端口引发的一个大坑
    GeoServer中WMS、WFS的请求规范
    常用网址
    JAVA方法参数传递
    针对开发的缺陷管理
    不同逻辑顺序产生相同的结果编码如何决策
    怎样做一个软件项目经理
  • 原文地址:https://www.cnblogs.com/Lune-Qiu/p/7725796.html
Copyright © 2020-2023  润新知