关于15桥梁课程1&2的笔记以及待做事项的梳理

1.指针所占用的空间是固定的

2.void *malloc(sizeof(int)); (这玩意耗时间,老师说通过内存池解决)

  free(p);free(p);   两次free()报错,正确的做法:

  if(p!=NULL){free(p);p=NULL;}

3.传值与传址的理解应转换为是否对其空间进行操作

4.函数指针:

  作用:ex: 不同厂商设备函数不同,操作系统定义设备抽象 device结构体 实现函数跳转

  通用形式: 返回类型 (* 函数指针变量)(形参列表)
  只能指向………………

  而指针函数 :返回值为指针类型

  

1 #include<stdio.h>
  2 void fun1(int *a)
  3 {
  4         int i;
  5         for(i=0;i<10;i++)
  6                 printf("%d\n",a[i]);
  7 }
  8 void fun2(int *a)
  9 {
 10         int i;
 11         for(i=0;i<10;i++)
 12                 printf("%d\n",10*a[i]);
 13 }
 14 void fun3(int *a,void (*f)(int *))
 15 {
 16         f(a);
 17 }
 18 int main()
 19 {
 20         int a[10]={1,2,3,4,5,6,7,8,9,10};
 21 //      fun1(a);
 22 //      fun2(a);
 23         fun3(a,fun1);
 24         fun3(a,fun2);
 25         return 0;
 26 }

5.数组与指针  (为何*(*(a+2)+1)有效) 

int data[3][4]   data的type为int (*)[4]  *data的type为int*  data[1]的type为 int *

data++   data=data+1*sizeof(int)//wrong

int *p;
p=data;
for(;p<data+12;p++)
{
printf("%d",*p);
}


void fun(int a[][5])
b[5]/b[]
void fun(int *a,int m,int n)
{
}//a[i][j]______________*(a+i*sizeof(n)+j)
main()
{
int data[3][3]={};
fun(data);
}

参考链接:http://blog.sina.com.cn/s/blog_6b1695860100q84k.html   http://blog.sina.com.cn/s/blog_5c6f793801019t3t.html

