链表的插入、删除、排序的程序
生活随笔
收集整理的這篇文章主要介紹了
链表的插入、删除、排序的程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{int data;struct node * pNext;
}*pNode,Node;
//#define bool int; //#define的意思是單純的替換,與別名沒有關系,而且C語言中沒有bool數據類型
typedef int bool; //typedef的意思就是別名,或者是聲明結構體數據類型的變量,int 用bool來替換
#define true 0
#define false 1
pNode create_list();
void traverse_list(const pNode);
bool is_empty(const pNode);
int length_list(const pNode);
bool insert_list(pNode, int, int);
bool delete_list(pNode, int);
void sort1_list(const pNode);
void sort2_list(const pNode);
int find_list(const pNode, int);
//void sort3_list(const pNode, int left, int right);
int main(void)
{pNode pHead=NULL;pHead=create_list();if(is_empty(pHead)){puts("鏈表不為空!");}elseputs("鏈表為空!");printf("鏈表的長度為:%d\n",length_list(pHead));puts("-----------------------------------鏈表值為--------------------------------");traverse_list(pHead);puts("-----------------------------------冒泡排序--------------------------------");sort1_list(pHead);traverse_list(pHead);puts("-----------------------------------普通排序--------------------------------");sort2_list(pHead);traverse_list(pHead);//puts("------------------------------------快速排序-----------------------------");//sort3_list(pHead,1,5);//traverse_list(pHead);puts("-----------------------------------插入鏈表--------------------------------");insert_list(pHead, 5, 77);traverse_list(pHead);puts("-----------------------------------刪除鏈表--------------------------------");delete_list(pHead,3);traverse_list(pHead);puts("-----------------------------------查找鏈表---------------------------------");find_list(pHead,49);return 0;
}
pNode create_list()
{int len,val,i; //特別注意:C語言中,變量的定義必須放在前面,否則編譯無法識別,會報錯。pNode pTail;pNode pHead=(pNode)malloc(sizeof(Node));if(pHead==NULL){printf("動態內存分配失敗!\n");exit(-1);}pTail=pHead;pTail->pNext=NULL;printf("請輸入有效節點個數:len=");scanf("%d",&len);for(i=0;i<len;++i){pNode pNew=(pNode)malloc(sizeof(Node));if(pNew==NULL){printf("動態內存分配失敗!");exit(-1);}printf("請輸入數第%d個據域的值val=",i+1);scanf("%d",&val);pNew->data=val;pTail->pNext=pNew;pTail=pNew;pTail->pNext=NULL;}return pHead;
}
void traverse_list(const pNode pHead){pNode p=pHead->pNext;while(p!=NULL){printf("%d\t",p->data);p=p->pNext;}putchar('\n');return;
}
bool is_empty(const pNode pHead)
{if(pHead->pNext==NULL)return true;elsereturn false;
}
int length_list(const pNode pHead)
{int count=0;pNode p=pHead;while(p!=NULL){count++;p=p->pNext;}return count;
}
bool insert_list(pNode pHead, int pos, int val) //位置從有效節點1開始
{int count=length_list(pHead);int i=1;pNode p=pHead;pNode q=(pNode)malloc(sizeof(Node));if(q==NULL){puts("內存分配失敗!");exit(-1);}q->data=val;if(pos<2 || pos>count+1){puts("鏈表當中不存在該位置!");return false;}while(i<pos-1){++i;p=p->pNext;}q->pNext=p->pNext;p->pNext=q;return true;
}
bool delete_list(pNode pHead, int pos)
{int count=length_list(pHead);int i=1;pNode p=pHead->pNext;pNode q;if(pos<2 || pos>count){puts("鏈表當中不存在該位置!");return false;}while(i<pos-1){++i;p=p->pNext;}printf("刪除的值是:%d\n",p->pNext->data);q=p->pNext;p->pNext=q->pNext;free(q);//p->pNext=p->pNext->pNext;//free(p->pNext);//不能這么釋放,這是錯誤的return true;
}
//冒泡排序
void sort1_list(const pNode pHead)
{int count=length_list(pHead);pNode p;int i,j;/*這是定義變量,不叫逗號表達式。需要是表達式才行。逗號表達式只取最后的那個表達式的值作為整個表達式的值,但是前邊的表達式也要運行,左結合性,從左往右運行的。這個鏈表的for循環一定要用i,j;否則上限沒法表示。*/for(i=1,p=pHead->pNext;i<count-1;++i,p=p->pNext)for(j=1,p=pHead->pNext;j<count-i;++j,p=p->pNext){if(p->data > p->pNext->data){int temp;temp=p->data;p->data=p->pNext->data;p->pNext->data=temp;}}return;
}
//普通排序,注意第三方的使用非常重要
void sort2_list(const pNode pHead)
{int count=length_list(pHead);pNode p,q;int i,j;for(i=1,p=pHead->pNext;i<count-1;++i,p=p->pNext)//第一個for循環表示比較多少次for(j=i+1,q=p->pNext;j<count;++j,q=q->pNext)//第二個for循環表示和哪個對象比較{if(p->data > q->data){int temp;temp=p->data;p->data=q->data;q->data=temp;}}return;
}
int find_list(const pNode pHead, int val)
{pNode p=pHead->pNext;int count=length_list(pHead);int flag=1,flag2=0;while(p !=NULL ){if(p->data==val){printf("你要查找的數的位置:%d\n",flag);flag2++;}p=p->pNext;++flag;}if(flag2==0)puts("鏈表中沒有查找的值!");return flag;}
//快速排序
/*
void sort3_list(const pNode pHead, int left, int right)
{pNode p=pHead->pNext;pNode q=pHead->pNext;pNode r=NULL;int i=left;int j=right;int key=p->data;int count=length_list(pHead);while(q != NULL)q=q->pNext;if(i>=j)exit(-1);while(i<j){//for(i=left,p=pHead->pNext;i<j,q->data<key;--j)while(i<j && q->data<key ){--j;r->pNext=q;q=r;r=NULL;break;}p->data=q->data;//a[i]=a[j];while(i<j && p->data>key ){++i;p=p->pNext;break;}q->data=p->data;}p->data=key;sort3_list(pHead, left, i-1);sort3_list(pHead, j+1, right);
}
*/
轉載于:https://www.cnblogs.com/fengkui/p/6117932.html
總結
以上是生活随笔為你收集整理的链表的插入、删除、排序的程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Objective-C之MRC、ARC模
- 下一篇: JCam2 v1.6.0 USB摄像头工