單鏈表代碼(指針實現)


#include <stdlib.h> #include <stdio.h> #include <time.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2 typedef int ElemType; typedef int Status; typedef struct Node{ ElemType data; struct Node * next; } Node; typedef struct Node* Link;    //定義鏈表指針結構類型 //Link L 表示聲明一個單鏈表結構體的指針 //Link *L 表示聲明一個存放單鏈表結構體指針的指針
//頭結點Link的存在是因為方便同一管理和操作數據。

/***********************
    頭插法創表
輸入:鏈表指針,初始化個數
輸出:狀態碼
功能:頭插法創建鏈表
************************/
Status createHead(Link *L,int num){ srand(time(0)); Link link = *L; Link new; link->data = 0; link->next = NULL; int i; for(i=0;i<num;i++){ new = (Link)malloc(sizeof(Node)); new->data = rand()%100+1; new->next = link->next; link->next = new; } return OK; }

/***********************
     尾插法創表
輸入:鏈表指針,初始化個數
輸出:狀態碼
功能:尾插法創建鏈表
************************/
Status createTail(Link * L,int num){ Link tmp,new,link; srand(time(0)); link = *L; link->data=0; link->next=NULL; tmp = link; int i; for(i=0;i<num;i++){ new = (Link)malloc(sizeof(Node)); new->data = rand()%100+1; new->next = tmp->next; tmp->next = new; tmp=tmp->next; } return OK; }
/***********************
     插入節點
輸入:鏈表指針,插入位置,插入值
輸出:狀態碼
功能:在指定的位置上插入指定的值
************************/
Status insertLink(Link * L,int index,ElemType e){ index--; Link new,tmp = *L; int i=0; while( tmp && i<index ){ tmp=tmp->next; i++; } if( !tmp || i>index ){ printf("overflow\n"); return OVERFLOW; } new=(Link)malloc(sizeof(Node)); new->data = e; new->next = tmp->next; tmp->next = new; return OK; }
/***********************
     刪除節點
輸入:鏈表指針,刪除位置
輸出:狀態碼
功能:在指定的位置上刪除指定的值
************************/

Status deleteLink(Link *L,int index,ElemType *e){ index--; Link net,tmp = *L; int i=0; while( tmp && i<index){ tmp=tmp->next; i++; } if( !tmp || i>index){ printf("overflow\n"); return OVERFLOW; } net = tmp->next; tmp->next = net->next; free(net); return OK; }
/***********************
     讀取數據
輸入:鏈表指針,讀取位置,存儲指針
輸出:狀態碼
功能:獲取指定位置的值賦予給存儲指針
************************/
Status getValue(Link L,int index,ElemType* e){ int i=0; index--; L=L->next; //ignore head node while(L && i<index){ L=L->next; i++; } if(!L || i>index){ printf("index is valid\n"); return ERROR; } *e = L->data; return OK; }
/***********************
     釋放鏈表
輸入:鏈表指針
輸出:狀態碼
功能:將鏈表的空間全部釋放
************************/
Status freeLink(Link*L){ Link p,q; p = (*L)->next; while(p){ q=p->next; free(p); p=q; } (*L)->next = NULL; return OK; } /***********************
     打印鏈表
輸入:鏈表指針
輸出:狀態碼
功能:將鏈表的值一一打印出來
************************/
Status printLink(Link L){ Link p = L; if(p->next==NULL){ printf("link is empty\n"); return OK; } int i = 1; while(p->next){ p=p->next; //忽略頭節點 printf("[%d] ",p->data); if(i%10==0)printf("\n"); i++; } printf("\n"); return OK; }

/***********************
     計算個數
輸入:鏈表指針,目標值
輸出:(int)個數結果
功能:計算鏈表中指定的目標值的個數
************************/
int getCount(Link L,ElemType x){ int count = 0; while( L->next ){ L=L->next; if(L->data == x)count++; } return count; } int main(){ int index,value,num = 50; Link L = (Link)malloc(sizeof(Node)); createTail(&L,num); printLink(L); //insert printf("enter index:"); scanf("%d",&index); printf("enter value:"); scanf("%d",&value); insertLink(&L,index,value); printLink(L); //delete printf("enter index:"); scanf("%d",&index); deleteLink(&L,index,&value); printLink(L); //getvalue printf("enter index:"); scanf("%d",&index); getValue(L,index,&value); printf("value is %d\n",value); //getCount printf("enter value:"); scanf("%d",&value); int c = getCount(L,value); printf("count is %d\n",c); //freeLink freeLink(&L); return OK; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM