創(chuàng)建鏈表函數(shù)c語言 c語言創(chuàng)建鏈表每一步詳解

c語言用函數(shù)創(chuàng)建單鏈表

#includestdio.h

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、源匯ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的源匯網(wǎng)站制作公司

#includestdlib.h

//鏈表定義

typedef int ElemType;

typedef struct LNode

{

int data;

struct LNode *next;

}LNode,*LinkList;

/*************************************

* 鏈表函數(shù) *

*************************************/

//鏈表初始化

void InitLink(LinkList L);

//創(chuàng)建函數(shù),尾插法

void CreateLink_T(LinkList L,int n);

//創(chuàng)建函數(shù),頭插法

void CreateLink_H(LinkList L,int n);

//銷毀函數(shù)

void DestroyLink(LinkList L);

//判斷是否為空函數(shù)

bool EmptyLink(LinkList L);

//獲取函數(shù)

bool GetLink(LinkList L,int i,int e);

//插入函數(shù)

void InsertLink(LinkList L,int i,int e);

//刪除函數(shù)

void DeleteLink(LinkList L,int i,int e);

//遍歷函數(shù)

void TraverseLink(LinkList L);

//鏈表長(zhǎng)度函數(shù)

int LengthLink(LinkList L);

//合并函數(shù)

void MergeLink(LinkList L1,LinkList L2);

void main()

{

LinkList L1,L2;

InitLink(L1);

InitLink(L2);

CreateLink_H(L1,2);

CreateLink_T(L2,2);

TraverseLink(L1);

printf("\n");

TraverseLink(L2);

printf("\n");

MergeLink(L1,L2);

TraverseLink(L1);

TraverseLink(L2);

}

//創(chuàng)建函數(shù),尾插法

void InitLink(LinkList L)

{

L=(LinkList)malloc(sizeof(LNode));

if (!L)

{

printf("Init error\n");

return;

}

L-next=NULL;

}

void CreateLink_T(LinkList L,int n)

{

if(n1)

{

printf("n must =1\n");

return ;

}

else

{

// L=(LinkList)malloc(sizeof(LNode));

L-next=NULL;

for(int i=0;in;i++)

{

LinkList p=(LinkList)malloc(sizeof(LNode));// the lower letter p

printf("enter the data :\t");

scanf("%d",(p-data));

p-next=L-next;

L-next=p;

}

}

}

//創(chuàng)建函數(shù),頭插法

void CreateLink_H(LinkList L,int n)

{

if (n1)

{

printf("n must =1\n ");

return;

}

else

{

//L=(LinkList)malloc(sizeof(LNode));

LinkList pre=(LinkList)malloc(sizeof(LNode));

L-next=NULL;

pre=L;

for(int i=0;in;i++)

{

LinkList p=(LinkList)malloc(sizeof(LNode));

printf("enter the data:\t");

scanf("%d",(p-data));

pre-next=p;

pre=p;

}

pre-next=NULL;

}

}

//銷毀函數(shù)

void DestroyLink(LinkList L)

{

LinkList q=L,p=L;

while (p)

{

q=p;

p=p-next;

free(q);

}

L-next=NULL;

}

//判斷是否為空函數(shù)

bool EmptyLink(LinkList L)

{

if (NULL==L-next)

{

return true;

}

else

{

return false;

}

}

//獲取函數(shù)

bool GetLink(LinkList L,int i,int e)

{

if (i1)

{

return false;

}

else

{

if (EmptyLink(L))

{

return false;

}

LinkList p=L-next;

int j=1;

while(pji)

{

p=p-next;

j++;

}

if (!p||ji)

{

return false;

}

else

{

e=p-data;

return true;

}

}

}

//插入函數(shù)

void InsertLink(LinkList L,int i,int e)

{

if (i0||iLengthLink(L))

{

printf("Insert error\n");

return;

}

else

{

LinkList p=L;

int j=0;

while(p(ji))

{

p=p-next;

j++;

}

if (!p||ji)

{

printf("Insert error\n");

return;

}

else

{

LinkList q=(LinkList)malloc(sizeof(LNode));

q-data=e;

q-next=p-next;

p-next=q;

}

}

}

//刪除函數(shù)

void DeleteLink(LinkList L,int i,int e)

{

if(i=0||iLengthLink(L))

{

printf("delete error\n");

return;

}

else

{

LinkList p=L;

int j=0;

while(pji-1)

{

p=p-next;

j++;

}

if(!p||ji)

{

printf("please enter i again\n");

return;

}

else

{

LinkList q=p-next;

e=p-next-data;

p-next=p-next-next;

free(q);

}

}

}

