有头单链表的一般写法
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}NODE,*LPNODE;
LPNODE createHead() {
LPNODE headNode = (LPNODE)malloc(sizeof(NODE));
if (NULL == headNode) {
printf("头节点内存申请失败!\n");
return NULL;
}
headNode->next = NULL;
return headNode;
}
LPNODE createNode(int data) {
LPNODE newNode = (LPNODE)malloc(sizeof(NODE));
if (NULL == newNode) {
printf("数据节点内存申请失败!\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertByHead(LPNODE headNode,int data) {
LPNODE newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void insertByTail(LPNODE headNode, int data) {
LPNODE newNode = createNode(data);
LPNODE curNode = headNode;
while (curNode->next != NULL) {
curNode = curNode->next;
}
curNode->next = newNode;
}
void insertByAppoint(LPNODE headNode, int data, int pos) {
LPNODE newNode = createNode(data);
LPNODE curNode = headNode->next;
while (curNode->next != NULL && --pos) {
curNode = curNode->next;
}
newNode->next = curNode->next;
curNode->next = newNode;
}
void deleteByHead(LPNODE headNode) {
LPNODE delNode = headNode->next;
if (NULL == delNode) {
printf("链表为空, 删除失败!\n");
return;
}
headNode->next = delNode->next;
free(delNode);
delNode = NULL;
}
void deleteByTail(LPNODE headNode) {
LPNODE curNode = headNode->next;
LPNODE preNode = headNode;
if (NULL == curNode) {
printf("链表为空, 删除失败!\n");
return;
}
while (curNode->next != NULL) {
preNode = curNode;
curNode = curNode->next;
}
free(curNode);
curNode = NULL;
preNode->next = NULL;
}
void deleteByTail2(LPNODE headNode) {
LPNODE delNode = NULL;
LPNODE curNode = headNode;
if (NULL == headNode->next) {
printf("链表为空,删除失败!\n");
return;
}
while (curNode->next->next != NULL) {
curNode = curNode->next;
}
delNode = curNode->next;
curNode->next = NULL;
free(delNode);
delNode = NULL;
}
void deleteByAppoint(LPNODE headNode, int posData) {
LPNODE preNode = headNode;
LPNODE curNode = headNode->next;
if (NULL == preNode->next) {
printf("链表为空,无数据可以删除!\n");
return;
}
while (curNode != NULL && curNode->data != posData) {
preNode = curNode;
curNode = curNode->next;
}
if (NULL == curNode) {
printf("未找到指定数据!\n");
return;
}
else {
preNode->next = curNode->next;
free(curNode);
curNode = NULL;
}
}
LPNODE searchByData(LPNODE headNode, int posData) {
LPNODE curNode = headNode->next;
while (curNode != NULL && curNode->data != posData) {
curNode = curNode->next;
}
return curNode;
}
void deleteAll(LPNODE headNode, int posData) {
while (searchByData(headNode, posData) != NULL) {
deleteByAppoint(headNode, posData);
}
}
void printList(LPNODE headNode) {
LPNODE curNode = headNode->next;
while (curNode != NULL) {
printf("%d\t", curNode->data);
curNode = curNode->next;
}
printf("\n");
}
int main()
{
printf("---- 头插 -----: \n");
LPNODE list = createHead();
for (int i = 0; i < 3; i++) {
insertByHead(list, 520 + i);
insertByHead(list, 520 + i);
}
printList(list);
printf("---- 尾插 -----: \n");
insertByTail(list, 1314);
printList(list);
printf("---- 指定位置插入 -----: \n");
insertByAppoint(list, 666, 3);
printList(list);
printf("---- 头删 -----: \n");
deleteByHead(list);
printList(list);
printf("---- 尾删 -----: \n");
deleteByTail(list);
printList(list);
printf("---- 尾删2 -----: \n");
deleteByTail2(list);
printList(list);
printf("---- 指定数据521删除 -----: \n");
deleteByAppoint(list, 521);
printList(list);
printf("---- 指定数据522全部删除 -----: \n");
deleteAll(list, 522);
printList(list);
system("pause");
return 0;
}