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

  • 指针

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

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

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\n","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\n",len);
15     printf("%d\n", 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\n", 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\n",pHead->xh);
133         pHead = pHead->pnext;
134     }
135 }
136 int getXh()
137 {
138     static int i = 1;
139     return i++;
140 }
链表的基本操作

 

转载于:https://www.cnblogs.com/Lune-Qiu/p/7725796.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值