链表插入功能实现演示
生活随笔
收集整理的這篇文章主要介紹了
链表插入功能实现演示
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
#include <stdio.h> #include <malloc.h> #include <string.h> #include <stdlib.h>typedef struct Node {int data; //數(shù)據(jù)域struct Node * pNext; //指針域}Node, *pNode;//函數(shù)聲明 pNode create_list(); void traverse_list(pNode pHead); //遍歷bool is_empty(pNode pHead); //判斷鏈表是否為空 int length_list(pNode pHead); //鏈表的長度 bool insert_list(pNode, int, int); //插入 第一個參數(shù)表示插入的鏈表 第二個參數(shù)表示插入的位置 第三個參數(shù)表示插入的元素 bool delete_list(pNode, int, int *); //第一個參數(shù)表示要刪除的位置,第二個參數(shù)表示要刪除的位置 第三參數(shù)表示刪除的元素的地址放入指針 void sort_list(pNode pHead);int main(void) {pNode pHead = NULL; //等價于 struct Node *pHead=NULLpHead = create_list(); //create_list()創(chuàng)建一個非循環(huán)單鏈表,并將該鏈表的頭結點的地址賦給pHeadtraverse_list(pHead); //遍歷輸出/**int len = length_list(pHead);printf("鏈表的長度%d\n", len);sort_list(pHead); //選擇排序traverse_list(pHead); //遍歷輸出*/insert_list(pHead, 4, 33);traverse_list(pHead);//遍歷輸出while(true){}return 0; }//創(chuàng)建單鏈表 pNode create_list() {int len; //用來存放有效節(jié)點數(shù)int i;int val; //存放用戶臨時輸入的節(jié)點數(shù)據(jù)//我們首先要先生成一個頭結點 不存放有效數(shù)據(jù)pNode pHead = (pNode)malloc(sizeof(Node));if (NULL == pHead) {printf("內(nèi)存分配失敗");//程序退出exit(-1);}pNode pTail = pHead; //pTail也指向了頭結點pTail->pNext = NULL;printf("請輸入你要輸入節(jié)點的個數(shù) len =");scanf_s("%d", &len);//假設輸入的長度5,我們需要循環(huán)for ( i = 0; i < len; i++){printf("請輸入第%d個節(jié)點的值:", i + 1);scanf_s("%d", &val);pNode pNew=(pNode)malloc(sizeof(Node));if (NULL == pNew) {printf("內(nèi)存分配失敗");//程序退出exit(-1);}pNew->data = val;//pHead->pNext = pNew;//pNew->pNext = NULL;pTail->pNext = pNew; //將這個新節(jié)點掛到尾節(jié)點pNew->pNext = NULL;pTail = pNew;}return pHead; }//遍歷 void traverse_list(pNode pHead) {pNode p = pHead->pNext;while (p!=NULL){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; //第一個節(jié)點int len = 0;while (NULL != p) { //只要指針指向的下一個元素不是空,指針就繼續(xù)向后移動++len;p=p->pNext;}return len; }//排序算法 void sort_arr(pNode pHead) {int i, j, tmp;int arr[6] = { 5,8,45,2,9,3 };int len=length_list(pHead); //獲取鏈表長度pNode p, q;for (i = 0; i < len-1; i++){for (j = i+1; j < len; j++){}}}//選擇排序 從小到大排 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) { //類似于 數(shù)組中的: a[i] > a[j]t = p->data; //類似于數(shù)組中的: t = a[i];p->data = q->data; //類似于數(shù)組中的: a[i] = a[j];q->data = t; // 類似于數(shù)組中的: a[j] = t;}}} }//在pHead所指向鏈表的第pos個節(jié)點的前面插入-一個新的結點,該節(jié)點的值是val,并 且pos的值是從1開始 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("動態(tài)分配內(nèi)存失敗!");exit(-1);}pNew->data = val;//定義一個臨時節(jié)點,數(shù)據(jù)交換存儲pNode tmp = p->pNext;p->pNext=pNew;pNew->pNext = tmp;return true;}?
?
?
當你輸入數(shù)組的長度為2的時候是不是就不能再第四個位置插入?
?
總結
以上是生活随笔為你收集整理的链表插入功能实现演示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bit-Z收不到邮箱验证码怎么办(如何添
- 下一篇: Bit-Z生态联盟正式上线 开启全球加