线性表的单链式存储结构实现

/* sig_link_list.c
 *
 * Copyright (c) 2020
 * Author: plant
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include 
#include 
#include 

typedef int ELEM_TYPE;

typedef struct _slnode
{
	ELEM_TYPE data;
	struct _slnode* next;
} slnode, * sig_link_list;	// Head node is exited in the linked list!

int sig_link_list_init(sig_link_list* list)
{
	*list = (sig_link_list)malloc(sizeof(slnode));
	if ((*list) == NULL)
	{
		printf("Error: failed to allocate memory size!\n");
		return 0;
	}
	(*list)->next = NULL;
	return 1;
}

/* 1 <= pos <= list_length */
int sig_link_list_get_elem(const sig_link_list list, const int pos, ELEM_TYPE* elem)
{
	slnode* temp;
	int i = 1;
	temp = list->next;
	while (temp && (i < pos))
	{
		temp = temp->next;
		i++;
	}
	if (!temp || i > pos) return 0;
	*elem = temp->data;
	return 1;
}

/* 1 <= *pos <= list_length */
int sig_link_list_locate_elem(const sig_link_list list, const ELEM_TYPE elem, int* pos)
{
	slnode* temp;
	int i = 1;
	temp = list->next;
	while (temp)
	{
		if (temp->data == elem)
		{
			*pos = i;
			break;
		}
		temp = temp->next;
		i++;
	}
	if (!temp) return 0;
	return 1;
}

/* 1 <= pos <= list_length */
int sig_link_list_insert(sig_link_list* list, const int pos, const ELEM_TYPE elem)
{
	slnode* temp1, * temp2;
	int i = 1;
	temp1 = *list;
	while (temp1 && (i < pos))
	{
		temp1 = temp1->next;
		i++;
	}
	if (!temp1 || (i > pos)) return 0;
	temp2 = (slnode*)malloc(sizeof(slnode));
	temp2->data = elem;
	temp2->next = temp1->next;
	temp1->next = temp2;
	return 1;
}

/* 1 <= pos <= list_length */
int sig_link_list_delete(sig_link_list* list, const int pos, ELEM_TYPE* elem)
{
	slnode* temp1, * temp2;
	int i = 1;
	temp1 = *list;
	while (temp1->next && (i < pos))
	{
		temp1 = temp1->next;
		i++;
	}
	if (!temp1->next || (i > pos)) return 0;
	temp2 = temp1->next;
	temp1->next = temp2->next;
	*elem = temp2->data;
	free(temp2);
	return 1;
}

int sig_link_list_create_by_head_insert(sig_link_list* list, const int len)
{
	if (len <= 0)
	{
		printf("Error: invalid arguments!\n");
		return 0;
	}
	slnode* temp;
	srand(time(0));
	*list = (sig_link_list)malloc(sizeof(slnode));
	(*list)->next = NULL;
	for (int i = 0; i < len; i++)
	{
		temp = (slnode*)malloc(sizeof(slnode));
		temp->data = (ELEM_TYPE)(rand() % 100 + 1);
		temp->next = (*list)->next;
		(*list)->next = temp;
	}
	return 1;
}

int sig_link_list_create_by_tail_insert(sig_link_list* list, const int len)
{
	if (len <= 0)
	{
		printf("Error: invalid arguments!\n");
		return 0;
	}
	slnode* temp1, * temp2;
	srand(time(0));
	*list = (sig_link_list)malloc(sizeof(slnode));
	(*list)->next = NULL;
	temp2 = *list;
	for (int i = 0; i < len; i++)
	{
		temp1 = (slnode*)malloc(sizeof(slnode));
		temp1->data = (ELEM_TYPE)(rand() % 100 + 1);
		temp2->next = temp1;
		temp2 = temp1;
	}
	temp2->next = NULL;
	return 1;
}

void sig_link_list_clear(sig_link_list* list)
{
	slnode* temp1, * temp2;
	temp1 = (*list)->next;
	while (temp1)
	{
		temp2 = temp1->next;
		free(temp1);
		temp1 = temp2;
	}
	(*list)->next = NULL;
	return;
}

void sig_link_list_print(const sig_link_list list)
{
	slnode* temp;
	temp = list->next;
	if (!temp) return;
	printf("node\tvalue\t\n");
	int i = 0;
	while (temp)
	{
		printf("%d\t%d\n", i, (int)temp->data);
		temp = temp->next;
		i++;
	}
	printf("length:%d\n", i);
	return;
}

你可能感兴趣的:(数据结构)