Two Day 单链表

  • Post author:
  • Post category:其他



目录


1)定义理解(—-)


定义:


特点:


单链表的头尾


2)代码各功能实现


1.创建节点、头指针、节点指针


2.创建链表


3.从尾巴插入节点


4.按照指定位置插入


5.按照给定数据插入


6.删除指定数据节点


7.求前驱


8.求指定数据类型最大值


9.清空单链表


3)地址访问


4)总结:


5)总代码:


1)定义理解(自我乱说)

定义:

单向的连接的数据存储结构。

特点:

  • 由多个节点通过指针连接而成;只有一个方向;(如图所示)

  • 每个节点分为俩个部分,及数据域和指针域;(如图所示)



  • 数据域


    :存储数据,比如放一个或多个整数类型,放一个或多个不同类型数据都行(这里代码演示只放一个int类型,为了方便)。


  • 指针域


    :存放一个指针,用于指向下一个节点,我愿称它为单链表的脊髓。


  • 地址存储特点


    :一个节点的数据域和指针域是存储在一个连续的空间上的,一个单链表的不同节点的空间是随机分配的,一般不是连续的空间。(如图所示)

单链表的头尾

对于每个单链表我们会定义一个头指针来指向链表的头部,以后要对单链表进行处理时,给他头指针即可,另外一般我们会把指向NULL的节点称为单链表的尾部,当然我们也可以通过定义一个尾指针来指向尾部。

  • 头指针可以是数据域为空的节点指针。

2)代码各功能实现

1.创建节点、头指针、节点指针


代码实现

typedef struct LinkNode{
	char data;
	struct LinkNode *next;
} LNode, *LinkList, *NodePtr;//节点/链表指针/节点指针 

2.创建链表


代码实现

LinkList initLinkList(){
	NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));//申请一个节点大小空间,转化为节点指针类型,用作头 
	tempHeader->data = '\0';
	tempHeader->next = NULL;
	return tempHeader; 
} 

3.从尾巴插入节点


图解




代码实现

void appendElement(NodePtr paraHeader, char paraChar){
	NodePtr tempP, tempQ;
	
	//Step 1.Construct a new node.
	tempQ = (NodePtr)malloc(sizeof(LNode));
	tempQ->data = paraChar;
	tempQ->next = NULL;
	//Step 2.Search to the tail.
	tempP = paraHeader;
	while(tempP->next != NULL){
		tempP = tempP->next;
	}//of while
	//Step 3.Now add/link.
	tempP->next = tempQ;
}//of appendElement


测试代码

void appendElementTest(){
	printf("--- appendElementTest Begin ---\n");
	LinkList tempHeader;
	tempHeader = initLinkList();
	appendElement(tempHeader, 'H');
	appendElement(tempHeader, 'E');
	appendElement(tempHeader, 'L');
	appendElement(tempHeader, 'L');
	appendElement(tempHeader, 'O');
	appendElement(tempHeader, '!');
	printList(tempHeader);
	printf("--- appendElementTest Begin ---\n");
}//of appendElementTest


由于每次要去找尾巴,所以创建一个尾指针就很方便。


优化代码


-初始化尾指针

LinkList initTailList(NodePtr paraHeader){
	NodePtr tempQ;
	tempQ = paraHeader;
	while(tempQ->next != NULL){
		tempQ = tempQ->next; 
	}
	NodePtr tempTail = (NodePtr)malloc(sizeof(LNode));//申请一个节点大小空间,转化为节点指针类型,用作头 
	tempTail->next = tempQ;
	tempTail->data = '\0'; 
	
	return tempTail;
}


-尾指针检查

