/*
* study linklist1: dan lian biao
* with create init insert del change lookup
* date: 2020/12/11
*/
#include <stdio.h>
#include <stdlib.h>
#define OK 0
#define ERROR -1
typedef struct linklist
{
int data;
struct linklist * next;
}ln;
int llist_init(ln* headNode, int* data, int size);
int list_data(ln* headnode);
int llist_insert(ln* current, int new_value);//顺序存储,数据new_value插入操作
int llist_dst_insert(ln* current, int dst, int value);//在dst位置插入值value
int llist_dst_del(ln* current, int dst);//删除指定位置dst的结点
int llist_changevalue(ln* current, int dst, int value);//改变dst位置的值为value
int llist_lookup_value(ln* current, int dst);//查找对应结点dst的值
//查找链表中是否有对应的值value,存在返回结点地址,否则返回NULL
ln* llist_lookup_dict(ln* current, int value);
int main()
{
int ret;
ln* current;
int a[5] = {1,2,3,4,5};
ln* headnode = (ln *)malloc(sizeof(ln));
if(headnode == NULL)
{
perror("malloc error!\n");
return ERROR;
}
llist_init(headnode, a, 5);
list_data(headnode);
printf("\n");
llist_insert(headnode,3);
list_data(headnode);
printf("\n");
llist_dst_insert(headnode,4,10);
list_data(headnode);
printf("\n");
llist_dst_del(headnode,4);
list_data(headnode);
printf("\n");
llist_changevalue(headnode,4,9);
list_data(headnode);
printf("\n");
if(ret = llist_lookup_value(headnode,4))
{
printf("%d\n", ret);
}
current = llist_lookup_dict(headnode, 9);
if(current == NULL)
{
printf("this value is not in linklist!\n");
}
printf("this value is in linklist, value: %d\n", current->data);
return OK;
}
int llist_init(ln* headNode, int* data, int size)
{
int i;
ln * currentNode = headNode;
for(i = 0; i < size; i++)
{
currentNode->data = *(data+i);
if(i < size-1)
{
currentNode->next = (ln *)malloc(sizeof(ln));
if(currentNode->next == NULL)
{
perror("malloc error!\n");
return ERROR;
}
currentNode = currentNode->next;
}
}
currentNode->next = NULL;
return OK;
}
int list_data(ln* headnode)
{
ln* current = headnode;
do{
printf("%d\n", current->data);
current = current->next;
}while(current != NULL);
return OK;
}
int llist_insert(ln* current, int new_value)//顺序存储,数据new_value插入操作
{
ln* previous;
ln* new;
while(current->data < new_value)
{
previous = current;
current = current->next;
}
new = (ln *)malloc(sizeof(ln));
if(new == NULL)
{
perror("malloc error!\n");
return ERROR;
}
new->data = new_value;
new->next = current;
previous->next = new;
return OK;
}
int llist_dst_insert(ln* current, int dst, int value)//在dst位置插入值 value
{
int i;
ln* previous;
ln* new;
for(i = 0; i < dst-1; i++)
{
previous = current;
current = current->next;
}
new = (ln*)malloc(sizeof(ln));
if(new == NULL)
{
perror("malloc error!\n");
return ERROR;
}
new->data = value;
new->next = current;
previous->next = new;
return OK;
}
int llist_dst_del(ln* current, int dst)//删除指定位置dst的结点
{
int i;
ln* previous;
for(i = 0; i < dst-1; i++)
{
previous = current;
current = current->next;
}
previous->next = current->next;
free(current);
current = NULL;
return OK;
}
int llist_changevalue(ln* current, int dst, int value)//改变dst位置的值为value
{
int i;
for(i = 0; i < dst-1; i++)
{
current = current->next;
}
current->data = value;
return OK;
}
int llist_lookup_value(ln* current, int dst)//查找对应结点dst的值
{
int i, value;
for(i = 0; i < dst-1; i++)
{
current = current->next;
}
value = current->data;
return value;
}
ln* llist_lookup_dict(ln* current, int value)//查找链表中是否有对应的值value,存在返回结点地址,
{ //否则返回NULL
int i = 1;
while(current->next != NULL)
{
if(current->data == value)
{
printf("value:%d is in linklist, where: %d\n", value, i);
return current;
}
current = current->next;
i++;
}
return NULL;
}
单链表的创建,初始化,插入,删除,修改,查询
最新推荐文章于 2024-07-06 17:59:41 发布