6.单向链表相关(by myself)

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 typedef struct node
  5 {
  6     int data;
  7     struct node *next;
  8 }Node;
  9 
 10 Node *Creat_link(Node*,int *,int);
 11 void Display(Node*);
 12 Node *FindPre(Node*,int);
 13 void Deleteone(Node*,int);
 14 void Deletemore(Node*,int);
 15 void Freeroom(Node*);
 16 Node *Reserve(Node*);
 17 void Addnode(Node*,int);
 18 Node *Combine(Node*,Node*);
 19 int main()
 20 {
 21     Node *head=NULL;
 22     Node *head2=NULL;
 23     Node *head3=NULL;
 24     int a[6]={1,4,4,7,8,9};
 25     int b[4]={2,3,6,9};
 26     head=Creat_link(head,a,6);
 27 //    head2=Creat_link(head2,b,4);
 28 //    head3=Combine(head,head2);//合并
 29 //    Display(head3);
 30 //    Deleteone(head,a[1]);//删除一个
 31 //    Display(head);
 32 //    Deletemore(head,a[2]);//可删除重复
 33 //    Display(head);
 34 //    Addnode(head,5);//增加一个节点
 35 //    Display(head);
 36     Reserve(head);//倒序
 37     Display(head);
 38     Freeroom(head);
 39     return 0;
 40 }
 41 
 42 Node *Creat_link(Node *h,int *a,int n)
 43 {
 44     int i;
 45     h=(Node *)malloc(sizeof(Node));
 46     
 47     if(h==NULL)
 48     {printf("Not enough memory\n");}
 49 
 50     h->next=NULL;
 51     Node *s=h;
 52     Node *p=NULL;
 53     for (i=n;i>0;i--)
 54     {
 55         p=(Node *)malloc(sizeof(Node));
 56         p->next=h->next;
 57         h->next=p;
 58         p->data=a[i-1];
 59     }
 60     return h;
 61 }
 62 
 63 void Display(Node *h)
 64 {
 65     Node *p=NULL;
 66     p=h;
 67     while(p->next!=NULL)
 68     {
 69     p=p->next;
 70     printf("%d ",p->data);
 71     }
 72     putchar('\n');
 73 }
 74 void Freeroom(Node *h)
 75 {
 76     Node *p=h->next;
 77     Node *tem=NULL;
 78     h->next=NULL;
 79     while(p!=NULL)
 80     {
 81     tem=p->next;
 82     if(tem!=NULL)
 83         {
 84         free(tem);
 85         tem=NULL;
 86         }
 87     p=tem;
 88     }
 89 }
 90 
 91 Node *Reserve(Node *h)
 92 {
 93     Node *p=NULL;
 94     Node *pnext=NULL;
 95     Node *q=NULL;
 96     if(h==NULL||h->next==NULL)
 97     return h;
 98     p=h->next;
 99     pnext=p->next;
100     p->next=NULL;
101     while(pnext!=NULL)
102     {
103         q=pnext->next;
104         pnext->next=p;
105         p=pnext;
106         pnext=q;
107     }
108     h->next=p;
109     return h;
110 }
111 Node *FindPre(Node *h,int n)
112 {
113     Node *p=h;
114     while(p->next!=NULL&&p->next->data!=n)
115     p=p->next;
116     return p;
117 }
118 void Deleteone(Node *h,int n)
119 {
120     Node *p=FindPre(h,n);
121     Node *temp=NULL;
122     if(p->next!=NULL)
123     {    
124         temp=p->next;
125         p->next=temp->next;
126         if(temp!=NULL){
127             free(temp);
128             temp=NULL;
129         }
130         
131     }
132     else
133     {printf("Not find!\n");}
134 }    
135 
136 void Deletemore(Node *h,int n)
137 {    
138     Node *p=FindPre(h,n);
139     Node *q=p->next;
140     Node *temp=NULL;
141     while(q->next!=NULL&&q->next->data==n)
142     {    
143         temp=q;
144         q=q->next;
145         if(temp==NULL){
146         free(temp);
147         temp=NULL;}
148     }
149     p->next=q->next;
150     if(q!=NULL){
151         free(q);
152         q=NULL;
153     }
154 }
155 
156 void Addnode(Node *h,int data)
157 {
158     Node *p=h;
159     Node *new=(Node *)malloc(sizeof(Node));
160     while(p->next!=NULL&&data>p->next->data)
161     {p=p->next;}
162     if(p->next==NULL)
163     {
164         p->next=new;
165         new->next=NULL;
166     }
167     else
168     {
169         new->next=p->next;
170         p->next=new;
171     }
172     new->data=data;
173 }
174 
175 Node *Combine(Node *h1,Node *h2)
176 {
177     Node *p=h1;
178     Node *q=h2;
179     Node *temp1,*temp2,*temp3;
180     Node *h3=(h1->next->data<h2->next->data)?h1:h2;
181     while(q->next!=NULL&&p->next!=NULL)
182     {
183         temp2=q->next;
184         while(q->next!=NULL&&q->next->data<=p->next->data)
185         q=q->next;
186     
187         temp1=p->next;
188         while(p->next!=NULL&&p->next->data<=q->next->data)
189         p=p->next;
190         
191         temp1=p->next;
192         p->next=temp2;
193         temp2=q->next;
194         q->next=temp1;
195     }
196     return h3;
197 }

8.双向链表

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct Node
 4 {
 5     int data;
 6     struct Node *front;
 7     struct Node *rear;
 8 }BNode;
 9 