void initTailListTest(){
	printf("--- initTailListTest Begin ---\n");
	LinkList tempHeader;
	tempHeader = initLinkList();
	appendElement(tempHeader, 'H');
	appendElement(tempHeader, 'E');
	appendElement(tempHeader, 'L');
	appendElement(tempHeader, 'L');
	appendElement(tempHeader, 'O');
	appendElement(tempHeader, '!');
	printList(tempHeader);
	
	LinkList tempTail = (LinkList)malloc(sizeof(LNode));
	tempTail = initTailList(tempHeader);
	
	printf("%c\n", tempTail->next->data);
	
	printf("--- initTailListTest End ---\n");
	
}
void superAppendElement(NodePtr paraTail, char paraChar){
	NodePtr tempP;
	
	//Step 1.initialize the p
	tempP = (NodePtr)malloc(sizeof(LNode));
	tempP->data = paraChar;
	

	//Step 2.insert by the tailPtr
	paraTail->next->next = tempP;
	tempP->next = NULL;
	
	//Step 3.remake the tailPtr 
	paraTail->next = tempP;
	
}


代码测试

void superAppendElementTest(){
	printf("--- superAppendElementTest Begin ---\n");
	LinkList tempHeader, tempTail;
	tempHeader = initLinkList();
	tempTail = initTailList(tempHeader);
	
	superAppendElement(tempTail, 'H'); 
	superAppendElement(tempTail, 'E');
	superAppendElement(tempTail, 'L');
	superAppendElement(tempTail, 'L');
	superAppendElement(tempTail, 'O');
	superAppendElement(tempTail, '!');
	superAppendElement(tempTail, ' ');
	superAppendElement(tempTail, 'W');
	
	printList(tempHeader);
	
	printf("--- superAppendElementTest End ---\n");
	
}


4.按照指定位置插入


图解




代码实现

void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
	NodePtr tempP, tempQ;
	
	//Step 1.Search to the position
	tempP = paraHeader;
	for (int i = 0; i < paraPosition - 1; i++){
		tempP = tempP->next;
		if(tempP == NULL){
			printf("The position %d is beyond the scope of the list.", paraPosition);
		}//of if
	}//of for
	
	//Step 2.Construct a new node.
	tempQ = initLinkList();
	tempQ->data = paraChar;
	
	//Step 3.Now link.
	printf("linking\r\n");
	tempQ->next = tempP->next;
	tempP->next = tempQ;
}//of InsertElenment


测试代码

void insertElementTest(){
	 printf("--- insertElementTest Begin ---\n");
	 
	 //initialize.
	 LinkList tempList;
	 tempList = initLinkList();
     appendElement(tempList, 'H');
	 appendElement(tempList, 'E');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'O');
	 appendElement(tempList, '!');
	 printList(tempList);
	 
	 //insert in firt
	 insertElement(tempList, '0', 1);
	 //insert in 3
	 insertElement(tempList, '0', 3);
	 //insert in last
	 insertElement(tempList, '0', 9);
	 
	 printList(tempList);
	  printf("--- insertElementTest End ---\n");
}


5.按照给定数据插入


图解




代码实现

 void insertElement1(NodePtr paraHeader, char paraChar, char paraGivenChar){
 	NodePtr tempP, tempQ;
	
	//Step 1.Search to the position
	tempP = paraHeader;
	while(tempP->next->data != paraGivenChar){
		tempP = tempP->next;
		if(tempP == NULL){
			printf("The data %c is beyond the scope of the list.", paraGivenChar);
		}//of if
	}//of while
	
	tempQ = (NodePtr)malloc(sizeof(LNode));
	tempQ->data = paraChar;
	
	//Step 2.insert
	tempQ->next = tempP->next;
	tempP->next = tempQ;
	
 }


测试代码

 void insertElementTest1(){
	 printf("--- insertElementTest1 Begin ---\n");
	 
	 //initialize.
	 LinkList tempList;
	 tempList = initLinkList();
     appendElement(tempList, 'H');
	 appendElement(tempList, 'E');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'O');
	 appendElement(tempList, '!');
	 printList(tempList);
	 
	 //insert before H
	 insertElement1(tempList, '0', 'H');
	 //insert before E
	 insertElement1(tempList, '0', 'E');
	 //insert before !
	 insertElement1(tempList, '0', '!');
	 
	 printList(tempList);
	  printf("--- insertElementTest1 End ---\n");
}


6.删除指定数据节点


图解




代码实现

