链表操作

/*************************************************************/
#include<stdio.h>
#include<stdlib.h>
/*************************************************************/
struct Node{
	int data;
	struct Node *next;
};
struct List{
	int length;
	struct Node *head;
};
/**************************************************************/
struct List *List_Construct(void);
char List_Empty(struct List *pList);
char List_Insert(struct List *pList,int a);
char List_Traverse(struct List *pList);
struct Node* List_Search(struct List *pList ,int a);
char List_DropElement(struct List *pList,int ele);
char List_Sort(struct List *pList);
/**************************************************************/
int main()
{
	struct List *pList=List_Construct();
	if(List_Empty(pList))
		printf("The List is empty\n");

	List_Insert(pList,1);
	List_Insert(pList,3);
	List_Insert(pList,6);
	List_Insert(pList,5);
	List_Insert(pList,7);
	List_Traverse(pList);

	List_Sort(pList);
	List_Traverse(pList);

	List_DropElement(pList,7);
	List_Traverse(pList);


	printf("The length of the List is%d\n",pList->length);

	return 1;
}
/**************************************************************/
struct List *List_Construct(void)
{	
	//建表
	struct List *pList=(struct List*)malloc(sizeof(struct List));
	//头节点
	pList->head=(struct Node*)malloc(sizeof(struct Node));
	pList->head->next=NULL;
	pList->length=0;
	return pList;
}
char List_Empty(struct List *pList)
{
	//如果空,返回1
	if(pList->head->next==NULL)
		return 1;
	return 0;
}
char List_Insert(struct List *pList,int a)
{
	struct Node *NewNode=(struct Node*)malloc(sizeof(struct Node));
	//插在表头位置
	NewNode->next=pList->head->next;
	NewNode->data=a;
	pList->head->next=NewNode;
	pList->length++;
	return 1;
}

char List_Traverse(struct List *pList)
{
	if(List_Empty(pList))
	{
		printf("Sorry,but the Listlist is empty.");
		return 0;
	}
	else
	{
		struct Node *pNode=pList->head->next;
		while(pNode!=NULL){
			printf("%6d",pNode->data);
			pNode=pNode->next;
		}
		printf("\n");
		return 1;
	}
}

struct Node* List_Search(struct List *pList,int a)
{
	struct Node *frontnode=pList->head;					
	struct Node *pNode=pList->head->next;
	while(pNode!=NULL)
	{
	if(pNode->data==a){//为便于删除操作,返回查找元素的前一个节点
		return frontnode;
	}
	frontnode=pNode;
	pNode=pNode->next;
	}
	return NULL;
}
char List_DropElement(struct List *pList,int ele)
{
	struct Node *temp=NULL;
	struct Node *pNode=List_Search(pList,ele);
	if(List_Empty(pList)){
		printf("Sorry,but the List is empty.");
		return 0;
	}
	
	if(pNode==NULL){
	printf("The element %d is not found",ele);
	return 0;
	}
	else{
		temp=pNode->next;
		pNode->next=temp->next;
		free(temp);
		pList->length--;
		return 1;
	}
}
char List_Sort(struct List *pList)
{
	struct Node *pNode=pList->head->next;
	struct Node *qNode=pNode;
	struct Node *little_node=pNode;
	int temp;

	while(pNode!=NULL)
	{
		while(qNode!=NULL){
			if(qNode->data<little_node->data)
			{
			little_node=qNode;
			}
			qNode=qNode->next;
		}
		temp=little_node->data;
		little_node->data=pNode->data;
		pNode->data=temp;

		pNode=pNode->next;
		qNode=pNode;
		little_node=pNode;
	}
	return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的链表操作包括创建链表、合并链表、删除链表中的元素以及翻转链表中的一段位置的元素。首先,我们可以通过创建一个链表节点类和链表操作类来实现这些操作链表节点类定义了节点的值和指向下一个节点的指针。链表操作类包含了创建链表、浏览链表等方法。 创建链表操作通过传入一个列表来创建链表。每个列表元素都会被转换为一个节点,并通过指针连接起来。浏览链表操作可以将链表中的元素遍历并返回一个列表。 除此之外,还可以进行一些其他的链表操作,如统计链表中某个元素出现的次数。可以通过调用列表的count()方法来实现。 还可以删除链表中倒数第n个元素并返回头结点。这个操作可以通过一次扫描实现。首先,将链表中的每个元素指向一个列表中的每个元素。然后,根据n的值来删除链表中的对应节点。 总之,Python提供了丰富的链表操作方法,可以根据需求进行选择和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [链表操作基础(python)](https://blog.csdn.net/beautiful77moon/article/details/120443389)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [python链表所有操作详解](https://blog.csdn.net/starmoth/article/details/88323536)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值