恩,准备重最简单的东西复习和学习一遍。那就从数据结构开始吧。
我尽量用公司的代码标准来进行编码。希望对刚刚开始学习数据结构的有一些帮助吧。
使我的代码可以简单易懂。
我记得上数据结构的第一个程序就是链表。
首先,解释一下ADT(abstract data type)。
这个东西我一开始学习的时候感觉很诡异。搞了好久都没有知道。其实很简单。我个人认为是这样的。在c里面没有面向对象的说法。在真实情况下一些操作(函数)是对于特定的结构体进行的。因为没有类的概念。所以,就人为的提出了一个概念。把带有一组操作的一些对象的集合叫ADT。在说简单点,就是操作同一个结构体的函数和这个结构体叫做ADT。
这个是我当初学习时候遇到的困惑。现在写出来跟大家分享一下。如果哪里有误还望大家指出。
接下来,我要实现一个简单的单向链表。由于很简单,而且我的代码上有注释。我就不解释了。一个图就说明:
我的实现分为3个文件
onewaylist.h
- /*FILE***********************************************************************
- 文件名:onewaylist.h
- 编写者: Killer_yu
- 编写日期: 2010/8/24
- 简要描述: 定义单向链表的头文件
- 主要函数列表:
- 修改日志:
- ***********************************************************************FILE*/
- #ifndef ONEWAYLIST_H
- #define ONEWAYLIST_H
- #include
- #include
- /*STR************************************************************************
- 结构名称:oneWayList
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:定义单向链表结构
- 属性:大小size,指向头节点指针,指向尾节点指针。
- 修改日志:
- ************************************************************************STR*/
- typedef struct oneWayList{
- int size;
- struct Node *head;
- struct Node *tail;
- }oneWayList;
- /*STR************************************************************************
- 结构名称:Node
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:定义单向链表结构
- 属性:数据element,指向下一个节点的指针。
- 修改日志:
- ************************************************************************STR*/
- typedef struct Node{
- int element;
- struct Node *next;
- }Node;
- void initList(struct oneWayList *);
- int getSize(struct oneWayList *);
- int empty(struct oneWayList *);
- void push_back(struct oneWayList *, struct Node *);
- void pop_back(struct oneWayList *, struct Node *);
- void print_List(struct oneWayList *);
- void list_remove(struct oneWayList *, int);
- #endif
onewaylist.c
- /*FILE***********************************************************************
- 文件名:onewaylist.h
- 编写者: Killer_yu
- 编写日期: 2010/8/24
- 简要描述: 实现单向链表的c文件
- 主要函数列表:
- 修改日志:
- ***********************************************************************FILE*/
- #include "onewaylist.h"
- /*FUN************************************************************************
- 函数名称:void initList(struct oneWayList *list)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:初始化单向链表
- 输入:指向单向链表结构体的指针
- 输出:无
- 修改日志:
- ************************************************************************FUN*/
- void initList(struct oneWayList *list)
- {
- list->size = 0;
- list->head = NULL;
- list->tail = NULL;
- }
- /*FUN************************************************************************
- 函数名称:int getSize(struct oneWayList *list)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:获得链表的大小
- 输入:指向单向链表结构体的指针
- 输出:链表大小
- 修改日志:
- ************************************************************************FUN*/
- int getSize(struct oneWayList *list)
- {
- return list->size;
- }
- /*FUN************************************************************************
- 函数名称:int empty(struct oneWayList *list)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:判断链表是否为空
- 输入:指向单向链表结构体的指针
- 输出:链表为空为返回1,链表不空返回0
- 修改日志:
- ************************************************************************FUN*/
- int empty(struct oneWayList *list)
- {
- if( 0 == list->size)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- /*FUN************************************************************************
- 函数名称:void push_back(struct oneWayList *list, struct Node *node)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:向链表表尾加入一个节点
- 输入:指向单向链表结构体的指针,需要加入的节点指针
- 输出:无
- 修改日志:
- ************************************************************************FUN*/
- void push_back(struct oneWayList *list, struct Node *node)
- {
- list->size++;
- node->next = NULL;
- if( NULL == list->head)
- {
- list->head = node;
- list->tail = node;
- }
- else
- {
- list->tail->next = node;
- list->tail = node;
- }
- }
- /*FUN************************************************************************
- 函数名称:void pop_back(struct oneWayList *list,struct Node *node)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:向链表表尾弹出一个节点
- 输入:指向单向链表结构体的指针,需要弹出的节点指针
- 输出:无
- 修改日志:
- ************************************************************************FUN*/
- void pop_back(struct oneWayList *list,struct Node *node)
- {
- struct Node *tempNode = (struct Node *)malloc(sizeof(struct Node));
- if ( empty(list) )
- {
- printf("List is empty!!\n");
- }
- else
- {
- list->size--;
- tempNode = list->head;
- node = list->tail;
- while(tempNode->next->next != NULL )
- {
- tempNode = tempNode->next;
- }
- tempNode->next = NULL;
- list->tail = tempNode;
- }
- }
- /*FUN************************************************************************
- 函数名称:void print_List(struct oneWayList *list)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:打印链表
- 输入:指向单向链表结构体的指针
- 输出:屏幕输出链表
- 修改日志:
- ************************************************************************FUN*/
- void print_List(struct oneWayList *list)
- {
- struct Node *node;
- node = list->head;
- while(node->next)
- {
- printf("%d ", node->element);
- node = node->next;
- }
- printf("%d\n",node->element);
- }
- /*FUN************************************************************************
- 函数名称:void list_remove(struct oneWayList *list, int element)
- 编写人:Killer_yu
- 编写日期:2010/8/24
- 功能描述:删除指定元素
- 输入:指向单向链表结构体的指针,指定元素
- 输出:无
- 修改日志:
- ************************************************************************FUN*/
- void list_remove(struct oneWayList *list, int element)
- {
- struct Node *tempNode;
- tempNode = list->head;
- while( tempNode->next )
- {
- if( element == tempNode->next->element )
- {
- list->size--;
- tempNode->next = tempNode->next->next;
- break;
- }
- tempNode = tempNode->next;
- }
- printf("No element of the list\n");
- }
onelistmain.c
- /*FILE***********************************************************************
- 文件名:onelistmain.c
- 编写者: Killer_yu
- 编写日期: 2010/8/24
- 简要描述: 测试单向链表
- 主要函数列表:
- 修改日志:
- ***********************************************************************FILE*/
- #include "onewaylist.h"
- int main(int argv, char argc[])
- {
- int temp;
- struct oneWayList list;
- struct Node *tempNode;
- initList(&list);
- /*测试空链表是弹出是否会错误*/
- pop_back(&list, tempNode);
- /*向链表加入10个节点*/
- for(temp = 0; temp < 10; temp++)
- {
- tempNode = (struct Node*) malloc(sizeof(struct Node));
- tempNode->element = temp;
- push_back(&list, tempNode);
- }
- /*打印链表*/
- print_List(&list);
- /*看一看链表长度*/
- printf("%d\n",getSize(&list));
- /*测试一下弹出函数*/
- pop_back(&list, tempNode);
- printf("%d\n", tempNode->element);
- /*打印是否链表正确*/
- print_List(&list);
- /*测试remove*/
- list_remove(&list,4);
- print_List(&list);
- return 0;
- }