//遍歷函數(shù)

void TraverseLink(LinkList L)

{

LinkList p=L-next;

if(!p)

{

printf("the Link L is empty\n");

}

while(p)

{

printf("%d\n",p-data);

p=p-next;

}

}

//鏈表長(zhǎng)度函數(shù)

int LengthLink(LinkList L)

{

int i=0;

LinkList p=L-next;

while(p)

{

p=p-next;

i++;

}

return i;

}

//合并函數(shù)

void MergeLink(LinkList L1,LinkList L2)

{

int i=0,flag=0;

LinkList p1=L1-next,p2=L2-next;

LinkList p=(LinkList)malloc ((LengthLink(L1)+LengthLink(L2)+2)*sizeof(LNode));

LinkList pre=p;

if (!p)

{

printf("MergeLink error\n");

return;

}

p-next=NULL;

while (p1p2)

{

if (p1-data=p2-data)

{

InsertLink(p,i++,p2-data);

p2=p2-next;

}

else

{

InsertLink(p,i++,p1-data);

p1=p1-next;

}

}

while (p1)

{

InsertLink(p,i++,p1-data);

p1=p1-next;

}

while(p2)

{

InsertLink(p,i++,p2-data);

p2=p2-next;

}

while(pre)

{

pre=pre-next;

}

LinkList q=L1;

L1=p;

DestroyLink(q);

DestroyLink(L2);

}

c語言創(chuàng)建鏈表?

#includestdio.h

#includestdlib.h

typedef struct data?{?int number;?struct data *next;?} DATA;

int main()?{?int n;?DATA *head,*p;

printf("how many?\n"); scanf("%d",n); head=create(n); printf("there is all\n");

while ( head!=NULL ) { printf("%d ",head-number); head=head-next; } printf("\n");

while ( head!=NULL ) {?p=head; head=head-next; free(p); }

return 0;

}

DATA *create(int n)?{?DATA *head=NULL,*t=NULL,*tial=NULL;?int i;

printf("let's create it\n");?head=(DATA*)malloc(sizeof(DATA));

if ( !head )?{?printf("error"); return NULL;?}

head-next=NULL;?printf("enter your data\n");? scanf("%d",head-number);

for ( i=1,tial=head;in;i++ )?{

t=(DATA*)malloc(sizeof(DATA));?if ( !t )?{?printf("error"); return head; }

scanf("%d",t-number);?t-next=NULL; tial-next=t;?tial=t;

}

return head;

}

如何用C語言編寫一個(gè)鏈表?

可以用結(jié)構(gòu)體和指針來實(shí)現(xiàn)

定義:

定義一個(gè)單個(gè)元素的結(jié)構(gòu)

typedef?struct?Chain_tag?{?//?這里用typedef來定義,方便使用

int?data;?//?這里的數(shù)據(jù)可以是任意類型

//其他數(shù)據(jù)

struct?Chain_tag?*prev,?*next;//?由于Chain為不完全類型,故只能用指針的方式聲明

}?Chain;

使用:

用單個(gè)結(jié)構(gòu)體的指針作為head

#include?malloc.h

//Chain的定義寫在這里

Chain?*

alloc_single_chain(int?data?/*,?(其他參數(shù))*/)

{

Chain?*tmp;

tmp?=?malloc(sizeof(Chain));

tmp.data?=?data;

//...其余數(shù)據(jù)初始化

tmp.prev?=?tmp.next?=?NULL;?//?將前后指針置為NULL

return?tmp;

}

void

dispose_chain(Chain?*target)?//其實(shí)這里功能簡(jiǎn)單,用宏實(shí)現(xiàn)也可以

{

free(target);

return;

}

int?main()

{

Chain?*head;

Chain?*pos;

head?=?alloc_single_chain(10);//初始化起始結(jié)點(diǎn)

head-next?=?alloc_single_chain(11);//同理。。下一個(gè)結(jié)點(diǎn)

for?(pos?=?head;?pos;?pos?=?pos-next)//清理垃圾好習(xí)慣

{

dispose_chain(pos);

}

return?0;

}

這里有幾點(diǎn)要注意:

由于鏈表用指針來實(shí)現(xiàn),故不要忘記分配內(nèi)存

垃圾清理時(shí)一定要從起始結(jié)點(diǎn)開始依次向后釋放,以防內(nèi)存泄漏

用c語言創(chuàng)建鏈表

主函數(shù)這里

Linklist?List;

