/* 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<stdio.h>#include<stdlib.h>#include<time.h>typedefint ELEM_TYPE;typedefstruct _slnode
{
ELEM_TYPE data;struct _slnode* next;} slnode,* sig_link_list;// Head node is exited in the linked list!intsig_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");return0;}(*list)->next =NULL;return1;}/* 1 <= pos <= list_length */intsig_link_list_get_elem(const sig_link_list list,constint pos, ELEM_TYPE* elem){
slnode* temp;int i =1;
temp = list->next;while(temp &&(i < pos)){
temp = temp->next;
i++;}if(!temp || i > pos)return0;*elem = temp->data;return1;}/* 1 <= *pos <= list_length */intsig_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)return0;return1;}/* 1 <= pos <= list_length */intsig_link_list_insert(sig_link_list* list,constint 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))return0;
temp2 =(slnode*)malloc(sizeof(slnode));
temp2->data = elem;
temp2->next = temp1->next;
temp1->next = temp2;return1;}/* 1 <= pos <= list_length */intsig_link_list_delete(sig_link_list* list,constint 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))return0;
temp2 = temp1->next;
temp1->next = temp2->next;*elem = temp2->data;free(temp2);return1;}intsig_link_list_create_by_head_insert(sig_link_list* list,constint len){if(len <=0){printf("Error: invalid arguments!\n");return0;}
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;}return1;}intsig_link_list_create_by_tail_insert(sig_link_list* list,constint len){if(len <=0){printf("Error: invalid arguments!\n");return0;}
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;return1;}voidsig_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;}voidsig_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;}