1. 作业1
单链表的按位置修改
/*
* funciotn: chnage item with position
* @param [ in] linklist
* @param [ in] position
* @return 0 for success, -1 for fail
*/
int update_pos(linklist L, int pos, datatype e)
{
if(L == NULL || L->len == 0 || pos < 1 || pos > L->len)
return -1;
linklist p = L;
for(int i = 0; i < pos; i++) p = p->next;
p->data = e;
return 0;
}
2. 作业2
单链表的按元素删除
/*
* funciotn: delete item with specific key
* @param [ in] linklist
* @param [ in] key
* @return 0 for success, -1 for fail
*/
int delete_data(linklist L, datatype key)
{
if(L == NULL || L->len == 0) return -1;
int index = search_data(L, key);
if(index == -1) return -1;
int flag = delete_pos(L, index);
if(flag == -1) return -1;
return 0;
}
3. 作业3
单链表的按元素修改
/*
* funciotn: update target node whit specific data
* @param [ in] the target data
* @param [ in] new data for update
* @return 0 for success, -1 for fail
*/
int update_data(linklist L, datatype key, datatype e)
{
if(L == NULL || L->len == 0) return -1;
int index = search_data(L, key);
if(index == -1) return -1;
int flag = update_pos(L, index, e);
if(flag == -1) return -1;
return 0;
}
4. 作业4
单链表的空间释放
/*
* funciotn: free space
* @param [ in] linklist
*/
void destroy(linklist L)
{
while(L->next) delete_head(L);
free(L);
L = NULL;
}