日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

数据结构相关C语言代码

發(fā)布時(shí)間:2025/3/21 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构相关C语言代码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

順序存儲(chǔ)結(jié)構(gòu)

#include<stdio.h> #include<stdlib.h>typedef struct Arr {int *pBase;//第一個(gè)下標(biāo)的地址 (類似于數(shù)組名) int lenth;//總長(zhǎng)度(最多可存儲(chǔ)數(shù)據(jù)) int cnt;//實(shí)際數(shù)據(jù)長(zhǎng)度 }*PARR,ARR;void init(PARR pArr,int len);//初始化數(shù)組 void show_arr(PARR pArr);//輸出數(shù)組 bool is_empty(PARR pArr);//判斷數(shù)組是否為空 bool is_full(PARR pArr);// 判斷數(shù)組是否已滿 bool append_arr(PARR pArr,int val);//在數(shù)組尾部增添數(shù)據(jù)val bool insert_arr(PARR pArr,int pos,int val);//在數(shù)組第pos個(gè)位置前插入數(shù)據(jù)val bool delete_arr(PARR pArr,int pos,int *pVal);//刪除鏈表中第pos位置的數(shù)據(jù),并把刪除的數(shù)據(jù)存入*pVal中; void inversion_arr(PARR pArr);//將數(shù)組逆序 void sort_arr(PARR pArr);//將數(shù)組內(nèi)元素排序 int main() {ARR arr;int pos1,val1,pos2,dnum;init(&arr,6);printf("初始化后");show_arr(&arr);for(int i=1;i<=5;i++)append_arr(&arr,i);printf("\n增添后的");show_arr(&arr);printf("\n請(qǐng)輸入插入的位置:");scanf("%d",&pos1);printf("請(qǐng)輸入插入的數(shù)據(jù):");scanf("%d",&val1);if(insert_arr(&arr,pos1,val1)){printf("插入后的");show_arr(&arr);}else{printf("插入失敗");}printf("\n請(qǐng)輸入刪除的位置:");scanf("%d",&pos2);if(delete_arr(&arr,pos2,&dnum)){printf("刪除的數(shù)據(jù)為:%d",dnum);printf("\n刪除后的");show_arr(&arr);} else{printf("刪除失敗");}inversion_arr(&arr);printf("\n逆序后的");show_arr(&arr);sort_arr(&arr);printf("\n排序后的");show_arr(&arr);return 0; } void init(PARR pArr,int len)//pArr只是定義的結(jié)構(gòu)體指針,通過它可以調(diào)用結(jié)構(gòu)體中的數(shù)據(jù) {pArr->pBase=(int *)malloc(sizeof(int)*len);//分配大小為len的空間為數(shù)組空間,此時(shí)pBase可當(dāng)數(shù)組用 if(pArr->pBase==NULL){printf("內(nèi)存分配失敗!!");exit(-1);}else{pArr->lenth=len; pArr->cnt=0;}return ; } void show_arr(PARR pArr) {if(is_empty(pArr))printf("順序鏈表為空!!");else{printf("順序鏈表為:");for(int i=0;i<pArr->cnt;i++)printf("%d ",pArr->pBase[i]);//輸出pArr指向的結(jié)構(gòu)體中的pBase數(shù)組 }return ; } bool is_empty(PARR pArr) {if(pArr->cnt==0)//實(shí)際長(zhǎng)度為0才為空 return true;elsereturn false; } bool is_full(PARR pArr) {if(pArr->cnt==pArr->lenth)//實(shí)際長(zhǎng)度和最大長(zhǎng)度相同是該數(shù)組已滿 return true;elsereturn false; } bool append_arr(PARR pArr,int val) {if(is_full(pArr))//若數(shù)組已滿則無(wú)法再在后面容納數(shù)據(jù) return false;pArr->pBase[pArr->cnt]=val;//注意:第cnt個(gè)數(shù)據(jù)的數(shù)組下標(biāo)為cnt-1 pArr->cnt++;//實(shí)際長(zhǎng)度++ return true; } bool insert_arr(PARR pArr,int pos,int val) {if(is_full(pArr))//若數(shù)組已滿則無(wú)法再有插入內(nèi)存 return false;if(pos<1||pos>pArr->cnt+1)//需要保證插入的位置存在,不能在小于實(shí)際長(zhǎng)度cnt的位置插入,但是可以在最后一個(gè)數(shù)據(jù)cnt-1后的cnt插入,大于cnt+1的位置也不行,否則中間會(huì)存在空位 return false;for(int i=pArr->cnt-1;i>=pos-1;i--)//注意:第cnt個(gè)數(shù)據(jù)的數(shù)組下標(biāo)為cnt-1,第pos個(gè)數(shù)據(jù)的數(shù)組下標(biāo)為pos-1,若數(shù)組不為空則pos和cnt都不會(huì)為0pArr->pBase[i+1]=pArr->pBase[i];//通過循環(huán)實(shí)現(xiàn)第pos及以后的數(shù)據(jù)后移一位; pArr->pBase[pos-1]=val;//第pos個(gè)位置插入了數(shù)據(jù)val pArr->cnt++;//插入后長(zhǎng)度++ return true; } bool delete_arr(PARR pArr,int pos,int *pVal) {if(is_empty(pArr))return false;if(pos<1||pos>pArr->cnt)//刪除的位置不能不存在 return false;*pVal=pArr->pBase[pos-1]; //將刪除的數(shù)據(jù)進(jìn)行存儲(chǔ); for(int i=pos;i<pArr->cnt;i++)pArr->pBase[i-1]=pArr->pBase[i];//讓刪除位置的后面的數(shù)據(jù)覆蓋該刪除數(shù)據(jù),后面數(shù)據(jù)并整體前移 pArr->cnt--;//順序鏈表長(zhǎng)度-- return true; } void inversion_arr(PARR pArr) {int i=0;//i為第一個(gè)元素下標(biāo) int j=pArr->cnt-1;//j為最后一個(gè)元素下標(biāo) int t;while(i<j)//頭和尾依次逐漸交換,直到i,j相遇 {t=pArr->pBase[i];pArr->pBase[i]=pArr->pBase[j];pArr->pBase[j]=t;i++;j--; }return ; } void sort_arr(PARR pArr) {int i,j,t;for(i=0;i<pArr->cnt-1;i++){for(j=i+1;j<pArr->cnt;j++){if(pArr->pBase[i]>pArr->pBase[j]){t=pArr->pBase[i];pArr->pBase[i]=pArr->pBase[j];pArr->pBase[j]=t;}}}return ; }

鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu):

#include<stdio.h> #include<stdlib.h>typedef struct Node {int data;struct Node *pNext; } *PNODE;PNODE creat_list(void);//創(chuàng)建鏈表 void traverse(PNODE pHead);//輸出鏈表 bool is_empty(PNODE pHead);//判斷該鏈表是否為空表 int lenth_list(PNODE pHead);//求該鏈表的長(zhǎng)度 void sort_list(PNODE pHead);//給鏈表順序排序 bool insert_list(PNODE pHead,int pos,int val);//從鏈表的第pos個(gè)位置插入數(shù)據(jù)val bool delete_list(PNODE pHead,int pos,int *pVal);//刪除鏈表的第pos位置的數(shù)據(jù)并將數(shù)據(jù)存入指針pVal中;int main() {int pos,val,pos2,val2; PNODE pHead=NULL;pHead=creat_list();//創(chuàng)建鏈表 if(is_empty(pHead)==false)//判斷鏈表是否為空 {printf("\n該鏈表為空!!!");}else{traverse(pHead);//輸出鏈表 printf("\n該鏈表為非空!!!"); printf("\n鏈表長(zhǎng)度為%d",lenth_list(pHead));//計(jì)算長(zhǎng)度 sort_list(pHead);//排序 printf("\n排序后");traverse(pHead);printf("\n請(qǐng)輸入您要插入的位置:");//插入 scanf("%d",&pos);if(pos>lenth_list(pHead)+1){printf("無(wú)法插入");}else{printf("請(qǐng)輸入您要插入的數(shù)據(jù)為:");scanf("%d",&val);insert_list(pHead,pos,val);printf("插入后");traverse(pHead);}printf("\n請(qǐng)輸入您要?jiǎng)h除的節(jié)點(diǎn)位置:");scanf("%d",&pos2);if(delete_list(pHead,pos2,&val2)){printf("刪除成功,您刪除的節(jié)點(diǎn)數(shù)據(jù)為%d",val2);printf("\n刪除后"); traverse(pHead);}else{printf("刪除失敗,該節(jié)點(diǎn)不存在");}} return 0; } PNODE creat_list(void) {int len;int val;int i;printf("請(qǐng)輸入您創(chuàng)造的鏈表長(zhǎng)度:");scanf("%d",&len);PNODE pHead=(PNODE)malloc(sizeof(Node));if(pHead==NULL){printf("錯(cuò)誤!!!");exit(-1);}PNODE pTail=pHead;pTail->pNext=NULL;for(i=0;i<len;i++){printf("請(qǐng)輸入第%d個(gè)數(shù)據(jù):",i+1);scanf("%d",&val);PNODE pNew=(PNODE)malloc(sizeof(Node));if(pNew==NULL){printf("錯(cuò)誤!!!");exit(-1); }pNew->data=val;pTail->pNext=pNew;pNew->pNext=NULL;pTail=pNew;}/*pTail->pNext=NULL;*/return pHead; } void traverse(PNODE pHead) {PNODE p=pHead->pNext;printf("該鏈表為:"); while(p!=NULL){printf("%d ",p->data);p=p->pNext;}return ; } bool is_empty(PNODE pHead) {if(pHead->pNext==NULL)return false;elsereturn true; } int lenth_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,len=lenth_list(pHead);PNODE q,p;for(i=0,q=pHead->pNext;i<len-1;i++,q=q->pNext){for(j=i+1,p=q->pNext;j<len;j++,p=p->pNext){if(q->data>p->data){t=q->data;q->data=p->data;p->data=t;}}} } bool insert_list(PNODE pHead,int pos,int val) {int i=1;PNODE p=pHead;while(p!=NULL&&i<pos)//p是插入位置前的那一個(gè)整體數(shù)據(jù) {p=p->pNext;i++;}if(p==NULL||i>pos)return false;PNODE pNew=(PNODE)malloc(sizeof(Node));if(pNew==NULL){printf("錯(cuò)誤!!!");exit(-1);}pNew->data=val;pNew->pNext=p->pNext;p->pNext=pNew;return true; } bool delete_list(PNODE pHead,int pos,int *pVal) {int i=1;PNODE p=pHead;while(p->pNext!=NULL&&i<pos){p=p->pNext;i++;}if(p->pNext==NULL||i>pos)return false;PNODE q=p->pNext;*pVal=q->data;p->pNext=q->pNext;free(q);return true; }

