免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1101 | 回复: 0
打印 上一主题 下一主题

第四周:C语言 链表,联合,位字段 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:41 |只看该作者 |倒序浏览
1.课堂笔记:
2)、结构与函数

结构的合法操作只有几种:
作为一个整体复制和赋值,通过 & 运算符取地址,访问其成员。

结构指针
struct point *p;
p->结构成员

3)、自引用结构

malloc()
单链表

4)、表查找 (可选)

5)、类型定义

typedef int LENGTH;
typedef char *STRING;

typedef int (*PFI)(char *, char *);

6)、联合

联合提供了一种方式,在单块存储区中管理不同类型的数据。

union a {
    int a;
    float b;
    char *c;
} d;

联合名.成员
联合指针->成员

7)、位字段 (位域)

struct {
    unsigned int a : 1;
    unsigned int b : 2;
    unsigned int c : 1;
} flags;

位序的问题:
据说常有公司出有关位域的面试题。
不同机器的位序不一致。
X86平台从低到高。

例:
union {
    struct {
        char a:1;
        char b:2;
        char c:1;
    } s;
    char d;
} u;
u.d = 1;
printf("%d\n", u.s.a);


2.老师课堂代码:

::::::::::::::
cal.c
::::::::::::::
#include <stdio.h>

#include "stack.h"

int main(void)
{
      char str[] = "354*+6-";
      int i;
      int tmp;

      for (i = 0; str[i] != '\0'; i++) {
        switch (str[i]) {
        case '+':
          push(pop() + pop());
          break;
        case '-':
          tmp = pop();
          push(pop() - tmp);
          break;
        case '*':
          push(pop() * pop());
          break;
        case '/':
          tmp = pop();
          push(pop() / tmp);
          break;
        default:
          push(str[i] - '0');
          break;
        }
      }

      printf("%d\n", pop());

      return 0;
}
::::::::::::::
llist.c
::::::::::::::
#include <stdio.h>
#include <stdlib.h>

#define NAMESIZE 32
struct score {
      int id;
      char name[NAMESIZE];
      int math;
};

struct node_st {
      struct score data;
      struct node_st *next;
};

int insert(struct node_st **list, struct score *data)
{
      struct node_st *new;

      new = malloc(sizeof(*new));
      if (new == NULL) {
        return -1;
      }

      new->data = *data;
      new->next = *list;
      *list = new;

      return 0;
}

void travel(struct node_st *list)
{
      struct node_st *cur;

      for (cur = list; cur != NULL; cur = cur->next) {
        printf("%d %s %d\n", cur->data.id, cur->data.name, cur->data.math);
      }
}

struct score *find(struct node_st *list, int id)
{
      struct node_st *cur;

      for (cur = list; cur != NULL; cur = cur->next) {
        if (id == cur->data.id) {
          return &cur->data;
        }
      }
      return NULL;
}

int main(void)
{
      struct score tmp, *datap;
      struct node_st *list = NULL;
      int i;

      for (i = 0; i < 7; i++) {
        tmp.id = i;
        tmp.math = 100 - i;
        snprintf(tmp.name, NAMESIZE, "stu%d", i);

        insert(&list, &tmp);
      }
      travel(list);

      datap = find(list, 9);
      if (datap == NULL) {
        printf("Can not find.\n");
      } else {
        printf("%d %s %d\n", datap->id, datap->name, datap->math);
      }

      return 0;
}
::::::::::::::
score.c
::::::::::::::
#include <stdio.h>
#include <stdlib.h>

#define NAMESIZE 32
struct score {
      int id;
      char name[NAMESIZE];
      int math;
};

struct node_st {
      struct score data;
      struct node_st *next;
};

#define BUFSIZE 256
enum {INSERT = 1, DELETE, FIND, LIST, QUIT};

