单链表的应用

//1、使用结构体创建一个结点,结点内容包括一个指向下一个结点的指针和一个char类型的数据
//2、使用一个字符数组创建单链表
//3、从头遍历单链表,设置一个计数器count,count代表当前元素的下标
//    1)、当前元素不是要查找的字符时,指针指向下一个结点,并且计数器count加一
//    2)、如果当前元素是要查找的字符,则返回计数器count
//    3)、如果单链表中没有所查找的字符,则直接返回-1
typedef struct SListNode{
	char _data;             //char类型的数据
	struct SListNode* _next;//指向下一个结点的指针
}Node, *pNode;

pNode BuyNewNode(char c)       //创建一个新结点
{
	pNode NewNode = (pNode)malloc(sizeof(Node));
	assert(NewNode);
	NewNode->_data = c;
	NewNode->_next = NULL;
	return NewNode;
}

pNode CreateList(char array[], int len)//使用一个字符数组创建单链表
{
	int i = 0;
	pNode pHead = NULL;
	pHead = BuyNewNode(array[0]);
	pNode cur = pHead;
	for (i = 1; i < len; i++)
	{
		pHead->_next = BuyNewNode(array[i]);
		pHead = pHead->_next;
	}
	return cur;
}


//下标:已经遍历过的结点个数
int Find(pNode pHead, char k)//从头遍历单链表,设置一个计数器count,count代表当前元素的下标
{
	pNode cur = pHead;
	int count = 0;
	while (cur != NULL)
	{                    
		if (cur->_data == k)    //如果当前结点的字符是要查找的字符,则返回下标count
			return count;    
		cur = cur->_next;
		count++;                //如果当前元素不是要查找的字符,则下标count+1,同时结点往后走
	}
	return -1;                   //如果单链表中没有所查找的字符,则直接返回-1
}

int main()
{
	int tmp = 0;
	char array[10] = { 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 's', 'f' };
	pNode pHead = CreateList(array, sizeof(array) / sizeof(array[0]));

	tmp = Find(pHead, 'c');
	if (tmp >= 0)
	{
		printf("%d\n", tmp);
	}
	else
	{
		printf("字符不存在!");
	}
}

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单链表应用实例代码,使用C语言实现: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 typedef struct Node { int data; struct Node* next; } node; // 创建新节点 node* create_node(int data) { node* new = (node*)malloc(sizeof(node)); new->data = data; new->next = NULL; return new; } // 在链表末尾插入节点 void insert_node(node** head, int data) { node* new = create_node(data); if (*head == NULL) { *head = new; return; } node* current = *head; while (current->next != NULL) { current = current->next; } current->next = new; } // 删除指定值的节点 void delete_node(node** head, int data) { if (*head == NULL) { return; } if ((*head)->data == data) { node* temp = *head; *head = (*head)->next; free(temp); return; } node* current = *head; while (current->next != NULL && current->next->data != data) { current = current->next; } if (current->next != NULL) { node* temp = current->next; current->next = current->next->next; free(temp); } } // 打印链表 void print_list(node* head) { printf("Linked List: "); while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { node* head = NULL; insert_node(&head, 1); insert_node(&head, 2); insert_node(&head, 3); insert_node(&head, 4); insert_node(&head, 5); print_list(head); delete_node(&head, 3); print_list(head); delete_node(&head, 1); print_list(head); return 0; } ``` 该代码实现了一个单链表的基本操作,包括创建节点、在末尾插入节点、删除指定值的节点和打印链表。可以通过在 `main` 函数中调用这些函数来使用这个单链表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值