单链表实现
程序员文章站
2023-03-26 17:00:53
单链表实现 #include #include #include #include typedef struct Node { int data; //数据域 struct Node * pNext; //指针域 }NO ......
单链表实现
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<stdbool.h> typedef struct node { int data; //数据域 struct node * pnext; //指针域 }node, *pnode; //node等价于struct node,pnode等价于struct node * //函数声明 pnode creat_list(); 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,int*); void sort_list(pnode phead); int main() { pnode phead = null; //等价于struct node * phead = null;头结点 int val; phead = creat_list(); traverse_list(phead); if( is_empty(phead) ) printf("链表为空\n"); else printf("链表不空\n"); int len = length_list(phead); printf("链表长度是%d\n",len); if( insert_list(phead,4,33) ) { printf("第四个节点插入数据成功!\n"); } else { printf("插入失败!\n"); } traverse_list(phead); if( delete_list(phead,4,&val) ) { printf("删除成功,您删除的元素是:%d\n",val); } else { printf("删除失败!您删除的元素不存在!\n"); } traverse_list(phead); sort_list(phead); printf("链表由小到大排序\n"); traverse_list(phead); return 0; } pnode creat_list() { int len; //用来存放有效节点的个数 int val; //用来临时存放用户输入节点的值 pnode phead = (pnode)malloc(sizeof(node)); if(null == phead) { printf("分配失败,程序终止"); 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("分配失败,程序终止"); exit(-1); } pnew->data = val; //尾插法 ptail->pnext = pnew; pnew->pnext = null; ptail = pnew; //ptail在不断的移动,相当于指向要插入节点上一个节点指针 } 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(null == phead->pnext) return true; else return false; } int length_list(pnode phead) { pnode p = phead->pnext; int len=0; while(null != p) { ++len; p = p->pnext; } return len; } void sort_list(pnode phead) //选择排序算法 { pnode p,q; int i,j,t; int len = length_list(phead); for(i=0,p=phead->pnext;i<len-1;++i,p=p->pnext) { for(j=0,q=p->pnext;j<len;++j,q=q->pnext) { if(p->data > q->data) //类似于数组中的:a[i]>a[j] { t = p->data;//类似于数组中的:t = a[i]; p->data = q->data;//类似于数组中的:a[i] = a[j]; q->data = t;//类似于数组中的:a[j] = t; } } } return; } 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->pnext && i<pos-1) { p = p->pnext; ++i; } if(i>pos-1 || null==p->pnext) return false; pnode q = p->pnext; //将要删除的节点赋值给临时指针q *pval = q->data; //删除p节点后面的节点 p->pnext = p->pnext->pnext; free(q); q = null; return true; }