int insert(struct node_st **list, struct score *data)
{
      struct node_st *new;

      new = malloc(sizeof(*new));
      if (new == NULL) {
        return -1;
      }

      new->data = *data;
      new->next = *list;
      *list = new;

      return 0;
}

void delete(struct node_st **list, int id)
{
      struct node_st *cur, *save = NULL;

      for (cur = *list; cur != NULL; cur = cur->next) {
        if (id == cur->data.id) {
          if (save == NULL) {
            *list = cur->next;
          } else {
            save->next = cur->next;
          }
          free(cur);
          return;
        }
        save = cur;
      }

}

void travel(struct node_st *list)
{
      struct node_st *cur;

      for (cur = list; cur != NULL; cur = cur->next) {
        printf("%d %s %d\n", cur->data.id, cur->data.name, cur->data.math);
      }
}

struct score *find(struct node_st *list, int id)
{
      struct node_st *cur;

      for (cur = list; cur != NULL; cur = cur->next) {
        if (id == cur->data.id) {
          return &cur->data;
        }
      }
      return NULL;
}

void print_menu(void)
{
      printf("%d insert\n", INSERT);
      printf("%d delete\n", DELETE);
      printf("%d find\n", FIND);
      printf("%d list\n", LIST);
      printf("%d quit\n", QUIT);
      printf("Please enter a num: ");
      fflush(NULL);
}

int getcommand(void)
{
      char buf[BUFSIZE];

      fgets(buf, BUFSIZE, stdin);
      return atoi(buf);
}

int get_score(struct score *data)
{
      char buf[BUFSIZE];

      fgets(buf, BUFSIZE, stdin);
      sscanf(buf, "%d %s %d\n", &data->id, data->name, &data->math);

      return 0;
}

void insert_record(struct node_st **list)
{
      struct score tmp;

      printf("Input a record:\n");
      get_score(&tmp);
      insert(list, &tmp);
}

int get_id(void)
{
      char buf[BUFSIZE];

      fgets(buf, BUFSIZE, stdin);
      return atoi(buf);
}

void print_s(struct score *data)
{
      printf("%d %s %d\n", data->id, data->name, data->math);
}

void find_record(struct node_st *list)
{
      int id;
      struct score *datap;

      id = get_id();
      datap = find(list, id);
      if (datap == NULL) {
        printf("Can not find.\n");
      } else {
        print_s(datap);
      }
}

void delete_record(struct node_st **list)
{
      int id;

      printf("Enter id:\n");
      id = get_id();

      delete(list, id);
}

int main(void)
{
      struct node_st *list = NULL;
      int quit = 0;
      int command;

      while (quit == 0) {
        print_menu();

        command = getcommand();
        switch (command) {
        case INSERT:
          insert_record(&list);
          break;
        case DELETE:
          delete_record(&list);
          break;
        case FIND:
          find_record(list);
          break;
        case LIST:
          travel(list);
          break;
        case QUIT:
          quit = 1;
          break;
        }
        getchar();
        printf("\033[2J\033[1;1H");
        fflush(NULL);
      }

      return 0;
}
::::::::::::::
string.c
::::::::::::::
#include <stdio.h>
#include <string.h>

char *mystrcpy(char *dest, const char *src)
{
      char *save = dest;

      while (*dest++ = *src++)
        ;

      return save;
}

char *mystrcat(char *dest, const char *src)
{
      int i;

      for (i = 0; dest[i] != '\0'; i++)
        ;
      while (dest[i++] = *src++)
        ;

      return dest;
}

int mystrcmp(const char *s1, const char *s2)
{
      int i;

      for (i = 0; s1[i] != '\0' && s1[i] == s2[i]; i++)
        ;

      return s1[i] - s2[i];
}

int main(void)
{
      char dest[64] = "hello";
      char *src = "hello";

      //mystrcpy(dest, src);
      //printf("%s\n", dest);
      //printf("%s\n", strcpy(dest, src));
      printf("%d\n", mystrcmp(dest, src));

      return 0;
}


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP