数据结构3,链表

1.本次课从老师对大家作业的评讲中,我认识到代码可观性的提升,需要注意代码的规范性,比如该空格的地方一定空格且注意数量,代码之间的空行不要太多。风格一致性,不要前后风格不一。

2.链表特点:用一组任意的存储结构单元存储数据元素,这组数据单元可以连续也可以不连续,除了存储本身信息(数据域)之外,还存储一个指向直接后继的位置(指针域)

3.首元结点:链表中第一个存储数据的结点。头结点:首元结点之前的一个结点,指针域指向首元结点。头指针:指向链表中第一个结点

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;

//函数申明
LINKLIST init_LinkList();
void print_List(NODEPTR paraHeader);
void append_Element(NODEPTR paraHeader, char paraChar);
void insert_Element(NODEPTR paraHeader, char paraChar, int paraPosition);
void delete_Element(NODEPTR paraHeader, char paraChar)
void append_insert_delete_Test();
void basicAddress_Test();

/**
 *Initialize the list with a header.
 @return the pointer to tne header,
 */
LINKLIST init_LinkList()
{
	NODEPTR tempHeader=(NODEPTR)malloc(sizeof(LNODE));
	tempHeader->data='\0';
	tempHeader->next=NULL;
	return tempHeader;
} //of init_LinkList

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

/**
 *Add an element to the tail.
 *@param paraHeader the header of the list.
 *@param paraChar the given char.
 */
void append_Element(NODEPTR paraHeader, char paraChar)
{
	NODEPTR p, q;
	
	//step1.Construct a new node.
	q=(NODEPTR)malloc(sizeof(LNODE));
	q->data=paraChar;
	q->next=NULL;
	
	//step2.Search to the tail.
	p=paraHeader;
	while(p->next!=NULL)
	{
		p=p->next;
	}//of while
	
	//step3.Now add/link.
	p->next=q;
}//of append_Element

/**
 *Insert an element to the given positon.
 *@param paraHeader the header of the list.
 *@param paraChar the given char.
 *@param paraposition the given position.
 */
void insert_Element(NODEPTR paraHeader, char paraChar, int paraPosition)
{
	NODEPTR p, q;
	
	//step1.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
	
	//step2.Construct a new node.
	q=(NODEPTR)malloc(sizeof(LNODE));
	q->data=paraChar;
	
	//step3.Now link.
	printf("linking.\n");
	q->next=p->next;
	p->next=q;
}//of insret_Element

/**
 *Delete an element from the list.
 *@param paraHeader the header of the list.
 *@param paraChar the given char.
 */
void delete_Element(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("Can't delete %c\n", paraChar);
		return;
	}//of if
	
	q=p->next;
	p->next=p->next->next;
	free(q);
}//of delete_Element

/**
 *Unit test.
 */
void append_insert_delete_Test()
{
	//step1.Initialize an empty list.
	LINKLIST tempList=init_LinkList();
	print_List(tempList);
	
	//step2.Add some characters.
	append_Element(tempList, 'H');
	append_Element(tempList, 'e');
	append_Element(tempList, 'l');
	append_Element(tempList, 'l');
	append_Element(tempList, 'o');
	append_Element(tempList, '!');
	print_List(tempList);
	
	//step3.Delete some characters (the first occurrence).
	delete_Element(tempList, 'e');
	delete_Element(tempList, 'a');
	delete_Element(tempList, 'o');
	print_List(tempList);
	
	//step4.Insert to a given position.
	insert_Element(tempList, 'o', 1);
	print_List(tempList);
}//of append_insert_delete_Test

/**
 *Address test: beyond the book.
 */
void basicAddress_Test()
{
	LNODE tempNode1, tempNode2;
	
	tempNode1.data=4;
	tempNode2.data=6;
	
	printf("The first node: %d, %d, %d\n", &tempNode1, &tempNode1.data, &tempNode1.next);
	printf("The second node: %d, %d, %d\n", &tempNode2, &tempNode2.data, &tempNode2.next);
	
	tempNode1.next=&tempNode2;
}//of basicAddress_Test

/**
 *The entrance.
 */
int main()
{
	append_insert_delete_Test();
}//of main

​

​

​

6.运行结果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值