C语言 数据结构链表的实例(十九种操作)

时间:2021-05-20

C语言 数据结构链表的实例(十九种操作)

#include <stdio.h>#include <string.h>#include <stdlib.h>/*************************************************************************************//*************************************************************************************/typedef int elemType;typedef struct NODE{ elemType element; struct NODE *next;} Node;void creatList(Node **pHead){ printf("Please enter the list:\n"); Node *p1, *p2; p1 = p2 = (Node *)malloc(sizeof(Node)); if (p1 == NULL || p2 == NULL) exit(0); memset(p1, 0, sizeof(Node)); scanf("%d", &p1->element); p1->next = NULL; while(p1->element > 0) { if (*pHead == NULL) (*pHead) = p1; else p2->next = p1; p2 = p1; p1 = (Node *)malloc(sizeof(Node)); if (p1 == NULL) exit(0); memset(p1, 0, sizeof(Node)); scanf("%d", &p1->element); p1->next = NULL; }}void printList(Node *pHead){ if (NULL == pHead) printf("The list is empty\n"); else while(NULL != pHead) { printf("%d ", pHead->element); pHead = pHead->next; } printf("\n");}int sizeList(Node *pHead){ int size = 0; while(pHead != NULL) { size ++; pHead = pHead->next; } return size;}void isEmptyList(Node *pHead){ if (pHead == NULL) { printf("The list is empty\n"); exit(0); }}void getElement(Node *pHead, int num){ for (int i = 1; i < num; ++i) pHead = pHead->next; printf("The value of the %dth element is:%d\n", num, pHead->element);}int getElemAddr(Node *pHead, int number){ int i = 1; while(pHead != NULL) { if (pHead->element == number) return i; i++; pHead = pHead->next; } return 0;}void modifyElem(Node **pList, int addr, int number){ Node *pHead; //在此处如果直接更改pList指向的话,主函数中调用printList就会从addr处开始打印 int i = 1; pHead = *pList; while(pHead != NULL) { if (i == addr) break; pHead = pHead->next; i++; } pHead->element = number;}void insertHeadList(Node **pHead){ Node *p1; p1 = (Node *)malloc(sizeof(Node)); if (p1 == NULL) exit(0); memset(p1, 0, sizeof(Node)); printf("Please enter a number to be inserted:"); scanf("%d", &p1->element); p1->next = (*pHead);// 此时pHead指向的是第一个结点(有数据域的),所以新的结点要插入到头结点前 (*pHead) = p1; // pHead指向第一个结点}void insertLastList(Node **pHead, int n){ Node *p1, *p2; p2 = (*pHead); int i; for (i = 1; i < n; ++i) p2 = p2->next; p1 = (Node *)malloc(sizeof(Node)); if (p1 == NULL) exit(0); memset(p1, 0, sizeof(Node)); printf("Please enter a number to be inserted:"); scanf("%d", &p1->element); p1->next = NULL; p2->next = p1;}void isAddPos(Node **pHead, int length){ Node *p1, *p2; int position, i; printf("Please enter the insert position:"); scanf("%d", &position); if (position > length || position <= 0) { printf("Input error, the program ends\n"); exit(0); } p1 = (Node *)malloc(sizeof(Node)); p2 = (*pHead); if (p1 == NULL) exit(0); memset(p1, 0, sizeof(Node)); printf("Please enter a number to be inserted:"); scanf("%d", &p1->element); for (i = 1; i < position - 1; ++i) p2 = p2->next; p1->next = p2->next; p2->next = p1;}void Arrange(Node **pHead, int length){ Node *p1; p1 = (*pHead); int i, j, temp; for (i = length; i > 0; --i) { for(j = i - 1; j > 0; --j) { if ((p1->element) > (p1->next->element)) { temp = p1->element; p1->element = p1->next->element; p1->next->element = temp; } p1 = p1->next; } p1 = (*pHead); }}int OrrderList(Node **pHead, int length){ Node *p1, *p2; p1 = (*pHead); p2 = (Node *)malloc(sizeof(Node)); if (p2 == NULL) exit(0); memset(p2, 0, sizeof(Node)); printf("Enter the value of the element to be inserted:"); scanf("%d", &p2->element); if (p2->element < p1->element) { p2->next = p1; (*pHead) = p2; return 1; } while(p1->next != NULL && p2->element > (p1->next->element)) p1 = p1->next; if (p1->next == NULL) { p2->next = NULL; p1->next = p2; return 1; } else { p2->next = p1->next; p1->next = p2; return 1; }}void DelHeadList(Node **pHead){ Node *p1; p1 = (*pHead); (*pHead) = (*pHead)->next; free(p1);}void DelLastList(Node **pHead){ Node *p1, *p2; p1 = (*pHead); p2 = p1->next; while(p2->next != NULL) { p2 = p2->next; p1 = p1->next; } p1->next = NULL; free(p2);}void DelPos(Node **pHead, int length){ int n, i; Node *p1, *p2; p1 = (*pHead); p2 = p1->next; printf("Please enter the serial number number to delete:"); scanf("%d", &n); if (n < 1 || n > length) exit(0); for (i = 1; i < n - 1; ++i) { p2 = p2->next; p1 = p1->next; } p1->next = p2->next; free(p2);}int Delx(Node **pHead){ Node *p1, *p2; p1 = (*pHead); p2 = p1->next; int number; printf("Please input is going to be deleted the value of x:"); scanf("%d", &number); if (number == (*pHead)->element) { (*pHead) = (*pHead)->next; free(p1); return 1; } while(p2 != NULL) { if (p2->element == number) { break; } p2 = p2->next; p1 = p1->next; } if (p2 == NULL) { printf("X does not exist in the list\n"); return 1; } else { p1->next = p2->next; free(p2); return 1; }}void exchange2pos(Node **pHead, int length){ Node *p1, *p2; int n1, n2, i, j, temp; printf("Please enter the first number:"); scanf("%d", &n1); printf("Please enter the second number:"); scanf("%d", &n2); if (n1 < 1 || n1 > length || n2 < 1 || n2 > length) exit(0); p1 = p2 = (*pHead); for (i = 1; i < n1; ++i) { p1 = p1->next; } for (j = 1; j < n2; ++j) { p2 = p2->next; } temp = p1->element; p1->element = p2->element; p2->element = temp;}void clearList(Node **pHead){ Node *p1; p1 = (*pHead); while(p1 != NULL) { p1 = p1->next; free((*pHead)); (*pHead) = p1; }}int main(int argc, char const *argv[]){ Node *pList = NULL; int length = 0, n, addr, number; printf("- - - - - - - - - 2 - - - - - - - -\n"); creatList(&pList); isEmptyList(pList); printList(pList); printf("- - - - - - - - - 4 - - - - - - - -\n"); length = sizeList(pList); printf("the Node length is:%d\n", length); printf("- - - - - - - - - 7 - - - - - - - -\n"); printf("Please input node number (n):"); scanf("%d", &n); if (n > length || n < 1) { printf("N is not within the scope of\n"); exit(0); } getElement(pList, n); printf("- - - - - - - - - 8 - - - - - - - -\n"); addr = 0; number; printf("Please enter to find element value (number):"); scanf("%d", &number); addr = getElemAddr(pList, number); if (addr == 0) printf("List the element\n"); else printf("The location of the number is:%d\n", addr); printf("- - - - - - - - - 9 - - - - - - - -\n"); addr = 0; number = 0; printf("Please input to replace the serial number (n):"); scanf("%d", &addr); if (addr > length || addr < 0) { printf("N is not within the scope of\n"); exit(0); } printf("Please input to replace the contents of the (number):"); scanf("%d", &number); modifyElem(&pList, addr, number); printf("The revised list is:\n"); printList(pList); printf("- - - - - - - - - 10 - - - - - - - -\n"); insertHeadList(&pList); printList(pList); printf("- - - - - - - - - 11 - - - - - - - -\n"); insertLastList(&pList, length); printList(pList); printf("- - - - - - - - - 12 - - - - - - - -\n"); isAddPos(&pList, length); printList(pList); printf("- - - - - - - - - 6 - - - - - - - -\n"); Arrange(&pList, length); printList(pList); printf("- - - - - - - - - 13 - - - - - - - -\n"); OrrderList(&pList, length); printList(pList); printf("- - - - - - - - - 14 - - - - - - - -\n"); DelHeadList(&pList); printList(pList); printf("- - - - - - - - - 15 - - - - - - - -\n"); DelLastList(&pList); printList(pList); printf("- - - - - - - - - 16 - - - - - - - -\n"); DelPos(&pList, length); printList(pList); printf("- - - - - - - - - 17 - - - - - - - -\n"); Delx(&pList); printList(pList); printf("- - - - - - - - - 18 - - - - - - - -\n"); exchange2pos(&pList, length); printList(pList); printf("- - - - - - - - - 19 - - - - - - - -\n"); clearList(&pList); printList(pList); return 0;}

以上就是C语言数据结构单链表的操作,所有基础操作都包含在内,很全面,本站对于数据结构的文章还很多,希望大家搜索查阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章