linux内核学习准备工作-链表回顾(1)

麻省理工-MIT:c内存管理课程

推荐博客:程序员技术练级攻略

[root@localhost c_memery_course-mit]# ls
lec02.zip
[root@localhost c_memery_course-mit]# unzip lec02.zip
Archive: lec02.zip
inflating: singly_linked_list.c
inflating: singly_linked_list.h
[root@localhost c_memery_course-mit]# ls
lec02.zip singly_linked_list.c singly_linked_list.h
[root@localhost c_memery_course-mit]# cat singly_linked_list.h

/*
 * Define a node structure.
 * 
 * Doing this is the same as doing:
 * struct node {
 *    int val;
 *    struct node* next;
 * };
 * typedef struct node node_t;
 */
typedef struct node {
  int val;
  struct node* next;
} node_t;

/*
 * Creates a new node from a given value, allocating heap memory for it.
 */
node_t* make_node(int val);

/*
 * Inserts a new value into a given linked list, allocating heap memory for
 * it.
 */
node_t* insert_val(int val, node_t* cur_head);

/*
 * Deletes the first value with the given value, deallocating the associated
 * memory.  Sets the "succeeded" flag to 1 if it found and deleted the value, 0
 * otherwise.
 */
node_t* delete_val(int val, node_t* cur_head, int* succeeded);

/*
 * Given a pointer to the head, frees the memory associated with an entire list.
 */
void delete_list(node_t* head);

/* Given a pointer to the head, prints all o fthe values in a list. */
void print_sll(node_t* head);

[root@localhost c_memery_course-mit]# cat singly_linked_list.c

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

/* Returns a new node with the given value. */
node_t* make_node(int val) {
  node_t* new_node = malloc(sizeof(node_t));
  new_node->val = val;
  new_node->next = NULL;
  return new_node;
}

/* Insert at the head of a linked list. */
node_t* insert_val(int val, node_t* cur_head) {
  node_t* new_node = make_node(val);
  new_node->next = cur_head;
  return new_node;
}

/* Deletes the first occurence of value val from the list. */
node_t* delete_val(int val, node_t* cur_head, int* succeeded) {
  *succeeded = 0;

  /* Special case: if first guy is the one we want to remove. */
  if (cur_head == NULL) {
    return NULL;
  } else if (cur_head->val == val) {
    node_t* new_head = cur_head->next;
    free(cur_head);
    *succeeded = 1;
    return new_head;
  } else {
    node_t* prev = cur_head;
    node_t* cur = cur_head->next;
    while (cur != NULL) {
      if (cur->val == val) {
        prev->next = cur->next;
        free(cur);
        *succeeded = 1;
        return cur_head;
      } else {
        cur = cur->next;
      }
    }
  }
  return cur_head;
}

/* Frees the memory associated with a linked list. */
void delete_list(node_t* head) {
  node_t* cur = head;
  node_t* next;

  while (cur != NULL) {
    /* Store our next one. */
    next = cur->next;

    /* Free the current guy. */
    free(cur);

    /* Move on. */
    cur = next;
  }
}

/* Prints a linked list. */
void print_sll(node_t* head) {
  node_t* cur = head;
  while (cur != NULL) {
    printf ("%d ", cur->val);
    cur = cur->next;
  }
  printf("\n");
  return;
}

[root@localhost c_memery_course-mit]# cat main.c

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

int main()
{
        printf("hello world!\n");
        node_t *head =NULL;

        head = make_node(1);
        head = insert_val(2,head);
        head = insert_val(3,head);
        head = insert_val(4,head);
        head = insert_val(5,head);
        head = insert_val(6,head);
    print_sll(head);

}

编译方式1:动态库
[root@localhost c_memery_course-mit]#
[root@localhost c_memery_course-mit]# gcc singly_linked_list.c -fPIC -shared -o liblist.so
[root@localhost c_memery_course-mit]# gcc main.c -L. -llist -o main-1
[root@localhost c_memery_course-mit]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
[root@localhost c_memery_course-mit]# ldd main-1
linux-vdso.so.1 => (0x00007fff3b1fe000)
liblist.so (0x00007f0c5bed6000)
libc.so.6 => /lib64/libc.so.6 (0x00007f0c5bb01000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0c5c0d9000)
[root@localhost c_memery_course-mit]# ./main-1
hello world!
6 5 4 3 2 1

注:动态库编译参数说明
-fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。

-L.:表示要连接的库在当前目录中

-llist:编译器查找动态连接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来确定库的名称

LD_LIBRARY_PATH:这个环境变量指示动态连接器可以装载动态库的路径。

linux环境变量配置文件/etc/profile

/etc/ld.so.conf文件指定动态库的连接地方,然后调用/sbin/ldconfig来达到同样的目的


编译方式2:静态库
[root@localhost c_memery_course-mit]# gcc main.c singly_linked_list.c -o main-2
[root@localhost c_memery_course-mit]# ./main-2
hello world!
6 5 4 3 2 1
[root@localhost c_memery_course-mit]# ls
lec02.zip liblist.so main-1 main-2 main.c singly_linked_list.c singly_linked_list.h
[root@localhost c_memery_course-mit]#

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值