单链表的操作和函数指针的基本使用

 ============================================================================
 Name        : TestLinkedList.c
 Author      : lf
 Version     :
 Copyright   : Your copyright notice
 Description : 1 单链表的增删改查等操作
               2 函数指针的使用.如示例中的:void (*visit)(link)
 ============================================================================
 */

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

//打印结点
void printNode(link p){
	printf("%d\n",p->item);
}

int main(void) {
	//插入结点
	link p = makeNode(10);
	insertNode(p);
	p = makeNode(8);
	insertNode(p);
	p = makeNode(6);
	insertNode(p);

	//遍历结点
	traverse(printNode);

	//查找结点
	link searchResult=searchNode(8);
	printf("%s","searchResult:");
	printNode(searchResult);

	//删除结点
	link deleteResult=deleteNode(searchResult);
	freeNode(deleteResult);
	traverse(printNode);
	printf("delete finish \n");

	//更新结点
	updateNode(6,99);
	traverse(printNode);
	printf("update finish \n");

	//销毁链表
	destroyLinkedlist();

	return EXIT_SUCCESS;
}



/*
 ============================================================================
 Name        : Linkedlist.h
 Author      : lf
 Version     :
 Copyright   : Your copyright notice
 注意的问题                  : 头文件(.h)的书写格式和惯例,比如此处头文件为Linkedlist.h,所以这么写:

               #ifndef LINKEDLIST_H
               #define LINKEDLIST_H
               ..................
               ..................
               ..................
               #endif
 ============================================================================
 */

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

typedef struct node *link;
struct node {
	unsigned char item;
	link nextNode;
};

//生成新的结点
link makeNode(unsigned char item);
//释放(free)结点
void freeNode(link p);
//向链表中插入结点
void insertNode(link p);
//删除链表中的结点
link deleteNode(link p);
//遍历链表
void traverse(void (*visit) (link));
//查找结点
link searchNode(unsigned char key);
//更新结点
void updateNode(unsigned char oldKey,unsigned char newKey);
//销毁链表
void destroyLinkedlist();

#endif

/*
 ============================================================================
 Name        : LinkedList.c
 Author      : lf
 Version     :
 Copyright   : Your copyright notice
   注意的细节                : 包含系统的头文件用的是<>包含自己的头文件用的是""
 ============================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include "Linkedlist.h"

static link head = NULL;

link makeNode(unsigned char item) {
	link p;
	p = malloc(sizeof *p);
	p->item = item;
	p->nextNode = NULL;
	return p;
}

void insertNode(link p) {
	p->nextNode = head;
	head = p;
}

//删除链表中的结点
link deleteNode(link p) {
	//删除的是头结点
	if (p == head) {
		head = p->nextNode;
		return p;
	}
	//删除的是除头结点以外的结点
	link preNode;
	for (preNode = head; preNode; preNode = preNode->nextNode) {
		if (preNode->nextNode == p) {
			preNode->nextNode = p->nextNode;
			return p;
		}
	}

	return NULL;
}

void freeNode(link p) {
	free(p);
}

/**
 * 遍历链表.
 * 注意此处的函数指针:
 * void (*visit)(link)
 * 函数的输入参数是link类型,返回值是void
 * 所以一般写成: 返回值 (*函数名)(输入参数)
 */
void traverse(void (*visit)(link)) {
	link p = NULL;
	for (p = head; p; p = p->nextNode) {
		visit(p);
	}
}

link searchNode(unsigned char key) {
	link p = NULL;
	for (p = head; p; p = p->nextNode) {
		if (p->item == key) {
			return p;
		}
	}
	return NULL;
}

void updateNode(unsigned char oldKey, unsigned char newKey) {
	link p = NULL;
	for (p = head; p; p = p->nextNode) {
		if (p->item == oldKey) {
			p->item = newKey;
		}
	}
}

void destroyLinkedlist(){
	link q;
	link p=head;
	head=NULL;
	while(p){
		q=p;
		p=p->nextNode;
		freeNode(q);
	}

}




上一篇:[转]delphi xe6 android屏幕操持常亮


下一篇:策略模式