void deleteElement(NodePtr paraHeader, char paraChar){
 	NodePtr tempP, tempQ;
 	tempP = paraHeader;
 	
 	//Step 1.Search to the position
 	while((tempP->next->data != paraChar) && (tempP->next != NULL)){
 		tempP = tempP->next;
	}// of While
	if(tempP->next == NULL){
		printf("there is no the value!");
		return;
	}//of if
	
	//Step 2.Delete
	tempQ = tempP->next;
	tempP->next = tempP->next->next;
	free(tempQ);
	
 } //of Delete


代码测试

void deleteElementTest(){
 	
 	 printf("--- deleteElementTest Begin ---\n");
 	 //Initialize.
 	 LinkList tempList;
	 tempList = initLinkList();
     appendElement(tempList, 'H');
	 appendElement(tempList, 'E');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'O');
	 appendElement(tempList, '!');
	 printList(tempList);
	 
	 //delete the 'H'
	 deleteElement(tempList, 'H');
	 printList(tempList);
	 //delete the 'L'(first)
	 deleteElement(tempList, 'L');
	 printList(tempList);
	 //delete the '!'
 	 deleteElement(tempList, '!');
	 printList(tempList);
 	 printf("--- deleteElementTest End ---\n");
 }


7.求前驱


代码实现

NodePtr getPriorNode(NodePtr paraHeader, char paraGivenChar){
 	NodePtr tempP;
	tempP = paraHeader;
	
	//seek the target data
	while(tempP->next->data != paraGivenChar){
		tempP = tempP->next;
		if(tempP == NULL){
			printf("no prior", paraGivenChar);
			return Emp;
		}//of if
	}//of while
	
	//if it is first , it has no proir
	if(tempP == paraHeader ){
		printf("no prior");
		return Emp;
	}
	
 	return tempP;
 }


测试代码

NodePtr getPriorNodeTest(){
 	printf("--- getPriorNodeTest Begin ---\n");
 	//initialize.
	LinkList tempList;
	tempList = initLinkList();
    appendElement(tempList, 'H');
	appendElement(tempList, 'E');
	appendElement(tempList, 'L');
	appendElement(tempList, 'L');
	appendElement(tempList, 'O');
	appendElement(tempList, '!');
	printList(tempList);
	
	NodePtr tempP;
	//firt node 
	tempP = getPriorNode(tempList, 'H');
	printf("%c\n", tempP->data);
	//second node
	tempP = getPriorNode(tempList, 'E');
	printf("%c\n", tempP->data);
	//last node
	tempP = getPriorNode(tempList, '!');
	printf("%c\n", tempP->data);
 	
 	printf("--- getPriorNodeTest End ---\n");
 }


8.求指定数据类型最大值


代码实现

char getMaxData(NodePtr paraHeader){
	NodePtr tempP = paraHeader;
 	
 	//if empty 
 	if(paraHeader->next == NULL){
 		printf("no data"); 
 		return '\0';
	}
 	char max = tempP -> data;
 	
 	while(tempP->next != NULL){
 		tempP = tempP->next;
		if(tempP->data > max ){
			max = tempP->data; 
		} 
	}	
 	return max;
}	


测试代码

void getMaxDataTest(){
	 printf("--- getMaxDataTest Begin ---\n");
 	 //Initialize.
 	 LinkList tempList;
	 tempList = initLinkList();
     appendElement(tempList, '1');
	 appendElement(tempList, '9');
	 appendElement(tempList, '3');
	 appendElement(tempList, '0');
	 appendElement(tempList, '1');
	 appendElement(tempList, '5');
	 printList(tempList);
	 
	 char max = getMaxData(tempList);
	 printf("%c\n", max);
	 printf("--- getMaxDataTest End ---\n");
}


9.清空单链表


代码实现:

void destroyList(NodePtr paraHeader){
	paraHeader->next = NULL;
}


代码测试

