数据结构学习第二天——单链表

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言——单链表的定义

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
在这里插入data域--存放结点值的数据域
next域--存放结点的直接后继的地址(位置)的指针域(链域)图片描述


提示:以下是本篇文章正文内容,下面案例可供参考

一、单链表的逻辑结构示意图

在这里插入图片描述

二、单链表的实际操作

1.定义单链表

 /**
 * Linked list of characters. The key is data.
 */
typedef struct LinkNode
{
	char data;
	struct LinkNode *next;
}LNode,*LinkList,*NodePtr;

2.创建单链表

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

3.打印单链表

/**
 * 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;
	}
	printf("\r\n");
}

4.插入

/**
 * 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;
	}
	
	// step 3. Now add/link.
	p->next=q;
}

5.指定位置插入

/**
 *Insert an element to the given position.
 *@param paraHeader The header of the list.
 *@param paraChar The given char.
 *@param paraPositon The given position.
 */ 
void insertElement(NodePtr paraHeader,char paraChar,int paraPosition) 
{
	NodePtr p,q;
	
	p=paraHeader;
	int i;
	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;
	  }
    }
    
    q = (NodePtr)malloc(sizeof(LNode));
    q->data = paraChar;
    
    printf("linking\r\n");
    q->next = p->next;
    p->next = q;
 
}

在这里插入图片描述

6.删除指定元素

/**
 *Delet 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;
	}
	
	if(p->next == NULL)
	{
		printf("Cannot delete %c\n",paraChar);
		return;
	}
	
	q = p->next;
	p->next = p->next->next;
	free(q);	     
}

在这里插入图片描述

完整代码

#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;
}
 
/**
 * 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;
	}
	printf("\r\n");
}
 
/**
 * 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;
	}
	
	// step 3. Now add/link.
	p->next=q;
}
 
/**
 *Insert an element to the given position.
 *@param paraHeader The header of the list.
 *@param paraChar The given char.
 *@param paraPositon The given position.
 */ 
void insertElement(NodePtr paraHeader,char paraChar,int paraPosition) 
{
	NodePtr p,q;
	
	p=paraHeader;
	int i;
	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;
	  }
    }
    
    q = (NodePtr)malloc(sizeof(LNode));
    q->data = paraChar;
    
    printf("linking\r\n");
    q->next = p->next;
    p->next = q;
 
}
 
 
 /**
 *Delet 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;
	}
	
	if(p->next == NULL)
	{
		printf("Cannot delete %c\n",paraChar);
		return;
	}
	
	q = p->next;
	p->next = p->next->next;
	free(q);	     
}
 
 /**
 * Unit test.
 */
void appendInsertDeleteTest()
{
	LinkList tempList = initLinkList();
	printList(tempList);
	
	appendElement(tempList,'H');
	appendElement(tempList,'e');
	appendElement(tempList,'l');
	appendElement(tempList,'l');
	appendElement(tempList,'o');
	appendElement(tempList,'!');
    printList(tempList);
    

    printf("删去刚才的输入'!'\n");
    deleteElement(tempList,'!');
    printList(tempList);
    
    printf("删去刚才输入的'e','o'\n");
    deleteElement(tempList,'e');
    deleteElement(tempList,'o');
    printList(tempList);
    
    printf("在'H'后添加'a'\n");
    insertElement(tempList,'a',1);
     printList(tempList);
} 
 
int main()
{
	appendInsertDeleteTest();
}
 
 
 
 
 
 
 
 

测试结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值