#include "linklist.h"
int create_linklist(linklistPtr* L){
if(!((*L) = (linklistPtr)malloc(sizeof(linklist))))
return 1;
(*L)->data = 234;
(*L)->next =NULL;
return 0;
}
int insert_linklist(linklistPtr* L){
int choose, value;
linklistPtr node, mark;
do {
printf("Choose head or rear, 1 to head, 2 to rear, 0 to exit:\n");
scanf("%d", &choose);
fflush(stdin);
switch (choose) {
case 1:
printf("Enter a value:\n");
scanf("%d", &value);
if (!(node = (linklistPtr)malloc(sizeof(linklist))))
return 1;
node->data = value;
node->next = (*L)->next;
(*L)->next = node;
break;
case 2:
printf("Enter a value:\n");
scanf("%d", &value);
if (!(node = (linklistPtr)malloc(sizeof(linklist))))
return 1;
mark = (*L);
while (mark->next)
mark = mark->next;
node->data = value;
node->next = NULL;
mark->next = node;
break;
default:
break;
}
} while (choose != 0);
return 0;
}
int del_linklist(linklistPtr* L, int value){
linklistPtr prePtr, curPtr;
prePtr = (*L);
curPtr = (*L);
while (curPtr->data != value) {
if(!curPtr)
return 1;
prePtr = curPtr;
curPtr = curPtr->next;
}
prePtr->next = curPtr->next;
curPtr->next = NULL;
free(curPtr);
return 0;
}
int clear_linklist(linklistPtr* L){
linklistPtr delnode;
while ((*L)->next) {
delnode = (*L)->next;
(*L)->next = delnode->next;
free(delnode);
}
return 0;
}
int get_linklist(linklistPtr* L, int position){
linklistPtr curPtr;
curPtr = (*L);
for (int i = 0; i < position; i++){
if (!curPtr)
return 1;
curPtr = curPtr->next;
}
return curPtr->data;
}
int locate_linklist(linklistPtr L, int value){
linklistPtr locatePtr;
locatePtr = L;
int count = 0;
while (locatePtr->data != value) {
if (!locatePtr)
return 1;
locatePtr = locatePtr->next;
count++;
}
return count;
}
int traversal_linklist(linklistPtr L){
//do not print the head node
L = L->next;
while (L) {
printf("%d", L->data);
L = L->next;
}
printf("\n");
return 0;
}
int isEmpty_linklist(linklistPtr L){
return L->next == NULL ? 0 : 1;
}
(一)链表函数定义文件
最新推荐文章于 2022-10-16 01:12:42 发布