void destroyListTest(){
	printf("--- destroyListTest Begin ---\n");
	//initialize.
	LinkList tempList;
	tempList = initLinkList();
    appendElement(tempList, 'H');
	appendElement(tempList, 'E');
	appendElement(tempList, 'L');
	appendElement(tempList, 'L');
	appendElement(tempList, 'O');
	appendElement(tempList, '!');
	printList(tempList);
	
	//destroy
	destroyList(tempList);
	printList(tempList);
	printf("--- destroyListTest End ---\n");
}


3)地址访问

代码实现:

void basicAddressTest(){
 	printf("--- basicAddressTest begin ---\n");
 	LNode tempList1, tempList2, tempList3;
 	
 	
 	tempList1.data = 4;
 	tempList1.next = NULL;
 	
 	tempList2.data = 6;
 	tempList2.next = NULL;
 	
 	tempList3.data = 8;
 	tempList3.next = NULL;
 	
 	int *p = NULL;
 	typedef struct LinkNode2{
		char data;
	}LNode2;
	
	typedef struct LinkNode3{
		struct LinkNode3 *next;
	}LNode3;
	

	printf("the sizeof of the LNode2: %d\n", sizeof(LNode2));
	printf("the sizeof of the LNode3: %d\n", sizeof(LNode3));
 	printf("the sizeof of the LNode: %d\n", sizeof(LNode));
 	printf("the sizeof of the LinkList: %d\n", sizeof(LinkList));
 	printf("the sizeof of the int *p: %d\n", sizeof(p));
 	
 	printf("The first list: %d %d %d\n", &tempList1, &tempList1.data, &tempList1.next);
 	printf("The second list: %d %d %d\n", &tempList2, &tempList2.data, &tempList2.next);
 	printf("The third list: %d %d %d\n", &tempList3, &tempList3.data, &tempList3.next);
 	
 	printf("--- basicAddressTest end ---\n");
 }


结果:


— basicAddressTest begin —

the sizeof of the LNode2: 1

the sizeof of the LNode3: 8

the sizeof of the LNode: 16

the sizeof of the LinkList: 8

the sizeof of the int *p: 8

The first list:           6487504 6487504 6487512

The second list:     6487488 6487488 6487496

The third list:          6487472 6487472 6487480

— basicAddressTest end —


思考:

一个节点内储存的空间是连续的, 但俩个连续的节点的空间储存上不是连续的,所以指针是单向通道。


4)总结:

上面的定义就是我对链表的理解;

主要是搞清楚头指针是上面,里面的作用就能看懂单链表。


5)总代码:

#include <stdio.h>
#include <malloc.h>

/**
 * Linked list of characters. The key is data.
 */
typedef struct LinkNode{
	char data;
	struct LinkNode *next;
} LNode, *LinkList, *NodePtr;//节点/链表指针/节点指针 

static NodePtr Emp = (NodePtr)malloc(sizeof(LNode));//empty node 

void initEmptyNode(){
    Emp->data = '\0'; 
    Emp->next = NULL;
}
/**
 * Initialize the list with a header.
 * @return The Pointer to the header.
 */

LinkList initLinkList(){
	NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));//申请一个节点大小空间,转化为节点指针类型,用作头 
	tempHeader->data = '\0';
	tempHeader->next = NULL;
	return tempHeader; 
} 

/**
 * Print the list
 * @param paraHead The header of teh List
 */
 
void printList(NodePtr paraHeader){
	NodePtr tempP = paraHeader->next;//skip headPtr,no data in headPtr 
	if(tempP == NULL){
		printf("The LinkList is empty");
		printf("\r\n");
		return;
	}
	while(tempP != NULL){
		printf("%c", tempP->data);
		tempP = tempP->next;
	}
	printf("\r\n");
}

/**
 * Add an element to the tail.
 * @param paraHeader The header of the list.
 * @param paraChar The give cahr.
 */


void appendElement(NodePtr paraHeader, char paraChar){
	NodePtr tempP, tempQ;
	
	//Step 1.Construct a new node.
	tempQ = (NodePtr)malloc(sizeof(LNode));
	tempQ->data = paraChar;
	tempQ->next = NULL;
	//Step 2.Search to the tail.
	tempP = paraHeader;
	while(tempP->next != NULL){
		tempP = tempP->next;
	}//of while
	//Step 3.Now add/link.
	tempP->next = tempQ;
}//of appendElement