鏈?zhǔn)綏?#xff1a;

#include<stdio.h> #include<stdlib.h>typedef struct Node//節(jié)點(diǎn)結(jié)構(gòu) {int data;struct Node *pNext; }*PNODE;typedef struct Stack//棧的頭節(jié)點(diǎn)和尾節(jié)點(diǎn) {PNODE pTop;PNODE pBottom; }STACK,*PSTACK;void init(PSTACK pS);//初始化 void push(PSTACK pS,int val);//壓棧 bool pop(PSTACK pS,int *pVal);//出棧 void traverse(PSTACK pS);//輸出棧 bool is_empty(PSTACK pS);//判斷棧是否為空 void clear_stack(PSTACK pS);//清空棧內(nèi)數(shù)據(jù) int main() {int i,val;STACK S;init(&S);for(i=10;i>=0;i--)push(&S,i);traverse(&S);pop(&S,&val);printf("\n刪除的頂部數(shù)據(jù)為:%d\n",val);traverse(&S);clear_stack(&S);printf("\n棧清空后"); traverse(&S);return 0; } void init(PSTACK pS) {pS->pTop=(PNODE)malloc(sizeof(Node));if(pS->pTop==NULL){printf("分配失敗!!!");exit(-1);}else{pS->pBottom=pS->pTop;pS->pTop->pNext=NULL;} } void push(PSTACK pS,int val) {PNODE pNew=(PNODE)malloc(sizeof(Node));pNew->data=val;pNew->pNext=pS->pTop;pS->pTop=pNew; } bool is_empty(PSTACK pS) {if(pS->pTop==pS->pBottom)return true;elsereturn false; } bool pop(PSTACK pS,int *pVal) {if(is_empty(pS))//如果為真 return false;else{PNODE p=pS->pTop;*pVal=p->data;pS->pTop=p->pNext;free(p);p=NULL;return true;} } void traverse(PSTACK pS) {PNODE p=pS->pTop;printf("該棧為:");while(p!=pS->pBottom){printf("%d ",p->data);p=p->pNext;} } void clear_stack(PSTACK pS) {if(is_empty(pS))return ;else{PNODE p=pS->pTop;PNODE q=NULL;while(p!=pS->pBottom){q=p->pNext;free(p);p=q;}}pS->pTop=pS->pBottom; }

循環(huán)隊(duì)列

