头文件:
#include<stdio.h>
#include <stdlib.h>
struct int_slinkedlist_node;
struct int_slinkedlist;
int j;
int ret;
struct int_slinkedlist * int_slinklist_init();
void int_slinkedlist_free(struct int_slinkedlist* list);
void int_slinkedlist__clear(struct int_slinkedlist* list);
int int_slinkedlist_length(struct int_slinkedlist* list);
void int_slinkedlist_add(struct int_slinkedlist* list, int index, int value);
void int_slinkedlist_remove(struct int_slinkedlist* list, int index);
void int_slinkedlist_set(struct int_slinkedlist* list, int index, int value);
int int_slinkedlist_get(struct int_slinkedlist* list, int index);
int int_slinkedlist_lookup(struct int_slinkedlist* list, int value);
int int_slinkedlist_isempty(struct int_slinkedlist* list);
void show(struct int_slinkedlist* list);
函数的实现:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include"listtsq.h"
struct int_slinkedlist_node{
int data;
struct int_slinkedlist_node* next;
};
struct int_slinkedlist{
struct int_slinkedlist_node* head;
int length;
};
struct int_slinkedlist * int_slinklist_init(){
struct int_slinkedlist *list = NULL;
list = (struct int_slinkedlist *)malloc(sizeof(struct int_slinkedlist));
list->head = (struct int_slinkedlist_node*)malloc(sizeof(struct int_slinkedlist_node));
list->length = 0;
list->head->data = -1;
list->head->next = NULL;
return list;
}
void int_slinkedlist_free(struct int_slinkedlist* list){
struct int_slinkedlist_node* comp = NULL;
while (list->head != NULL)
{
struct int_slinkedlist_node* comp = list->head;
list->head = comp->next;
free(comp);
}
free(list);
}
void int_slinkedlist__clear(struct int_slinkedlist* list){
struct int_slinkedlist_node* comp = NULL;
while (list->head->next != NULL)
{
struct int_slinkedlist_node* comp = list->head->next;
list->head->next = comp->next;
free(comp);
}
list->length = 0;
}
int int_slinkedlist_length(struct int_slinkedlist* list){
return list->length;
}
void int_slinkedlist_add(struct int_slinkedlist* list, int index, int value){
int i;
struct int_slinkedlist_node* comp2 = NULL;
struct int_slinkedlist_node* comp3 = NULL;
comp2 = list->head;
for (i = 0; i<index - 1; i++)//或while(comp2!=NULL&&I<index-1)
{
comp2 = comp2->next;
}
comp3 = (struct int_slinkedlist_node*)malloc(sizeof(struct int_slinkedlist_node));
comp3->data = value;
comp3->next = comp2->next;
comp2->next = comp3;
list->length++;
}
void int_slinkedlist_remove(struct int_slinkedlist* list, int index)
{
int i = 0;
struct int_slinkedlist_node* comp2 = NULL;
struct int_slinkedlist_node* comp3 = NULL;
comp2 = list->head;
for (i = 0; i<index - 1; i++)//或while(comp2!=NULL&&I<index-1)//查找第index-1的元素
{
comp2 = comp2->next;
}
comp3 = comp2->next;
comp2->next = comp3->next;
free(comp3);
list->length--;
}
void int_slinkedlist_set(struct int_slinkedlist* list, int index, int value)
{
int i;
struct int_slinkedlist_node* comp2 = NULL;
comp2 = list->head;
for (i = 0; i<index; i++)//或while(comp2!=NULL&&I<index-1)//查找第index-1的元素
{
comp2 = comp2->next;
}
comp2->data = value;
}
int int_slinkedlist_get(struct int_slinkedlist* list, int index){
int i;
struct int_slinkedlist_node* comp2 = NULL;;
comp2 = list->head;
for (i = 0; i<index; i++)//或while(comp2!=NULL&&I<index-1)//查找第index-1的元素
{
comp2 = comp2->next;
}
return comp2->data;
}
int int_slinkedlist_lookup(struct int_slinkedlist* list, int value)//遍历操作
{
struct int_slinkedlist_node*comp2 = NULL;
comp2=list->head;
int i = 0;
while (comp2->data != value&&comp2->next != NULL){
comp2 = comp2->next;
i++;
}
if (comp2->data == value)
{
comp2 = NULL;
return i ;
}
else
comp2 = NULL;
return 0;
}
int int_slinkedlist_isempty(struct int_slinkedlist* list)
{
list->length = 0;
return list->length;
}
void show(struct int_slinkedlist* list)
{
int n = int_slinkedlist_length(list);
for (int i = 1; i <=n; i++)
{
printf("%d ", int_slinkedlist_get(list, i));
}
printf("%\n");
}
//
void GB(struct int_slinkedlist* p1, struct int_slinkedlist* p2)//归并
{
int t=int_slinkedlist_length(p1);
int q = int_slinkedlist_length(p2);
struct int_slinkedlist_node*comp3 = NULL;
int i = 1;
while (q--)
{
int_slinkedlist_add(p1, t + i, (int_slinkedlist_get(p2, i)));//从第一个单链表的末尾插入
i++;
}
}
main函数(测试):
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include"listtsq.h"
int main()
{
struct int_slinkedlist * p1;
struct int_slinkedlist * p2;
p1 = int_slinklist_init();//初始化单链表p1
p2 = int_slinklist_init();//初始化单链表p2
for (j = 0; j<10; j++)
{
int_slinkedlist_add(p1, j + 1, j+1);//初始化为0
int_slinkedlist_add(p2, j + 1, j + 1);//初始化为0
}
show(p1);
int p, q;
printf("输入插入元素的位置是:>值为:>");
scanf("%d%d", &p, &q);
int_slinkedlist_add(p1, p, q);//插入目标元素
show(p1);
printf("输入你要取的位置:>");
scanf("%d", &p);
ret = int_slinkedlist_get(p1,p);
printf("你所取的位置的值为%d\n", ret);
printf("设置你目标位置:>该目标位置的值为:>");
scanf("%d%d", &p, &q);
int_slinkedlist_set(p1, p, q);
show(p1);
printf("输入你想要查找的元素的值:");
scanf("%d", &p);
ret = int_slinkedlist_lookup(p1, p);//查找
printf("你所查找元素的位置是 %d\n", ret);
show(p1);
printf("输入你想要移除的元素的位置:>");
scanf("%d", &p);
int_slinkedlist_remove(p1, p);
show(p1);//输出全部元素序列
printf("另一个单链表的元素序列为:>");
show(p2);
printf("\n");
GB(p1, p2 );
printf("归并后的单链表为:>");
show(p1);
printf("是否要销毁表,please input 1/0");
printf("\n");
int z = 0;
scanf("%d", &z);
if (z == 1)
{
int_slinkedlist__clear(p1);
ret = int_slinkedlist_length(p1);
printf("表中元素的个数为%d\n", ret);
int_slinkedlist_free(p1);
}
return 0;
}
测试结果: