涉及到链表的 创建、遍历、求长度、插入、删除、查找、排序。
零碎知识点
typedef struct Student { int sid; char name[100]; char sex; }* PSTU, STU;
int main(void) { STU st; PSTU ps = &st; ps->sid = 99; printf("%d", ps->sid); return 0; }
|
确定一个链表所需要的参数
头指针
每一个链表节点的数据类型该如何表示的问题
typedef struct Node //节点————每一个节点分为两部分,数据域和指针域 { int data; struct Node * pNext; }NODE, *PNODE;
|
1. r = p->pNext; p->pNext = q; q->pNext = r;
2. q->pNext = p->pNext; p->pNext = q;
|
r = p->pNext; p->pNext = p->pNext->pNext; free(r);
|
程序
#include <stdio.h> #include <malloc.h> #include <stdlib.h>
typedef struct Node { int data; struct Node * pNext; }NODE, *PNODE;
PNODE Create_List(void); void Traverse_List(PNODE pHead); bool Is_Empty(PNODE pHead); int Length_List(PNODE pHead); bool Insert_List(PNODE pHead, int pos, int val); bool Delete_List(PNODE pHead, int pos, int * pVal); void Sort_List(PNODE); void Sort_List_Bubble(PNODE);
int main(void) { int val; PNODE pHead = NULL; pHead = Create_List(); Traverse_List(pHead);
if( Delete_List(pHead, 4, &val) ) { printf("删除成功, 删除的元素为:%d\n", val); } else printf("删除失败\n");
Sort_List_Bubble(pHead); Traverse_List(pHead);
PNODE p = pHead->pNext; while (p != NULL) { PNODE temp = p; p = p->pNext; free(temp); } free(pHead); return 0; }
PNODE Create_List(void) { int len; int val; PNODE pHead = (PNODE)malloc(sizeof(NODE)); if(NULL == pHead) { printf("内存分配失败!\n"); exit(-1); } PNODE pTail = pHead; pTail->pNext = NULL; printf("请输入需要生成的链表节点个数:len = "); scanf("%d", &len); for(int i=0;i<len;i++) { printf("请输入第%d个节点的值:", i+1); scanf("%d", &val); PNODE pNew = (PNODE)malloc(sizeof(Node)); if(NULL == pNew) { printf("内存分配失败!\n"); exit(-1); } pNew->data = val; pTail->pNext = pNew; pNew->pNext = NULL; pTail = pNew;
} return pHead; }
void Traverse_List(PNODE pHead) { PNODE p = pHead->pNext; while(NULL != p) { printf("%d ", p->data); p = p->pNext; } printf("\n"); return; }
bool Is_Empty(PNODE pHead) { if(pHead->pNext == NULL) { return true; } else return false; }
int Length_List(PNODE pHead) { PNODE p = pHead->pNext; int len = 0; while(p != NULL) { len++; p = p->pNext; } return len; }
void Sort_List(PNODE pHead) { int i, j, t; int len = Length_List(pHead); PNODE p, q; for(i=0, p=pHead->pNext;i<len-1;i++, p=p->pNext) { for(j=i+1, q=p->pNext;j<len;j++, q=q->pNext) { if(p->data > q->data) { t = p->data; p->data = q->data; q->data = t; } } } return; }
void Sort_List_Bubble(PNODE pHead) { int i, j, t; int len = Length_List(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) { t = q->data; q->data = q->pNext->data; q->pNext->data = t; } } } }
bool Insert_List(PNODE pHead, int pos, int val) { int i = 0; PNODE p = pHead; while( NULL!=p && i<pos-1) { p = p->pNext; i++; } if(i>pos-1 || NULL==p) { return false; } PNODE pNew = (PNODE)malloc(sizeof(NODE)); if(NULL == pNew) { printf("动态内存分配失败!\n"); exit(-1); } pNew->data = val; PNODE q = p->pNext; p->pNext = pNew; pNew->pNext = q; return true; }
bool Delete_List(PNODE pHead, int pos, int * pVal) { int i = 0; PNODE p = pHead; while( NULL!=p && i<pos-1) { p = p->pNext; i++; } if(i>pos-1 || NULL==p->pNext) { return false; } PNODE q = p->pNext; *pVal = q->data; p->pNext = p->pNext->pNext; free(q); q = NULL; return true; }
|