链表的C语言实现

 

 

#include<stdio.h>

typedef struct Node{
	int id;
	int value;
	struct Node* next;
}Node;


Node* creatSingleList(int singleListLength);
Node* getSingleListTail(Node* singleListHead);
void printSingleList(Node* singleListHead);
void printNodeIDandValue(Node* singleListPointer);
int getSingleListLength(Node* singleListHead);
int insertNodeAfterId(Node* singleListHead, int id);
int insertNodeAfterValue(Node* singleListHead, int value);
int searchIdFromList(Node* singleListHead, int id);
int searchValueFromList(Node* singleListHead, int value);
int updateSingleListLength(Node* singleListHead, int* singleListLengthPointer);

int main(int argc, char** argv){

	Node* singleListHead;
	Node* singleListTail;
	int singleListLength;
	int i;

	singleListLength = 20;

	singleListHead = creatSingleList(singleListLength);
	
	printSingleList(singleListHead);

	singleListTail = getSingleListTail(singleListHead);

	printNodeIDandValue(singleListTail);

	singleListLength = getSingleListLength(singleListHead);
	printf("List length is : %d \n", singleListLength);
	updateSingleListLength(singleListHead, &singleListLength);
	printf("List length is : %d \n", singleListLength);

	i = searchIdFromList(singleListHead, 6);
	if(i != -1){
			printf("id position  is : %d \n", i);
	}

	i = searchValueFromList(singleListHead, 6);
	if(i != -1){
			printf("value position  is : %d \n", i);
	}

	return 0;
}

void printSingleList(Node* singleListHead){
	Node* singleListPointer;
	singleListPointer = singleListHead;
	while(singleListPointer != NULL){
		printf("Node ID: %d \tvalue: %d ",singleListPointer->id,singleListPointer->value);
		printf("\n");
		singleListPointer = singleListPointer->next;
	}
}

Node* creatSingleList(int singleListLength){
	Node* singleListHead;
	Node* singleListPointer;
	int i;

	singleListHead = (Node*)malloc(sizeof(Node));
	singleListPointer = singleListHead;
	srand(time(NULL));
	for(i=0;i<singleListLength-1;i++){
		singleListPointer->id = rand()%singleListLength;
		singleListPointer->value = 2*i;
		singleListPointer->next = (Node*)malloc(sizeof(Node));
		singleListPointer = singleListPointer->next;
	}
	singleListPointer->id = rand()%singleListLength;
	singleListPointer->value = 2*i;
	singleListPointer->next = NULL;

	return singleListHead;
}

Node* getSingleListTail(Node* singleListHead){

	Node* singleListTail;
	Node* singleListPointer;

	singleListPointer = singleListHead;
	while(singleListPointer->next != NULL){
		singleListPointer = singleListPointer->next;
	}
	singleListTail = singleListPointer;

	return singleListTail;
}

void printNodeIDandValue(Node* singleListPointer){

	printf("\n");
	printf("Node ID: %d\tvalue: %d",singleListPointer->id,singleListPointer->value);
	printf("\n");

}

int getSingleListLength(Node* singleListHead){

	int i;
	Node* singleListPointer;

	singleListPointer = singleListHead;
	i = 1;
	while(singleListPointer->next != NULL){
		i++;
		singleListPointer = singleListPointer->next;
	}		
	return i;
}

int insertNodeAfterId(Node* singleListHead, int id){


	return 0;
}

int insertNodeAfterValue(Node* singleListHead, int value){


	return 0;
}

int updateSingleListLength(Node* singleListHead, int* singleListLengthPointer){

	int length;
	length = getSingleListLength(singleListHead);
	*singleListLengthPointer = length;
	return 0;
}

int searchIdFromList(Node* singleListHead, int id){

	Node* singleListPointer;
	int position;

	position = 0;
	singleListPointer = singleListHead;
	while(singleListPointer->id != id){
		singleListPointer = singleListPointer->next;
		position++;
		if(position >= getSingleListLength(singleListHead)){
			fprintf(stderr,"Error! can not find id in the list!\n");
			position = -1;
			break;
		}
	}
	return position;	
}

int searchValueFromList(Node* singleListHead, int value){

	Node* singleListPointer;
	int position;

	position = 0;
	singleListPointer = singleListHead;
	while(singleListPointer->value != value){
		singleListPointer = singleListPointer->next;
		position++;
		if(position >= getSingleListLength(singleListHead)){
			fprintf(stderr,"Error! can not find id in the list!\n");
			position = -1;
			break;
		}
	}
	return position;	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值