void appendElementTest(){
	printf("--- appendElementTest Begin ---\n");
	LinkList tempHeader;
	tempHeader = initLinkList();
	appendElement(tempHeader, 'H');
	appendElement(tempHeader, 'E');
	appendElement(tempHeader, 'L');
	appendElement(tempHeader, 'L');
	appendElement(tempHeader, 'O');
	appendElement(tempHeader, '!');
	printList(tempHeader);
	printf("--- appendElementTest Begin ---\n");
}//of appendElementTest

LinkList initTailList(NodePtr paraHeader){
	NodePtr tempQ;
	tempQ = paraHeader;
	while(tempQ->next != NULL){
		tempQ = tempQ->next; 
	}
	NodePtr tempTail = (NodePtr)malloc(sizeof(LNode));//申请一个节点大小空间,转化为节点指针类型,用作头 
	tempTail->next = tempQ;
	tempTail->data = '\0'; 
	
	return tempTail;
}

void initTailListTest(){
	printf("--- initTailListTest Begin ---\n");
	LinkList tempHeader;
	tempHeader = initLinkList();
	appendElement(tempHeader, 'H');
	appendElement(tempHeader, 'E');
	appendElement(tempHeader, 'L');
	appendElement(tempHeader, 'L');
	appendElement(tempHeader, 'O');
	appendElement(tempHeader, '!');
	printList(tempHeader);
	
	LinkList tempTail = (LinkList)malloc(sizeof(LNode));
	tempTail = initTailList(tempHeader);
	
	printf("%c\n", tempTail->next->data);
	
	printf("--- initTailListTest End ---\n");
	
}

void superAppendElement(NodePtr paraTail, char paraChar){
	NodePtr tempP;
	
	//Step 1.initialize the p
	tempP = (NodePtr)malloc(sizeof(LNode));
	tempP->data = paraChar;
	

	//Step 2.insert by the tailPtr
	paraTail->next->next = tempP;
	tempP->next = NULL;
	
	//Step 3.remake the tailPtr 
	paraTail->next = tempP;
	
}

void superAppendElementTest(){
	printf("--- superAppendElementTest Begin ---\n");
	LinkList tempHeader, tempTail;
	tempHeader = initLinkList();
	tempTail = initTailList(tempHeader);
	
	superAppendElement(tempTail, 'H'); 
	superAppendElement(tempTail, 'E');
	superAppendElement(tempTail, 'L');
	superAppendElement(tempTail, 'L');
	superAppendElement(tempTail, 'O');
	superAppendElement(tempTail, '!');
	superAppendElement(tempTail, ' ');
	superAppendElement(tempTail, 'W');
	
	printList(tempHeader);
	
	printf("--- superAppendElementTest End ---\n");
	
}

/**
 * Insert an element to the given position.
 * @param paraHeader The header of the list.
 * @param paraChar The give cahr.
 * @param paraPostion The given position.
 */

void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
	NodePtr tempP, tempQ;
	
	//Step 1.Search to the position
	tempP = paraHeader;
	for (int i = 0; i < paraPosition - 1; i++){
		tempP = tempP->next;
		if(tempP == NULL){
			printf("The position %d is beyond the scope of the list.", paraPosition);
		}//of if
	}//of for
	
	//Step 2.Construct a new node.
	tempQ = initLinkList();
	tempQ->data = paraChar;
	
	//Step 3.Now link.
	printf("linking\r\n");
	tempQ->next = tempP->next;
	tempP->next = tempQ;
}//of InsertElenment

void insertElementTest(){
	 printf("--- insertElementTest Begin ---\n");
	 
	 //initialize.
	 LinkList tempList;
	 tempList = initLinkList();
     appendElement(tempList, 'H');
	 appendElement(tempList, 'E');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'O');
	 appendElement(tempList, '!');
	 printList(tempList);
	 
	 //insert in firt
	 insertElement(tempList, '0', 1);
	 //insert in 3
	 insertElement(tempList, '0', 3);
	 //insert in last
	 insertElement(tempList, '0', 9);
	 
	 printList(tempList);
	  printf("--- insertElementTest End ---\n");
}