#include<stdio.h> #include<stdlib.h> # define size 6typedef struct Queue {int *pBase;int front;int rear; }QUEUE,*PQUEUE;void init(PQUEUE Q); void traverse_queue(PQUEUE Q); bool is_empty(PQUEUE Q); bool is_full(PQUEUE Q); bool EnQueue(PQUEUE Q,int val); bool DeQueue(PQUEUE Q,int *pVal);int main() {QUEUE Q;int i,val1,val2;init(&Q);for(i=0;i<4;i++)EnQueue(&Q,i);traverse_queue(&Q);printf("\n請(qǐng)輸入入隊(duì)的數(shù)據(jù)為:");scanf("%d",&val1);EnQueue(&Q,val1);printf("插入后");traverse_queue(&Q);DeQueue(&Q,&val2);printf("\n出隊(duì)的數(shù)據(jù)為:%d",val2);printf("\n刪除后");traverse_queue(&Q);return 0; } void init(PQUEUE Q) {Q->pBase=(int*)malloc(sizeof(int)*size);if(!Q->pBase){printf("內(nèi)存分配失敗!!");exit(-1);}Q->front=0;Q->rear=0;return ; } void traverse_queue(PQUEUE Q) {int p=Q->front;printf("該隊(duì)列為:");while(p!=Q->rear){printf("%d ",Q->pBase[p]);p=(p+1)%size;}return ; } bool is_empty(PQUEUE Q) {if(Q->front==Q->rear)return true;elsereturn false; } bool is_full(PQUEUE Q) {if((Q->rear+1)%size==Q->front)return true;elsereturn false; } bool EnQueue(PQUEUE Q,int val)//入隊(duì),隊(duì)尾后移一位存入新數(shù)據(jù) {if(is_full(Q))return false;else{Q->pBase[Q->rear]=val;Q->rear=(Q->rear+1)%size;return true;} } bool DeQueue(PQUEUE Q,int *pVal)//出隊(duì),隊(duì)首后移一位,注意移動(dòng)方向與隊(duì)尾相同 {if(is_empty(Q))return false;else{*pVal=Q->pBase[Q->front];Q->front=(Q->front+1)%size;return true;} }

二叉鏈表(二叉樹)

//測(cè)試樣例:ABC##DE#G##F### #include<stdio.h> #include<stdlib.h>typedef struct BtNode {char data;struct BtNode *lchild;struct BtNode *rchild; }BiTNode,*BiTree;void creat_btree(BiTree *T);//創(chuàng)建樹 void Midtraverse_btree(BiTree T);//中序遍歷輸出樹 void copy(BiTree T,BiTree *NewT);//復(fù)制樹 int depth(BiTree T);//計(jì)算樹的深度 int NodeCount(BiTree T);//計(jì)算樹的節(jié)點(diǎn)數(shù) int LeafCount(BiTree T);//計(jì)算葉子節(jié)點(diǎn)的個(gè)數(shù) int main() {BiTree T=NULL;BiTree NewT=NULL;creat_btree(&T);printf("中序輸出該樹為:");Midtraverse_btree(T);copy(T,&NewT);printf("\n復(fù)制后中序輸出該樹為:"); Midtraverse_btree(NewT);printf("\n該樹深度為:%d",depth(T));printf("\n該樹的節(jié)點(diǎn)數(shù)為:%d",NodeCount(T));printf("\n該樹的葉子節(jié)點(diǎn)數(shù)為:%d",LeafCount(T));return 0; } void creat_btree(BiTree *T) {char ch;scanf("%c",&ch);if(ch=='#')*T=NULL;else{*T=(BiTree)malloc(sizeof(BiTNode));(*T)->data=ch;creat_btree(&(*T)->lchild);creat_btree(&(*T)->rchild);}return ; } void Midtraverse_btree(BiTree T) {if(T){if(T->lchild)Midtraverse_btree(T->lchild);printf("%c ",T->data);if(T->rchild)Midtraverse_btree(T->rchild);}return ; } void copy(BiTree T,BiTree *NewT)//修改節(jié)點(diǎn)所指向的內(nèi)存時(shí)一定要記住用二級(jí)指針 {if(!T)*NewT=NULL;else{*NewT=(BiTree)malloc(sizeof(BiTNode));(*NewT)->data=T->data;copy(T->lchild,&(*NewT)->lchild);copy(T->rchild,&(*NewT)->rchild);}return ; } int depth(BiTree T) {if(!T)return 0;else{int m=0,n=0;m=depth(T->lchild);n=depth(T->rchild);if(m>n)return m+1;elsereturn n+1;} } int NodeCount(BiTree T) {if(!T)return 0;elsereturn NodeCount(T->lchild)+NodeCount(T->rchild)+1; } int LeafCount(BiTree T) {if(!T)return 0;if(!T->lchild&&!T->rchild)return 1;elsereturn LeafCount(T->lchild)+LeafCount(T->rchild); }

