数据结构2.2单链表

  • Post author:
  • Post category:其他


单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。接下来将对单链表相关数据处理进行代码展示

创建一个单链表

typedef struct Linknode
{
	char data;
	struct Linknode* next;
}LNode, * LinkList, * NodePtr;
/*
* Initial the list with a header.
* @return The pointerr to the header.
*/

单链表的初始化

LinkList initLinkList()
{
	NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));
	tempHeader->data = '\0';
	tempHeader->next = NULL;
	return tempHeader;
}//of initLinkList
/*

打印单链表

void printList(NodePtr paraHeader)
{
	NodePtr p = paraHeader->next;
	while (p != NULL)
	{
		printf("%c", p->data);
		p = p->next;
	}//of while
	printf("\r\n");
}//of printList

单链表增添数据

void appendElement(NodePtr paraHeader, char paraChar)
{
	NodePtr p, q;
	p = paraHeader;
	//step 1.Construct a new node
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;
	q->next = NULL;
	//step 2. search to the tail.
	while (p->next != NULL)
	{
		p = p->next;
	}//of while
	//step 3.Now add
	p->next = q;
}//of appendelement

插入元素

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

删除元素

void deleteElement(NodePtr paraHeader, char paraChar)
{
	NodePtr p, q;
	p = paraHeader;
	while ((p->next != NULL) && (p->next->data != paraChar))
	{
		p = p->next;
	}//of while
	if (p->next == NULL)
	{
		printf("Cannot delete %c\r\n", paraChar);
		return;
	}//of it
	q = p->next;
	p->next = p->next->next;
	free(q);
}//of deleteElement
/*
Unite test
*/

单链表基础操作

void appendInsertDeleteTest()
{
	//step 1.Initialize an empty list.
	LinkList tempList = initLinkList();
	printList(tempList);
	//step 2. Add some characters.
	appendElement(tempList, 'H');
	appendElement(tempList, 'e');
	appendElement(tempList, 'l');
	appendElement(tempList, 'l');
	appendElement(tempList, 'o');
	printList(tempList);
	//step 3. Delete some characters(the first occurrencr
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);
	//step 4.Insert to a given position.
	insertElement(tempList, '0', 1);
	printList(tempList);
}//of appendInsertDeleteTest
/*

测试

void basicAddressTest()
{
	LNode tempNode1, tempNode2;
	tempNode1.data = 4;
	tempNode1.next = NULL;
	tempNode2.data = 6;
	tempNode2.next = NULL;
	printf("The first node:%d,%d,%d\r\n", &tempNode1, &tempNode1.data, &tempNode1.next);
	printf("The second node:%d,%d,%d\r\n", &tempNode2, &tempNode2.data, &tempNode2.next);
	tempNode1.next = &tempNode2;
}//of basicAddressTest

整体代码展示

#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;
/*
* Initial the list with a header.
* @return The pointerr to the header.
*/
LinkList initLinkList()
{
	NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));
	tempHeader->data = '\0';
	tempHeader->next = NULL;
	return tempHeader;
}//of initLinkList
/*
*print the list
* @param paraHeader The header of the list
*/
void printList(NodePtr paraHeader)
{
	NodePtr p = paraHeader->next;
	while (p != NULL)
	{
		printf("%c", p->data);
		p = p->next;
	}//of while
	printf("\r\n");
}//of printList
/*
Add an element to the tail
@param paraHeader The header of the list.
@param paraChar The given char.
*/
void appendElement(NodePtr paraHeader, char paraChar)
{
	NodePtr p, q;
	p = paraHeader;
	//step 1.Construct a new node
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;
	q->next = NULL;
	//step 2. search to the tail.
	while (p->next != NULL)
	{
		p = p->next;
	}//of while
	//step 3.Now add
	p->next = q;
}//of appendelement
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition)
{
	int i;
	NodePtr p, q;
	//Step 1. Search to the position.
	p = paraHeader;
	q = paraHeader;
	for (i = 0; i < paraPosition; i++)
	{
		p = p->next;
		if (p == NULL)
		{
			printf("The position %d is beyond the scope of the list.", paraPosition);
			return;
		}//of it
	}//of for i
	//step 2.Construct a new node.
	q = (NodePtr)malloc(sizeof(NodePtr));
	q->data = paraChar;
	//step 3.Now link
	printf("linking\r\n");
	q->next = p->next;
	p->next = q;
}//of insertElement
void deleteElement(NodePtr paraHeader, char paraChar)
{
	NodePtr p, q;
	p = paraHeader;
	while ((p->next != NULL) && (p->next->data != paraChar))
	{
		p = p->next;
	}//of while
	if (p->next == NULL)
	{
		printf("Cannot delete %c\r\n", paraChar);
		return;
	}//of it
	q = p->next;
	p->next = p->next->next;
	free(q);
}//of deleteElement
/*
Unite test
*/
void appendInsertDeleteTest()
{
	//step 1.Initialize an empty list.
	LinkList tempList = initLinkList();
	printList(tempList);
	//step 2. Add some characters.
	appendElement(tempList, 'H');
	appendElement(tempList, 'e');
	appendElement(tempList, 'l');
	appendElement(tempList, 'l');
	appendElement(tempList, 'o');
	printList(tempList);
	//step 3. Delete some characters(the first occurrencr
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);
	//step 4.Insert to a given position.
	insertElement(tempList, '0', 1);
	printList(tempList);
}//of appendInsertDeleteTest
/*
	Address test:beyond the book
*/
void basicAddressTest()
{
	LNode tempNode1, tempNode2;
	tempNode1.data = 4;
	tempNode1.next = NULL;
	tempNode2.data = 6;
	tempNode2.next = NULL;
	printf("The first node:%d,%d,%d\r\n", &tempNode1, &tempNode1.data, &tempNode1.next);
	printf("The second node:%d,%d,%d\r\n", &tempNode2, &tempNode2.data, &tempNode2.next);
	tempNode1.next = &tempNode2;
}//of basicAddressTest
/*
	The entrance.
*/
int main()
{
	appendInsertDeleteTest();
}//of main

