Advanced programming language study notes Lecture 7 --C list

Lecture 7 list

1. Variable Array

//array.h
#ifndef _ARRAY_H_
#define _ARRAY_H_

typedef struct {
    int *array;
    int size;
} Array;

Array array_create(int init_size);
void array_free(Array *a);
int array_size(const Array *a);
int* array_at(Array *a, int index);
void array_inflate(Array *a, int more_size);

#endif

1.1 Variable Array

the Interface

Array array_create(int init_size) 
{
   Array a;
   a.array = (int*)malloc(sizeof(int)*init_size);
   a.size = init_size;
   return a;
}

void array_free(Array *a)
{
    free(a->array);
    a->array = NULL;
    a->size = 0;
}

1.2 Data Access array variable

//封装
int array_size(const Array *a)
{
    return a->size;
}

int* array_at(Array *a, int index)
{
    if ( index >= a->size ) {
        array_inflate(a, (index/BLOCK_SIZE+1)*BLOCK_SIZE-a->size); //BLOCK_SIZE=20;
    }
    return &(a->array[index]);
}

Automatic increase of 1.3 variable array

void array_inflate(Array *a, int more_size)
{
    int *p = (int*)malloc(sizeof(int)*(a->size + more_size))
    int i;
    for ( i=0; i<a->size; i++) {  
        p[i] = a->array[i];
    } //此循环可替换为标准库函数:memcpy,提高效率
    free(a->array);
    a->array = p;
    a->size += more_size;
}

1.4 The main function

//array.c
#include "array.h"
#include <stdio.h>
#include <stdlib.h>

const BLOCK_SIZE = 20;

int main()
{
    Array a = array_create(100); //创建初始数组
    *array_at(&a,0) = 10; //给第一个元素赋值
    int number=0;
    int cnt=0;
    while(number != -1) {
        scanf("%d", &number);
        if (number != -1)
            *array_at(&a, cnt++)=number; //写入当前位置
    }
    array_free(&a);
    
    return 0;
}

2. list

2.1 Variable array of defects

  • When an array of growth, each copy takes time (more and more)
  • Obviously enough memory, but can not apply for space ( the remaining space is not continuous )

2.2 Linked-list list

#ifndef _NODE_H_
#define _NODE_H_

typedef struct _node {
    int value;
    struct _node *next;
} Node;

#endif

2.3 list of functions

Example: Read the number of number (not know how many) and recorded, until the read -1.

#include"node.h"
#include<stdio.h>
#include<stdlib.h>

typedef struct _list {
    Node* head;
  //Node* tail; //可以改进代码
} List;

void add(List* pList, int number);
void print(List *plist);  
int main()
{
    List list;
    int number;
    list.head = NULL;
    do {
        scanf("%d", &number);
        if (number != -1) {
            head = add(&list, number);
        }
    } while (number != -1);   
    print(&list); //遍历输出
    
    /*****链表的搜索*****/
    scanf("%d", &number);
    Node *p;
    int isFound = 0;
    for ( p=list.head; p; p=p->next) {
        if ( p->value == number ) {
            printf("找到了\n");
            isFound = 1;
            break;
        }
    }
    if ( !isFound ) {
        printf("没找到\n");
    }
         
    return 0;
}

void add(List* pList, int number)
{
    //添加元素至链表
    Node *p = (Node*)malloc(sizeof(Node));
    p->value = number;
    p->next = NULL;
    //找到最后一个表
    Node *last = pList->head;
    if ( last ) {
        while ( last->next ) {
            last = last->next;
        }
        //将表链接起来
        last->next = p;
    } else {
        pList->head = p; //boundary situation
    }
}

void print(List *pList)  
{
	Node *p;
    /*链表的遍历*/
    for ( p=pList.head; p; p=p->next) {
        printf("%d\t", p->value);
    }
    printf("\n");
}

2.4 search list

List traversal:

void print(List *pList)  
{
	Node *p;
    /*链表的遍历*/
    for ( p=pList.head; p; p=p->next) {
        printf("%d\t", p->value);
    }
    printf("\n");
}

Search list:

scanf("%d", &number);
Node *p;
int isFound = 0;
for ( p=list.head; p; p=p->next) {
    if ( p->value == number ) {
        printf("找到了\n");
        isFound = 1;
        break;
    }
}
if ( !isFound ) {
    printf("没找到\n");
}

2.5 delete the list

Node *q;
for ( q=NULL, p=list.head; p; q=p, p=p->next) {
    if ( p->value == number) {
        if(q) {
            q->next = p->next;
        } else {
            list.head = p->next; //boundary situation
        }
        free(p);
        break;
    }
}

Use 指针how to find the boundary conditions?

When the pointer appears ->nextwhen the left side, you need to determine whether the pointer is not NULL, in order to ensure safety

2.6 Clear the list

for ( p=head; p; p=q) {
    q = p->next;
    free(p);
}
Published 58 original articles · won praise 101 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_43871127/article/details/104451463