鄰接矩陣

#include<iostream> #include<stdlib.h> #define MVnum 100 #define MaxInt 0 using namespace std;typedef struct Graph {char vexs[MVnum];int arcs[MVnum][MVnum];int vexnum,arcnum; } AMGraph;void creatUDN(AMGraph &G); void traverse_graph(AMGraph G); int LocateVex(AMGraph G,char ch);int main() {AMGraph G;creatUDN(G);traverse_graph(G);return 0; } void creatUDN(AMGraph &G) {int i,j,k;cout<<"請(qǐng)分別輸入頂點(diǎn)數(shù)和邊數(shù):";cin>>G.vexnum>>G.arcnum;for(i=0;i<G.vexnum;i++){printf("\n請(qǐng)輸入第%d個(gè)頂點(diǎn):",i+1);cin>>G.vexs[i];}for(i=0;i<G.vexnum;i++){for(j=0;j<G.vexnum;j++)G.arcs[i][j]=MaxInt;}for(k=0;k<G.arcnum;k++){char v1,v2;int m,n,w;printf("\n請(qǐng)輸入第%d條邊的兩個(gè)頂點(diǎn)和權(quán)值:",k+1);cin>>v1>>v2>>w;m=LocateVex(G,v1);n=LocateVex(G,v2);G.arcs[m][n]=w;G.arcs[n][m]=G.arcs[m][n];}return ; } void traverse_graph(AMGraph G) {int i,j;for(i=0;i<G.vexnum;i++){for(j=0;j<G.vexnum;j++)printf("%d ",G.arcs[i][j]);cout<<endl;}return ; } int LocateVex(AMGraph G,char ch) {int i;for(i=0;i<G.vexnum;i++){if(G.vexs[i]==ch)return i;}return -1; }

鄰接表

#include<iostream> #include<stdlib.h> using namespace std;#define MVnum 100typedef struct Arcnode {int adjvex;struct Arcnode *nextarc;int info; }ArcNode; typedef struct Vnode {char data;ArcNode *firstarc; }VNode,AdjList[MVnum]; typedef struct Graph {AdjList vertices;int vexnum,arcnum; }ALGraph;void creatUDG(ALGraph &G); void traverse(ALGraph G); int LocateVex(ALGraph G,char ch);int main() {ALGraph G;creatUDG(G);traverse(G);return 0; } void creatUDG(ALGraph &G) {int i,j,k;ArcNode *p1,*p2;char v1,v2;printf("請(qǐng)輸入頂點(diǎn)數(shù)和邊數(shù):");cin>>G.vexnum>>G.arcnum;for(i=0;i<G.vexnum;i++){printf("\n請(qǐng)輸入第%d個(gè)頂點(diǎn):",i+1);cin>>G.vertices[i].data;G.vertices[i].firstarc=NULL;}for(k=0;k<G.arcnum;k++){printf("\n請(qǐng)輸入第%d條邊的兩個(gè)頂點(diǎn):",k+1);cin>>v1>>v2;i=LocateVex(G,v1);j=LocateVex(G,v2);p1=new ArcNode;p1->adjvex=j;p1->nextarc=G.vertices[i].firstarc;G.vertices[i].firstarc=p1;p2=new ArcNode;p2->adjvex=i;p2->nextarc=G.vertices[j].firstarc;G.vertices[j].firstarc=p2;}return ; } void traverse(ALGraph G) {int i;printf("鄰接表為:\n");for(i=0;i<G.vexnum;i++){char q=G.vertices[i].data;ArcNode *p=G.vertices[i].firstarc;printf("%c ",q);while(p){printf("%d ",p->adjvex);p=p->nextarc;}printf("\n");}return ; } int LocateVex(ALGraph G,char ch) {int i;for(i=0;i<G.vexnum;i++){if(G.vertices[i].data==ch)return i;}return -1; }

這些只是部分主要基本函數(shù),數(shù)據(jù)類型部分用的整型,可供參考(若有問題和錯(cuò)誤請(qǐng)及時(shí)提出!!!

總結(jié)

以上是生活随笔為你收集整理的数据结构相关C语言代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。