printf("輸入創(chuàng)建鏈表的長(zhǎng)度:");

scanf("%d",num);

CreateList_H(List,num); //創(chuàng)建鏈表

改為?

LNode?List;

printf("輸入創(chuàng)建鏈表的長(zhǎng)度:");

scanf("%d",num);

CreateList_H(List,num); //創(chuàng)建鏈表

函數(shù)內(nèi)在堆上分配好內(nèi)存,但是 沒有傳遞到棧上

另外?你的變量名很迷人

c語言鏈表建立

#includestdio.h

#includestring.h

#includeconio.h

#includestdlib.h

typedef struct

{

char sno[10];

char name[10];

float score[3];

}Student;

typedef struct node

{

Student data;

struct node *next;

}LinkList;

Student s;

LinkList *head=NULL;

LinkList *last=NULL;

void Create(Student s)

{

LinkList *p;

p=(LinkList*)malloc(sizeof(LinkList));

p-data=s;

p-next=NULL;

if(head!=NULL)

{

last-next=p;

last=p;

}

else

{

head=p;

last=p;

}

}

void OutPut()

{

LinkList *p;

p=head;

while(p!=NULL)

{

printf("\n%5s%5s%5.1f%5.1f%5.1f",p-data.sno,p-data.name,p-data.score[0],p-data.score[1],p-data.score[2]);

p=p-next;

}

}

void Delete(LinkList *p) //*p 是要?jiǎng)h除的節(jié)點(diǎn)

{

LinkList *q;

q=head;

while(q!=NULL q-next!=p)

q=q-next;

if(q==NULL)printf("\nNode not exist!");

else

{

printf("刪除的節(jié)點(diǎn)是:");

printf("\n%5s%5s%5.1f%5.1f%5.1f",p-data.sno,p-data.name,p-data.score[0],p-data.score[1],p-data.score[2]);

q-next=p-next;

free(p);

}

}

void Append(Student s)

{

LinkList *p;

LinkList *q;

p=last; //尾插法

q=(LinkList*)malloc(sizeof(LinkList));

q-data=s;

q-next=p-next;

p-next=q;

}

void Save()

{

FILE *fp;

LinkList *p;

p=head;

// char filename="student.txt"

if(fp=fopen("filename","wb")==NULL)printf("\nfile open error");

while(p)

{

if(fwrite((p-data),sizeof(Student),1,fp)!=1)

printf("\nfile write error");

p=p-next;

}

fclose(fp);

}

LinkList * Query(Student s)

{

LinkList *q;

q=head;

while(q!=NULL)

{

if(strcmp(q-data.sno,s.sno)!=0)

q=q-next;

else break;

}

return q;

}

int main()

{

LinkList *p;

int ch;

do

{

system("cls");

printf("\n1.創(chuàng)建 2.添加 3.刪除 4.查詢 5.保存文件 6.輸出 0.退出\n");

scanf("%d",ch);

switch(ch)

{

case 1:printf("\n請(qǐng)輸入:學(xué)號(hào),姓名,三門分?jǐn)?shù)\n");

scanf("%s%s%f%f%f",s.sno,s.name,s.score[0],s.score[1],s.score[2]);

Create(s);break;

case 2:printf("\n請(qǐng)輸入:學(xué)號(hào),姓名,三門分?jǐn)?shù)\n");

scanf("%s%s%f%f%f",s.sno,s.name,s.score[0],s.score[1],s.score[2]);

Append(s);break;

case 3:printf("\n請(qǐng)輸入要?jiǎng)h除的學(xué)號(hào):"); //按學(xué)號(hào)刪除;

scanf("%s",s.sno);

p=Query(s);

Delete(p);break;

case 4:printf("\n請(qǐng)輸入要查詢的學(xué)號(hào):"); //按學(xué)號(hào)查詢

scanf("%s",s.sno);

p=Query(s);

printf("\n%5s%5s%5.1f%5.1f%5.1f",p-data.sno,p-data.name,p-data.score[0],p-data.score[1],p-data.score[2]);

break;

case 5:Save();break;

case 6:OutPut();break;

case 0:exit(0);

}

printf("\npress any key return to menu...");

getch();

}while(1);

return 0;

}

.....

當(dāng)前名稱:創(chuàng)建鏈表函數(shù)c語言 c語言創(chuàng)建鏈表每一步詳解
轉(zhuǎn)載注明:http://www.muchs.cn/article16/dosshgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站改版網(wǎng)站設(shè)計(jì)公司、面包屑導(dǎo)航網(wǎng)頁設(shè)計(jì)公司、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)