#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <stdbool.h>
typedef struct Node{ int data; struct Node * pNext; }NODE, * PNODE;
PNODE List_Init(); void List_Traverse(PNODE pHead); bool List_Insert(PNODE pHead, int pos, int val); bool List_Delete(PNODE pHead, int pos, int * pVal); int List_Length(PNODE pHead); PNODE List_FindValue(PNODE pHead, int val); PNODE List_FindOrder(PNODE pHead, int pos); void List_Sort(PNODE pHead);
int main(void) { int pVal; PNODE pHead = NULL; pHead = List_Init(); List_Traverse(pHead); List_Insert(pHead, 1, 34); List_Insert(pHead, 2, 14); List_Insert(pHead, 1, 94); List_Insert(pHead, 3, -4); List_Insert(pHead, 4, 4); List_Traverse(pHead); printf("当前链表的长度是:%d\n\n", List_Length(pHead)); if ( List_Delete(pHead, 3, &pVal) ){ printf("删除成功!删除的元素是:%d\n\n", pVal); } List_Traverse(pHead); printf("当前链表的长度是:%d\n\n", List_Length(pHead)); if ( List_FindValue(pHead, 33) ){ printf("是\n\n"); } else printf("否\n\n"); List_Sort(pHead); List_Traverse(pHead); return 0; }
PNODE List_Init(){ PNODE pHead = (PNODE)malloc(sizeof(NODE)); if ( pHead == NULL ){ exit(-1); } PNODE pTail = pHead; pTail->pNext = NULL; int n, i; printf("输入要创建的链表长度:"); scanf("%d", &n); for ( i = 0; i < n ; i++ ){ printf("第 %d 个节点的值:", i +1 ); int val; scanf("%d", &val); PNODE pNew = (PNODE)malloc(sizeof(NODE)); pNew->data = val; pTail->pNext = pNew; pTail = pNew; pTail->pNext = NULL; } return pHead; }
void List_Traverse(PNODE pHead){ PNODE q; int len, j; len = List_Length(pHead); for ( j = 0, q = pHead->pNext; j < len; j++, q = q->pNext ){ printf("%d ", q->data); } printf("\n"); }
int List_Length(PNODE pHead){ PNODE pNew = pHead->pNext; int length = 0; while ( pNew != NULL ){ length++; pNew = pNew->pNext; } return length; }
bool List_Insert(PNODE pHead, int pos, int val){ PNODE q; if ( pos == 1 ){ q = (PNODE)malloc(sizeof(NODE)); q->data = val; q->pNext = pHead->pNext; pHead->pNext = q; return true; } q = List_FindOrder(pHead, pos - 1); if ( !q ){ printf("结点不存在,插入失败!\n"); return false; } else{ PNODE pNew = (PNODE)malloc(sizeof(NODE)); pNew->data = val; pNew->pNext = q->pNext; q->pNext = pNew; return true; } }
bool List_Delete(PNODE pHead, int pos, int * pVal){ PNODE p; if ( pos == 1 ){ p = pHead->pNext; *pVal = p->data; pHead->pNext = p->pNext; free(p); return true; } p = List_FindOrder(pHead, pos - 1); if ( !p || !p->pNext ){ printf("结点不存在,删除失败!\n"); return false; } else{ PNODE q = p->pNext; *pVal = q->data; p->pNext = q->pNext; free(q); return true; } }
PNODE List_FindOrder(PNODE pHead, int pos){ int i = 1; PNODE p = pHead->pNext; while ( i < pos && NULL != p ){ i++; p = p->pNext; } if ( i == pos ) return p; else return NULL; }
PNODE List_FindValue(PNODE pHead, int val){ PNODE p = pHead->pNext; while ( NULL != p && p->data != val ){ p = p->pNext; } return p; }
void List_Sort(PNODE pHead){ int i, j, len = List_Length(pHead); PNODE p, q; for ( i = 0, p = pHead->pNext; i < len - 1; i++, p = p->pNext ) for ( j = 0, q = pHead->pNext; j < len - 1 - i; j++, q = q->pNext ) if ( q->data > q->pNext->data ){ int t = q->data; q->data = q->pNext->data; q->pNext->data = t; } }
|