10 void create(BNode **head1,BNode **head2)
11 {
12     int m;
13     scanf("%d",&m);
14     BNode *p=NULL,*q=*head2;
15     while(m>0)
16     {    
17         p=(BNode*)malloc(sizeof(BNode));
18         p->data=m;
19         p->front=NULL;
20         p->rear=NULL;
21         
22         if(*head1==NULL)
23         {
24             *head1=p;
25             *head2=p;
26         }
27         else
28         {
29             p->rear=*head1;
30             p->front=NULL;
31             (*head1)->front=p;
32             *head1=p;        
33         }
34         scanf("%d",&m);
35     }
36 }
37 void display(BNode **head1)
38 {
39     BNode *h=*head1;
40     while(h!=NULL)
41     {
42         printf("%d  ",h->data);
43         h=h->rear;
44     }
45 }
46 
47 void addnode(BNode **head,int m)//插入m
48 {
49     BNode *p=*head;
50     BNode *q=(BNode*)malloc(sizeof(BNode));
51     q->data=m;
52     while(p!=NULL&&p->data<m)
53     {
54 //        printf("%d__",p->data);
55         p=p->rear;
56     }
57     if(p!=NULL)
58     {
59         q->rear=p;
60         q->front=p->front;
61         p->front->rear=q;
62         p->front=q;
63         ;
64     }
65 }
66 int main()
67 {
68     BNode *head1=NULL,*head2=NULL;
69     create(&head1,&head2);
70     display(&head1);
71     addnode(&head1,5);
72     printf("\n");
73     display(&head1);
74     return 0;
75 }

 

9. 栈 队列

//链栈
#include<stdio.h> #include<stdlib.h> typedef struct node { struct node *next; int data; }Node; typedef struct stack { Node *top; }Stack; void Init(Stack *s) { s->top=NULL; } void Push(Stack *s,int m) { Node *p=(Node *)malloc(sizeof(Node)); p->data=m; p->next=s->top; s->top=p; } int Pop(Stack *s,int *m) { Node *p; if(s->top==NULL) return 0; *m=s->top->data; p=s->top; s->top=p->next; free(p); return 1; } int main() { int m=3; Stack *s; Push(s,3); Push(s,2); if(Pop(s,&m)); { printf("%d\n",m); } getch(); return 0; }

 

#include<stdio.h>//
#include<stdlib.h>

typedef struct node 
{
    int top;
    int data[10];
}Stack;


void Init(Stack *s)
{
    s->top=-1;
}

int Push(Stack *s,int m)
{
    if(s->top>=9) return 0;
    s->data[++(s->top)]=m;
    return 1;
}

int Pop(Stack *s,int *m)
{    
    
    if(s->top==-1) return 0;
    *m=s->data[s->top--];
    return 1;
}
int main()
{
    int m=3;
    Stack *s;
    Init(s);
    Push(s,3);
    Push(s,2);
    if(Pop(s,&m));
    {
        printf("+++%d+++\n",m);
    }
    if(Pop(s,&m));
    {
        printf("+++%d+++\n",m);
    }
    getch();
    return 0;
}

 

10.通用链表

11.二叉树的遍历(递归与非递归)

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct BTNode
 4 {    
 5     int data;
 6     struct BTNode *Lchild;
 7     struct BTNode *Rchild;
 8 } BTNode;
 9 
10 BTNode *CreateTree()//以递归的方式创建
11 {
12     int data;
13     BTNode *T;
14     
15     printf("Please input a data.");
16     scanf(" %d",&data);
17     if(data=='#')
18         T=NULL;
19     else
20         {    
21                 BTNode *P=(BTNode*)malloc(sizeof(BTNode));
22                 P->data=data;
23                 P->Rchild=CreateTree();
24                 P->Lchild=CreateTree();
25         }
26     return T;
27 } 
28 void Preorder(BTNode *T)//递归方式的前序遍历
29 {    
30     if(T!=NULL)
31     {
32         printf("%d  ",T->data);
33         Preorder(T->Lchild);
34         Preorder(T->Rchild);
35     }
36 }37 int main()
38 {
39     BTNode *T=NULL;
40     T=CreateTree();
41     Preorder(T);
42     
43     return 0;
44 }

 

12.make file  

  参考链接:跟我一起写makefile http://blog.csdn.net/liang13664759/article/details/1771246

13.静态库  动态库  

  参考资料:http://blog.chinaunix.net/uid-23069658-id-3142046.html

14.哈希表

 

posted on 2015-08-04 16:07  Degrone  阅读(179)  评论(0编辑  收藏  举报

导航