数据结构——单链表

先上老师の代码

#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

1.单链表结点定义

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode;

2.单链表的创建

①头插法创建单链表

LNode *HeadCreateList(int len)
{
	LNode *L = (LNode*)malloc(sizeof(LNode));
	LNode *temp = L;
	temp->next = NULL;
	
	for(int i=1;i<=len;i++)
	{
		LNode *p = (LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		p->next = temp->next;
		temp->next = p;	
	}
	
	return (LNode*)L;
}

②尾插法创建单链表

LNode *TailCreateList(int len)
{
	LNode *L = (LNode*)malloc(sizeof(LNode));
	LNode *temp = L;
	temp->next = NULL;
	for(int i=1;i<=len;i++)
	{
		LNode *p = (LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		temp->next = p;
		temp = p;
	}
	temp->next = NULL;	
	return (LNode*)L;
}

 

 

2、链表中查找某结点

int Serch(LNode *L, int elem)
{
	LNode *temp = L;
	int pos = 0;
	int i = 1;
	
	while(temp->next)
	{
		temp = temp->next;
		if(elem==temp->data)
		{
			pos = i;
			printf("The %d position in the list is %d\n",elem,pos);
			return pos;
		i++;
	}
	printf("Serch error!\n");
	
	return ERROR;
}

 

 

3、修改某结点的数据域

LNode *Replace(LNode *L, int pos, int elem)
{
	LNode *temp = L;
	temp = temp->next;
	for(int i=1;i<pos;i++)
	{
		temp = temp->next;
	}
	temp->data = elem;
	
	return (LNode*)L;
}

 

 

4、往链表中插入结点

LNode *Insert(LNode *L, int pos, int elem)
{
	LNode *temp = L;
	int i = 0;
	while( (temp!=NULL)&&(i<pos-1) )
	{
		temp = temp->next;
		++i;
	}
	if( (temp==NULL)||(i>pos-1) )		
	{
		printf("%s:Insert false!\n",__FUNCTION__);
		return (LNode*)temp;
	}
	LNode *new = (LNode*)malloc(sizeof(LNode));
	new->data = elem;
	new->next = temp->next;
	temp->next = new;
	
	return (LNode*)L;
}

 

 

5、删除链表结点

LNode *Delete(LNode *L, int pos, int *elem)
{
	LNode *temp = L;
	int i = 0;
    while( (temp!=NULL)&&(i<pos-1) )
	{
		temp = temp->next;
		++i;
	}
	if( (temp==NULL)||(i>pos-1) )
	{
		printf("%s:Delete false!\n",__FUNCTION__);
		return (LNode*)temp;
	}
	LNode *del = temp->next;
	*elem = del->data;
	temp->next = del->next;
	free(del);
	del = NULL;
	
	return (LNode*)L;
}

 

 

以下是完整的代码:

#include <stdio.h>
#include <stdlib.h>
 
#define ERROR 1
#define OK 	  0
typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode;
 
//操作函数的声明
LNode *HeadCreateList(int len);							//头插法
LNode *TailCreateList(int len);							//尾插法
int Serch(LNode *L, int elem);							//在表中寻找元素的位置
LNode *Replace(LNode *L, int pos, int elem); 	   		//替换第pos个位置的元素成elem
LNode *Insert(LNode *L, int pos, int elem);				//在表中插入新结点
LNode *Delete(LNode *L, int pos, int *elem);			//删除表中的结点
void PrintfList(LNode *L);								//打印链表
int MenuSelect(void);									//菜单

void Test1(LNode *L);	//测试"Serch"函数
void Test2(LNode *L);	//测试"Replace"函数
void Test3(LNode *L);	//测试"Insert"函数
void Test4(LNode *L);	//测试"Delete"函数
LNode *Test5(void);		//测试"TailCreateList"函数
LNode *Test6(void);		//测试"HeadCreateList"函数
int main(void)
{
	int len = 0;
	int cmd;
	LNode *L;
	printf("Please input list length:");
	scanf("%d",&len);
	L = TailCreateList(len);
	PrintfList(L);
	while(1)
	{
		cmd = MenuSelect();
		switch(cmd)
		{
			case 1: Test1(L);		break; //测试"Serch"函数
			case 2: Test2(L);		break; //测试"Replace"函数
			case 3: Test3(L);		break; //测试"Insert"函数
			case 4: Test4(L);		break; //测试"Delete"函数
			case 5: L=Test5();		break; //测试"TailCreateList"函数
			case 6: L=Test6();		break; //测试"HeadCreateList"函数
			case 7: system("cls");	break; //清屏
			case 8: exit(0);		break; //退出
		}
	}
	return 0;
}
LNode *HeadCreateList(int len)
{
	LNode *L = (LNode*)malloc(sizeof(LNode));
	LNode *temp = L;
	temp->next = NULL;
	
	for(int i=1;i<=len;i++)
	{
		LNode *p = (LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		p->next = temp->next;
		temp->next = p;
	}
	
	return (LNode*)L;
}
 
LNode *TailCreateList(int len)
{
	LNode *L = (LNode*)malloc(sizeof(LNode));
	LNode *temp = L;
	temp->next = NULL;
	for(int i=1;i<=len;i++)
	{
		LNode *p = (LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		temp->next = p;
		temp = p;
	}
	temp->next = NULL;
	
	return (LNode*)L;
}
int Serch(LNode *L, int elem)
{
	LNode *temp = L;
	int pos = 0;
	int i = 1;
	
	while(temp->next)
	{
		temp = temp->next;
		if(elem==temp->data)
		{
			pos = i;
			printf("The %d position in the list is %d\n",elem,pos);
			return pos;
		}
		i++;
	}
	printf("Serch error!\n");
	return ERROR;
}
LNode *Replace(LNode *L, int pos, int elem)
{
	LNode *temp = L;
	temp = temp->next;
	for(int i=1;i<pos;i++)
	{
		temp = temp->next;
	}
	temp->data = elem;	
	return (LNode*)L;
}
LNode *Insert(LNode *L, int pos, int elem)
{
	LNode *temp = L;
	int i = 0;
	while( (temp!=NULL)&&(i<pos-1) )
	{
		temp = temp->next;
		++i;
	}
	if( (temp==NULL)||(i>pos-1) )		
	{
		printf("%s:Insert false!\n",__FUNCTION__);
		return (LNode*)temp;
	}
	LNode *new = (LNode*)malloc(sizeof(LNode));
	new->data = elem;
	new->next = temp->next;
	temp->next = new;
	
	return (LNode*)L;
}
LNode *Delete(LNode *L, int pos, int *elem)
{
	LNode *temp = L;
	int i = 0;
	while( (temp!=NULL)&&(i<pos-1) )
	{
		temp = temp->next;
		++i;
	}
	if( (temp==NULL)||(i>pos-1) )
	{
		printf("%s:Delete false!\n",__FUNCTION__);
		return (LNode*)temp;
	}
	LNode *del = temp->next;
	*elem = del->data;
	temp->next = del->next;
	free(del);
	del = NULL;
	
	return (LNode*)L;
}
void PrintfList(LNode *L)
{
	LNode *temp = L;
	int count = 0;
	printf("List:\n");
	while(temp->next)
	{
		temp = temp->next;
		printf("%d\t",temp->data);
		count++;
		if(count%5==0)
		{
			printf("\n");
		}
	}
	printf("\n");
}
int MenuSelect(void)
{
	int cmd;
	
	printf("1.Serch test\n");
	printf("2.Replace test\n");
	printf("3.Insert test\n");
	printf("4.Delete test\n");
	printf("5.TailCreateList test\n");
	printf("6.HeadCreateList test\n");
	printf("7.Clear\n");
	printf("8.Exit\n");
	do
	{
		printf("Enter your choice: ");
		scanf("%d",&cmd);
	}while(cmd<0||cmd>8);
	
	return cmd;
}
void Test1(LNode *L)	
{
	int serchElem = 0;
	
	printf("--------------------%s start!--------------------\n",__FUNCTION__);
	PrintfList(L);
	printf("Please input the element you want to serch:");
	scanf("%d",&serchElem);
	Serch(L,serchElem);
	printf("--------------------%s end!--------------------\n",__FUNCTION__);
	printf("\n");
}
void Test2(LNode *L)	
{
	int replacePos = 0, replaceElem = 0;
	
	printf("--------------------%s start!--------------------\n",__FUNCTION__);
	PrintfList(L);
	printf("Please input the position and the element you want replace(example:10,33):");
	scanf("%d,%d",&replacePos,&replaceElem);
	L = Replace(L,replacePos,replaceElem);
	printf("After replace by position,list is:\n");
	PrintfList(L);
	printf("--------------------%s end!--------------------\n",__FUNCTION__);
	printf("\n");
}
void Test3(LNode *L)	
{
	int insertPos = 0, insertElem = 0;
	
	printf("--------------------%s start!--------------------\n",__FUNCTION__);
	PrintfList(L);
	printf("Please input the position and the element you want insert(example:10,33):");
	scanf("%d,%d",&insertPos,&insertElem);
	L = Insert(L,insertPos,insertElem);
	printf("After insert,list is:\n");
	PrintfList(L);
	printf("--------------------%s end!--------------------\n",__FUNCTION__);
	printf("\n");
}
void Test4(LNode *L)	
{
	int deletePos = 0;
	int elem = NULL;
	
	printf("--------------------%s start!--------------------\n",__FUNCTION__);
	PrintfList(L);
	printf("Please input the position of the element you want to delete(example:10):");
	scanf("%d",&deletePos);
	L = Delete(L,deletePos,&elem);
	printf("Delete node data is:%d\n",elem);
	printf("After delete,list is:\n");
	PrintfList(L);
	printf("--------------------%s end!--------------------\n",__FUNCTION__);
	printf("\n");
}
LNode *Test5(void)	
{
	LNode *L;
	int len = 0;
	
	printf("--------------------%s start!--------------------\n",__FUNCTION__);
	printf("Please input list length:");
	scanf("%d",&len);
	L = TailCreateList(len);
	PrintfList(L);
	printf("--------------------%s end!--------------------\n",__FUNCTION__);
	printf("\n");
	
	return (LNode*)L;
}
LNode *Test6(void)	
{
	LNode *L;
	int len = 0;
	
	printf("--------------------%s start!--------------------\n",__FUNCTION__);
	printf("Please input list length:");
	scanf("%d",&len);
	L = HeadCreateList(len);
	PrintfList(L);
	printf("--------------------%s end!--------------------\n",__FUNCTION__);
	printf("\n");
	
	return (LNode*)L

本人比较菜,仅靠自己肯定是写不完的,参考了其他文章的一些功能代码(不会写就多抄,抄着抄着就会了),但有些功能仍然有bug需要修复。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值