#include <stdio.h>
#include <stdlib.h>
typedef enum{ERROR = 0,SUCCESS}Status;
struct LNode{
int data;
struct LNode *next;
}LNode;
typedef struct LNode* LinkList;
LinkList CreatList(){
LinkList headlist = (LinkList)malloc(sizeof(LNode));
if(headlist == NULL){
printf("内存申请失败\r\n");
return NULL;
}
headlist->next = NULL;
return headlist;
}
LinkList CreatNode(int dat){
LinkList newnode = (LinkList)malloc(sizeof(LNode));
newnode->data = dat;
newnode->next = NULL;
return newnode;
}
void InsertNode_ByHead(LinkList headnode,int dat){
LinkList newnode = CreatNode(dat);
newnode->next = headnode->next;
headnode->next = newnode;
}
void InsertNode_ByTail(LinkList headnode,int dat){
LinkList newnode = CreatNode(dat);
LinkList plisttail = headnode;
while(plisttail->next != NULL){
plisttail = plisttail->next;
}
plisttail->next = newnode;
}
void InsertNode_ByAnyLocation(LinkList headnode,int dat,int location){
LinkList new_node = CreatNode(dat);
LinkList plist = headnode->next;
LinkList plistfront = headnode;
int length = 1;
if((headnode->next == NULL && location != 1) || (location < 1))
return;
while(length++ != location){
if(plist->next == NULL && length != location)
return;
plistfront = plist;
plist = plist->next;
}
new_node->next = plist;
plistfront->next = new_node;
}
int SearchList_ByLocation(LinkList headnode,int location){
LinkList plist = headnode;
if(location < 1)
return -1;
for(int i = 1;i <= location;i++){
if(plist->next == NULL)
return -1;
plist = plist->next;
}
return plist->data;
}
int SearchList_ByData(LinkList headnode,int search_data){
LinkList plist = headnode->next;
int length = 1;
if(headnode->next == NULL)
return -1;
while(plist->data != search_data){
if(plist->next == NULL)
return -1;
plist = plist->next;
length++;
}
return length;
}
void DeleteList_PointData(LinkList headnode,int dat){
LinkList nodenext = headnode->next;
LinkList nodefront = headnode;
if(nodenext == NULL){
printf("空链表无法删除\r\n");
return;
}
while(nodenext->data != dat){
nodefront = nodenext;
nodenext = nodefront->next;
if(nodenext == NULL){
printf("链表中无此数据\r\n");
return;
}
}
nodefront->next = nodenext->next;
free(nodenext);
}
void DeleteList_PointLocation(LinkList headnode,int location){
LinkList plist = headnode->next;
LinkList plistfront = headnode;
int length = 1;
if((location < 1) && (headnode->next == NULL)){
printf("未知错误或空链表导致的无法删除\r\n");
return;
}
while(length++ != location){
if(plist->next == NULL){
printf("无此位置\r\n");
return;
}
plistfront = plist;
plist = plist->next;
}
plistfront->next = plist->next;
free(plist);
}
void PrintList(LinkList headnode){
LinkList pmove = headnode->next;
while(pmove){
printf("%d\n",pmove->data);
pmove = pmove->next;
}
}
int main(){
LinkList head = CreatList();
LinkList tail = CreatList();
LinkList empty_list = CreatList();
printf("=================头插法====================\n");
for(int i = 10;i > 0;i--){
InsertNode_ByHead(head,i);
}
PrintList(head);
printf("=================删除链表中的数据10==================\n");
DeleteList_PointData(head,10);
PrintList(head);
printf("=================尾插法====================\n");
for(int i = 1;i <= 10;i++){
InsertNode_ByTail(tail,i);
}
PrintList(tail);
printf("=================查找第八位数据====================\n");
printf("%d\r\n",SearchList_ByLocation(tail,8));
printf("=================查找8的位置====================\n");
printf("%d\r\n",SearchList_ByData(tail,8));
printf("=================在第八位插入100并打印队列====================\n");
InsertNode_ByAnyLocation(tail,100,8);
PrintList(tail);
printf("=================删除第8位数据====================\n");
DeleteList_PointLocation(tail,8);
PrintList(tail);
return 0;
}