/**
 * Insert an element to the given position.
 * @param paraHeader The header of the list.
 * @param paraChar The give cahr.
 * @param paraPostion The given data.
 */
 void insertElement1(NodePtr paraHeader, char paraChar, char paraGivenChar){
 	NodePtr tempP, tempQ;
	
	//Step 1.Search to the position
	tempP = paraHeader;
	while(tempP->next->data != paraGivenChar){
		tempP = tempP->next;
		if(tempP == NULL){
			printf("The data %c is beyond the scope of the list.", paraGivenChar);
		}//of if
	}//of while
	
	tempQ = (NodePtr)malloc(sizeof(LNode));
	tempQ->data = paraChar;
	
	//Step 2.insert
	tempQ->next = tempP->next;
	tempP->next = tempQ;
	
 }
 
 void insertElementTest1(){
	 printf("--- insertElementTest1 Begin ---\n");
	 
	 //initialize.
	 LinkList tempList;
	 tempList = initLinkList();
     appendElement(tempList, 'H');
	 appendElement(tempList, 'E');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'O');
	 appendElement(tempList, '!');
	 printList(tempList);
	 
	 //insert before H
	 insertElement1(tempList, '0', 'H');
	 //insert before E
	 insertElement1(tempList, '0', 'E');
	 //insert before !
	 insertElement1(tempList, '0', '!');
	 
	 printList(tempList);
	  printf("--- insertElementTest1 End ---\n");
}
 
 
/**
 * Delete an element from the list.
 * @param paraHeader The header of list.
 * @param paraChar The given char.
 */
 
 void deleteElement(NodePtr paraHeader, char paraChar){
 	NodePtr tempP, tempQ;
 	tempP = paraHeader;
 	
 	//Step 1.Search to the position
 	while((tempP->next->data != paraChar) && (tempP->next != NULL)){
 		tempP = tempP->next;
	}// of While
	if(tempP->next == NULL){
		printf("there is no the value!");
		return;
	}//of if
	
	//Step 2.Delete
	tempQ = tempP->next;
	tempP->next = tempP->next->next;
	free(tempQ);
	
 } //of Delete
 
 void deleteElementTest(){
 	
 	 printf("--- deleteElementTest Begin ---\n");
 	 //Initialize.
 	 LinkList tempList;
	 tempList = initLinkList();
     appendElement(tempList, 'H');
	 appendElement(tempList, 'E');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'L');
	 appendElement(tempList, 'O');
	 appendElement(tempList, '!');
	 printList(tempList);
	 
	 //delete the 'H'
	 deleteElement(tempList, 'H');
	 printList(tempList);
	 //delete the 'L'(first)
	 deleteElement(tempList, 'L');
	 printList(tempList);
	 //delete the '!'
 	 deleteElement(tempList, '!');
	 printList(tempList);
 	 printf("--- deleteElementTest End ---\n");
 }
 
 /**
 * get an priorElement from the list.
 * @param paraHeader The header of list.
 * @param paraChar The given char.
 */
 
 
 NodePtr getPriorNode(NodePtr paraHeader, char paraGivenChar){
 	NodePtr tempP;
	tempP = paraHeader;
	
	//seek the target data
	while(tempP->next->data != paraGivenChar){
		tempP = tempP->next;
		if(tempP == NULL){
			printf("no prior", paraGivenChar);
			return Emp;
		}//of if
	}//of while
	
	//if it is first , it has no proir
	if(tempP == paraHeader ){
		printf("no prior");
		return Emp;
	}
	
 	return tempP;
 }
 
 NodePtr getPriorNodeTest(){
 	printf("--- getPriorNodeTest Begin ---\n");
 	//initialize.
	LinkList tempList;
	tempList = initLinkList();
    appendElement(tempList, 'H');
	appendElement(tempList, 'E');
	appendElement(tempList, 'L');
	appendElement(tempList, 'L');
	appendElement(tempList, 'O');
	appendElement(tempList, '!');
	printList(tempList);
	
	NodePtr tempP;
	//firt node 
	tempP = getPriorNode(tempList, 'H');
	printf("%c\n", tempP->data);
	//second node
	tempP = getPriorNode(tempList, 'E');
	printf("%c\n", tempP->data);
	//last node
	tempP = getPriorNode(tempList, '!');
	printf("%c\n", tempP->data);
 	
 	printf("--- getPriorNodeTest End ---\n");
 }
 
 //some interesting thing
 void basicAddressTest(){
 	printf("--- basicAddressTest begin ---\n");
 	LNode tempList1, tempList2, tempList3;
 	
 	
 	tempList1.data = 4;
 	tempList1.next = NULL;
 	
 	tempList2.data = 6;
 	tempList2.next = NULL;
 	
 	tempList3.data = 8;
 	tempList3.next = NULL;
 	
 	int *p = NULL;
 	typedef struct LinkNode2{
		char data;
	}LNode2;
	
	typedef struct LinkNode3{
		struct LinkNode3 *next;
	}LNode3;
	

	printf("the sizeof of the LNode2: %d\n", sizeof(LNode2));
	printf("the sizeof of the LNode3: %d\n", sizeof(LNode3));
 	printf("the sizeof of the LNode: %d\n", sizeof(LNode));
 	printf("the sizeof of the LinkList: %d\n", sizeof(LinkList));
 	printf("the sizeof of the int *p: %d\n", sizeof(p));
 	
 	printf("The first list: %d %d %d\n", &tempList1, &tempList1.data, &tempList1.next);
 	printf("The second list: %d %d %d\n", &tempList2, &tempList2.data, &tempList2.next);
 	printf("The third list: %d %d %d\n", &tempList3, &tempList3.data, &tempList3.next);
 	
 	printf("--- basicAddressTest end ---\n");
 }
 
  /**
 * get the max data from the list.
 * @param paraHeader The header of list.
 * @return max.
 */
 