老师的代码

#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;

/**
 * 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;
}// Of initLinkList

/**
 * Print the list.
 * @param paraHeader The header of the list.
 */
void printList(NodePtr paraHeader){
	NodePtr p = paraHeader->next;
	while (p != NULL) {
		printf("%c", p->data);
		p = p->next;
	}// Of while
	printf("\r\n");
}// Of printList

/**
 * Add an element to the tail.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 */
void appendElement(NodePtr paraHeader, char paraChar){
	NodePtr p, q;

	// Step 1. Construct a new node.
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;
	q->next = NULL;

	// Step 2. Search to the tail.
	p = paraHeader;
	while (p->next != NULL) {
		p = p->next;
	}// Of while

	// Step 3. Now add/link.
	p->next = q;
}// Of appendElement

/**
 * Insert an element to the given position.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 * @param paraPosition The given position.
 */
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
	NodePtr p, q;

	// Step 1. Search to the position.
	p = paraHeader;
	for (int i = 0; i < paraPosition; i ++) {
		p = p->next;
		if (p == NULL) {
			printf("The position %d is beyond the scope of the list.", paraPosition);
			return;
		}// Of if
	} // Of for i

	// Step 2. Construct a new node.
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;

	// Step 3. Now link.
	printf("linking\r\n");
	q->next = p->next;
	p->next = q;
}// Of insertElement

/**
 * Delete an element from the list.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 */
void deleteElement(NodePtr paraHeader, char paraChar){
	NodePtr p, q;
	p = paraHeader;
	while ((p->next != NULL) && (p->next->data != paraChar)){
		p = p->next;
	}// Of while

	if (p->next == NULL) {
		printf("Cannot delete %c\r\n", paraChar);
		return;
	}// Of if

	q = p->next;
	p->next = p->next->next;
	free(q);
}// Of deleteElement

/**
 * Unit test.
 */
void appendInsertDeleteTest(){
	// Step 1. Initialize an empty list.
	LinkList tempList = initLinkList();
	printList(tempList);

	// Step 2. Add some characters.
	appendElement(tempList, 'H');
	appendElement(tempList, 'e');
	appendElement(tempList, 'l');
	appendElement(tempList, 'l');
	appendElement(tempList, 'o');
	appendElement(tempList, '!');
	printList(tempList);

	// Step 3. Delete some characters (the first occurrence).
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	// Step 4. Insert to a given position.
	insertElement(tempList, 'o', 1);
	printList(tempList);
}// Of appendInsertDeleteTest

/**
 * Address test: beyond the book.
 */
void basicAddressTest(){
	LNode tempNode1, tempNode2;

	tempNode1.data = 4;
	tempNode1.next = NULL;

	tempNode2.data = 6;
	tempNode2.next = NULL;

	printf("The first node: %d, %d, %d\r\n",
		&tempNode1, &tempNode1.data, &tempNode1.next);
	printf("The second node: %d, %d, %d\r\n",
		&tempNode2, &tempNode2.data, &tempNode2.next);

	tempNode1.next = &tempNode2;
}// Of basicAddressTest

/**
 * The entrance.
 */
int main(){
	appendInsertDeleteTest();
}// Of main



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