单链表

单链表,第1张

.h代码
#ifndef _LINKLIST_H  
#define _LINKLIST_H

#include 
#include 
#include 

#define STRINGSZIE 20

typedef struct ElemType{
	int int_val;
	char char_val;
	char string_val[STRINGSZIE]; 
}ElemType;

typedef struct LinkList{
	ElemType data;
	struct LinkList *next;
}LinkList,*PLinkList; 


PLinkList InitList();
void addElem(PLinkList list, int index, ElemType *data);
void deleteElem(PLinkList list, int index);
void changeElem(PLinkList list, int index, ElemType *data);
int examineElem(PLinkList list, ElemType *data);
void printElem(PLinkList list);
int leng(PLinkList list);

#endif 
.c代码
#include "LinkList.h"

PLinkList InitList()
{
	PLinkList list_head = (PLinkList)malloc(sizeof(LinkList));
	if(list_head == NULL){
		return 0;	
	}
	
	list_head->next = NULL;
	
	return list_head;
}

void addElem(PLinkList list, int index, ElemType *data)
{
	if(list == NULL && data == NULL && index < 0){
		return;
	}
	PLinkList list_temp = list;
	int i_num = 0;
	while(list_temp != NULL && (i_num < index-1)){
		list_temp = list_temp->next;
		i_num++;
	}
	PLinkList list_val = (PLinkList)malloc(sizeof(LinkList));
	if(list_val == NULL){
		return;	
	}
	memcpy(&list_val->data, data, sizeof(ElemType));
	list_val->next = list_temp->next;
	list_temp->next = list_val;

}
 
 void deleteElem(PLinkList list, int index)
 {
	if (list == NULL && index < 1) return;
	PLinkList list_temp = list;
	int i_num = 0;
	while(list_temp != NULL && (i_num < index - 1)){
		list_temp = list_temp->next;
		i_num++;
	}
	if (list_temp->next == NULL) return; 
	PLinkList dele_elem = list_temp->next;
	list_temp->next = list_temp->next->next;
	
	free(dele_elem);	
 }
 
 void changeElem(PLinkList list, int index, ElemType *data)
 {
 	if (list == NULL && data == NULL && index < 1) return;
 	PLinkList list_list_temp = list->next;
 	int i = 0;
 	while(list_list_temp != NULL){	
 		if (i == index-1){
 			list_list_temp->data.int_val = data->int_val;
 			list_list_temp->data.char_val = data->char_val;
 			strcpy(list_list_temp->data.string_val,data->string_val);
 		}
 		list_list_temp = list_list_temp->next; 
 		i++;		
 	}
 }
 
 int examineElem(PLinkList list, ElemType *data)
 {	
 	if (list == NULL && data == NULL ) return 0;
 	PLinkList examine_list_temp = list->next;
 	int return_examine = 0;
 	while(examine_list_temp != NULL){
	 	return_examine++;	
 		if(examine_list_temp->data.int_val == data->int_val &&
 			examine_list_temp->data.char_val == data->char_val &&
 			strcmp(examine_list_temp->data.string_val, data->string_val)>=0){
 				return return_examine;
 			return_examine = examine_list_temp->data.int_val;
 		}
 		examine_list_temp = examine_list_temp->next; 		
 	}
 	
 	return 0;	
 }
 
 void printElem(PLinkList list)
 {
 	if (list == NULL) return;
 	PLinkList list_temp = list->next;
 	
 	printf("{");
 	while(list_temp != NULL){
 		printf("[%d-%c-%s]",list_temp->data.int_val, list_temp->data.char_val, list_temp->data.string_val);
 		list_temp = list_temp->next;
 	}
 	printf("}\n");
 }
 
 int leng(PLinkList list)
 {
 	if (list == NULL ) return 0;
 	PLinkList list_temp = list->next;
 	
 	int list_leng = 0;
 	while(list_temp != NULL){
 		list_temp = list_temp->next;
 		list_leng++;
 	}	
 	
 	return list_leng;
 }
main函数测试
int main()
{
	PLinkList i = InitList();
	ElemType j;
	
	j.int_val = 1, j.char_val = 'a', strcpy(j.string_val,"aaa"); addElem(i,1,&j);
	j.int_val = 2, j.char_val = 'b', strcpy(j.string_val,"bbb"); addElem(i,1,&j);
	j.int_val = 3, j.char_val = 'c', strcpy(j.string_val,"ccc"); addElem(i,1,&j);
	printf("-------------增----------------\n\n");
	printf("LinkList length:%d\n",leng(i)); 
	printElem(i);
	printf("-------------删----------------\n\n");
	deleteElem(i, 2);
	printElem(i);
	printf("-------------改----------------\n\n");
	j.int_val = 0, j.char_val = 'o', strcpy(j.string_val,"ooo");
	changeElem(i, 1, &j);
	printElem(i);
	printf("-------------查----------------\n\n");
	j.int_val = 0, j.char_val = 'o', strcpy(j.string_val,"ooo");
	printf("List_Elem index:%d\n",examineElem(i, &j));

	
	return 0;
}

结果

-----------------------------

LinkList length:3
{[3-c-ccc][2-b-bbb][1-a-aaa]}
-----------------------------

{[3-c-ccc][1-a-aaa]}
-----------------------------

{[0-o-ooo][1-a-aaa]}
-----------------------------

List_Elem index:1

--------------------------------
Process exited after 0.02477 seconds with return value 0
请按任意键继续. . .

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/872639.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存