char getMaxData(NodePtr paraHeader){
	NodePtr tempP = paraHeader;
 	
 	//if empty 
 	if(paraHeader->next == NULL){
 		printf("no data"); 
 		return '\0';
	}
 	char max = tempP -> data;
 	
 	while(tempP->next != NULL){
 		tempP = tempP->next;
		if(tempP->data > max ){
			max = tempP->data; 
		} 
	}	
 	return max;
}	
 
void getMaxDataTest(){
	 printf("--- getMaxDataTest Begin ---\n");
 	 //Initialize.
 	 LinkList tempList;
	 tempList = initLinkList();
     appendElement(tempList, '1');
	 appendElement(tempList, '9');
	 appendElement(tempList, '3');
	 appendElement(tempList, '0');
	 appendElement(tempList, '1');
	 appendElement(tempList, '5');
	 printList(tempList);
	 
	 char max = getMaxData(tempList);
	 printf("%c\n", max);
	 printf("--- getMaxDataTest End ---\n");
}

  /**
 * Destroy list.
 * @param paraHeader The header of list.
 */
 
 
void destroyList(NodePtr paraHeader){
	paraHeader->next = NULL;
}

void destroyListTest(){
	printf("--- destroyListTest Begin ---\n");
	//initialize.
	LinkList tempList;
	tempList = initLinkList();
    appendElement(tempList, 'H');
	appendElement(tempList, 'E');
	appendElement(tempList, 'L');
	appendElement(tempList, 'L');
	appendElement(tempList, 'O');
	appendElement(tempList, '!');
	printList(tempList);
	
	//destroy
	destroyList(tempList);
	printList(tempList);
	printf("--- destroyListTest End ---\n");
}

int main()
{
	initEmptyNode();
	appendElementTest();
	insertElementTest(); 
	deleteElementTest();
	basicAddressTest();
	superAppendElementTest();
	initTailListTest();
	insertElementTest1();
	getPriorNodeTest();
	getMaxDataTest();
	destroyListTest();
	return 0;
}



版权声明:本文为qq_60459642原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。