链表
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct NODE{ int value; struct NODE *next; } Node; int createHeadNode(Node **headLocation) { //创建头节点 Node *new = (Node *)malloc(sizeof(Node)); printf("createHeadNode : %d\n", new); if (NULL == new) { printf("createHeadNode error\n"); return 0; } else { //初始化头节点(将所有数据都置为零,包括指针也指向空) memset(new, 0, sizeof(Node)); //将头节点地址赋给头指针 *headLocation = new; } return 1; } void printList(Node *head) { printf("------------------打印所有的节点-------------------\n"); printf("%d [head]\n", head); Node *tempNode = head; int number = 1; while (NULL != tempNode->next) { printf("%d [node%02d] : %d\n", tempNode->next, number, tempNode->next->value); tempNode = tempNode->next; number++; } printf("---------------------------------------------------\n"); } int getListLength(Node *head) { int len = 0; Node *tempNode = head; while (NULL != tempNode->next) { len++; tempNode = tempNode->next; } return len; } int insertNode(Node *head, int value) { Node *tempNode = head; //创建新节点 Node *new = (Node *)malloc(sizeof(Node)); if (NULL == new) { printf("insertNode error\n"); return 0; } //新节点赋值 new->value = value; new->next = NULL; //寻找尾节点 while (NULL != tempNode->next) { tempNode = tempNode->next; } //尾节点->next指向新节点 tempNode->next = new; return 1; } int autoInsert(Node *head) { for (int i = 1; i <= 11; i++) { if (0 == insertNode(head, i)) { return 0; } } return 1; } int updateNode(Node *head) { int len = 0; int num = 0; int value = 0; Node *tempNode = head; len = getListLength(head); printf("请输入需要修改的节点序号:"); scanf("%d", &num); if (num <= 0 || num > len) { printf("节点序号无效\n"); return 0; } int i = 1; while (i != num) { tempNode = tempNode->next; i++; } printf("请输入需要修改成的值:"); scanf("%d", &value); tempNode->next->value = value; printf("修改成功\n"); return 1; } int deleteNode(Node *head) { int len = 0; int num = 0; Node *tempNode = head; //用于寻找需要的那个节点 Node *freeNode = NULL; //用于接收要释放的节点 len = getListLength(head); printf("请输入需要删除的节点序号:"); scanf("%d", &num); if (num <= 0 || num > len) { printf("节点序号无效\n"); return 0; } int i = 1; while (i != num) { tempNode = tempNode->next; i++; } //freeNode接收要释放的节点 freeNode = tempNode->next; //将更后面的节点赋值给tempNode->next tempNode->next = tempNode->next->next; free(freeNode); printf("删除成功\n"); return 1; } int main(int argc, char *argv[]) { //创建头指针 Node *head = NULL; int listLength = 0; //创建头节点 if (!createHeadNode(&head)) { return 0; } //自动插入五个节点 if (!autoInsert(head)) { return 0; } printf("---------------------function----------------------\n"); printf("(1)print (2)insert\n"); printf("(3)update (4)delete\n"); printf("---------------------------------------------------\n"); while (1) { int function = 0; int value = 0; printf("请选择功能:"); scanf("%d", &function); switch (function) { case 1: printList(head); break; case 2: printf("请输入节点的value:"); scanf("%d", &value); insertNode(head, value); break; case 3: updateNode(head); break; case 4: deleteNode(head); break; default: printf("无此功能!!\n"); } } return 1; }