當前位置:首頁 » 編程語言 » c語言創建鏈表
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言創建鏈表

發布時間: 2022-01-28 18:23:56

⑴ 關於c語言建立鏈表的代碼

L -> data[i] = a[i]; 位於for循環中,是給線性表中的各個元素賦值;
L -> length用來說明線性表長度,也就是元素的個數

⑵ C語言 關於鏈表的創建

#include<stdio.h>
#include<stdlib.h>

typedefintelemtype;
typedefstructLnode{
elemtypedata;
Lnode*next;
}Lnode;

Lnode*CreatList(Lnode*Head){
Head=(Lnode*)malloc(sizeof(Lnode));
Head->next=NULL;
Lnode*p=Head;
printf("請輸入元素的個數:");
inti,n;
scanf("%d",&n);
for(i=0;i<n;++i){
p->next=(Lnode*)malloc(sizeof(Lnode));
printf("請輸入第%d個元素:",i+1);
scanf("%d",&p->next->data);
p=p->next;
}
p->next=NULL;
returnHead;
}

voidAllList(Lnode*head){
Lnode*p=head->next;
while(p){
printf("%d",p->data);
p=p->next;
}
printf(" ");
}

intmain(){
Lnode*head=NULL;
head=CreatList(head);
AllList(head);
return0;
}

⑶ c語言創建鏈表

1、你使用了malloc函數但沒有導入頭文件malloc.h。
2、函數DATA *create(int n);沒有申明就直接調用。
(另外main函數中DATA* head;給個初值NULL,避免野指針。)
修改以上內容,親測可運行。

⑷ 怎樣創建一個線性鏈表(C語言)

/*線性鏈表的構建*/
#include<stdio.h>
#include<stdlib.h>

typedefstructLnode
{
intdata;
structLnode*next;
}Lnode;

intmain()
{
Lnode*H,*p1,*p2,*p3,*p4;
H=(Lnode*)malloc(sizeof(Lnode));
p1=(Lnode*)malloc(sizeof(Lnode));
p2=(Lnode*)malloc(sizeof(Lnode));
p3=(Lnode*)malloc(sizeof(Lnode));
p4=(Lnode*)malloc(sizeof(Lnode));

p1->data=132;
p1->next=p2;
p2->data=942;
p2->next=p3;
p3->data=158;
p3->next=182;
p4->data=231;
p4->next=NULL;

printf("%d,%d ",p1->data,p3->data);
printf("%d",p1->next->data);

return0;
}

⑸ C語言如何創建單鏈表

C語言創建單鏈表如下:

#include"stdio.h"

#include"stdlib.h"

#include"malloc.h"

#include "iostream.h"

typedef struct node

{

intdata;

node * next;

}node , * List;

void create(int n)

{

int c;

List s,L;

L=(List)malloc(sizeof(node));

L->next=NULL;

printf("請輸入第1個數據:");

scanf("%d",&c);

L->data=c;

for(int i=2;i<=n;i++)

{

s=(List)malloc(sizeof(node));

printf("請輸入第%d個數據:",i);

scanf("%d",&c);

s->data=c;

s->next=L;

L->next =s;

}

printf("鏈表創建成功!");

}

void main()

{

int n;

printf("請你輸入鏈表的個數:");

scanf("%d",&n);

create(n);

}

⑹ c語言鏈表的創建

這個鏈表做得不好。其實鏈表可以不用創建這一步。因為插入操作已經包含有創建功能了。else後面的語句,就如同你給繩子打結一樣。鏈表的節點好比一段一段的繩子,現在你需要把它們都接起來。你每接一段,手就要往後移動一節,以准備給下一段打結。else後面的語句,其實就是讓當前指針指向的節點後移。
我給你個程序:
#include <stdio.h>
#include <stdlib.h>

typedef struct tagPERSON //個人信息結構
{
char name[20];
long age;
}PERSON;

//template<typename DT> //如果是C++的話,這里方便許多,可以使用模板和類
typedef struct tagLNODE* pLNODE;
typedef struct tagLNODE //鏈表節點
{
PERSON data;
pLNODE next;
}LNODE;

int link_insert(pLNODE *head,PERSON data)//鏈表插入
{
pLNODE cur_tmp,lnode_tmp;

cur_tmp=*head;
lnode_tmp=(pLNODE)malloc(sizeof(LNODE));
if(lnode_tmp==NULL)return -1;
lnode_tmp->data=data;
lnode_tmp->next=NULL;

if(*head==NULL)
*head=lnode_tmp; //如果head為空,則需要對main()中的head修改,所以head的類型為指向指針的指針
else
{
while(cur_tmp->next!=NULL)
cur_tmp=cur_tmp->next;
cur_tmp->next=lnode_tmp;
}

return 0;
}

int link_display_cmd(pLNODE head) //控制台下的鏈表顯示
{
pLNODE cur_tmp;

cur_tmp=head;
while(cur_tmp!=NULL)
{
printf("%s:%d\n",(cur_tmp->data).name,(cur_tmp->data).age);
cur_tmp=cur_tmp->next;
}
return 0;
}

int link_clear(pLNODE *head) //清空鏈表
{
pLNODE cur_tmp,old_tmp;

cur_tmp=*head;
while(cur_tmp!=NULL)
{
old_tmp=cur_tmp;
cur_tmp=cur_tmp->next;
free(old_tmp);
}

*head=NULL;
return 0;
}

int main(void)
{
pLNODE head=NULL;
PERSON temp;

printf("Please input the name:");
scanf("%s",temp.name);
printf("Please input the age:");
scanf("%d",&(temp.age));
while(temp.age>0)
{
link_insert(&head,temp);
printf("Please input the name:");
scanf("%s",temp.name);
printf("Please input the age:");
scanf("%d",&(temp.age));
}

link_display_cmd(head);
link_clear(&head);

return 0;
}

⑺ 用c語言創建鏈表

void CreateList_H(Linklist L,int n)中的L是局部變數,你生成的頭結點L不能被返回,應該改為:
Linklist CreateList_H(int n)
調用方式和函數內部返回值都需要相應改動。

⑻ 急~~~數據結構用C語言創建鏈表

#include<stdio.h>
#include<stdlib.h>
typedef struct node *pointer;
struct node
{
int data;
pointer next;
};
pointer next,null,p,L,s,q;
int j,e,n,k;
creater(int n)
{
int i;
L=(struct node *)malloc(sizeof(struct node));
L->next-null;
s=L;
for(i=1;i<=n;i++)
{printf("\n輸入數據%d:",i);
scanf("%d",&k);
p=(struct node *)malloc(sizeof(struct node));
p->data=k;
s->next=p;
s=p;
}
p->next=null;
}
print()
{pointer p;
p=L->next;
while(p)
{printf("\n%d",p->data);
p=p->next;
}
printf("\n");
}
insert(int i,int e)
{if(i<1||i>n+1)printf("不存在 i\n");
else
{j=0;p=L;
while(j<i-1)
{p=p->next;j++;}
q=(struct node *)malloc(sizeof(struct node));
q->data=e;
q->next=p->next;
p->next=q;
}
}
delete(int i,int e)
{if(i<1||i>n)printf("不存在 i\n");
else
{j=0;p=L;
while(j<i-1)
{p=p->next;j++;}
q=p->next;
p->next=q->next;
e=q->data;
free(q);
printf("e=%d\n",e);
}
}
main()
{int i=1;
while(i)
{printf(" 1--代表建立新的鏈表 \n");
printf(" 2--代表添加元素\n");
printf(" 3--代表刪除元素\n");
printf(" 4--代表輸出當前表中的元素\n");
printf(" 0--代表退出\n");
printf(" 請選擇!\n");
scanf("%d",&i);
switch(i)
{case 1:printf("n="); /*初始化鏈表的時候,n代表你想要輸入的數據個數*/
scanf("%d",&n);
creater(n);
print();break;
case 2:printf("i=");
scanf("%d",&i);
printf("input e");
scanf("%d",&e);
insert(i,e);
print();break;
case 3:printf("i=");
scanf("%d",&i);
delete(i,e);
print();break;
case 4:print();break;
case 0:return;break;
default:printf("ERROR!Try again!\n\n");
}
}
getch();
}

⑼ C語言中怎樣建立鏈表

參考以前寫的這個吧,寫的不好,你修改吧
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define CD sizeof(struct Biao)
struct Biao
{
int num;
struct Biao *next;
};
int m,x;
int main()
{
struct Biao *jianli();
void paixu(struct Biao *n);
void chashu(struct Biao *n);
void shanshu(struct Biao *n);
void qiupingjun(struct Biao *n);
void tuichu();
int n;
struct Biao *t;
printf("1.建立鏈表\n2.排序\n3.插數\n4.刪數\n5.求平均值\n");
printf("請輸入選項:\n");
for(;;)
{
scanf("%d",&n);
switch(n)
{
case 1:t=jianli();break;
case 2:paixu(t);break;
case 3:chashu(t);break;
case 4:shanshu(t);break;
case 5:qiupingjun(t);break;
case 6:tuichu();break;
default:printf("輸入錯誤!請重新輸入\n");
}
}
}
struct Biao *jianli()
{
int i,j;
printf("建立鏈表,請輸入數據:\n");
struct Biao *head,*p1,*p2;
p1=p2=(struct Biao * )malloc(CD);//if(p1==NULL)建立鏈表失敗
for(i=0;i<=100;i++)
{
if(i==0)
{
head=p1;
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
continue;
}
if(p1==NULL)
break;
else
{
scanf("%d",&p1->num);
if(p1->num==-1)
{
p2->num=NULL;
break;
}
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
}
}
if(head->next->num==NULL)
printf("您建立了一個空鏈表\n");
else
{
printf("共有數據:%d\n",i-1);
m=i-1;
printf("輸出鏈表:\n");
p1=head->next;
for(j=0;j<i-1;j++)
{
printf("%d ",p1->num);
p1=p1->next;
}
}
printf("\n");
return(head);
}
void paixu(struct Biao *n)
{
int i,j,a,temp;
a=m;
struct Biao *p1,*p2,*tp;
p2=p1=n->next;
printf("從小到大排序結果:\n");
for(i=0;i<a-1;i++)
{
p1=p2;
tp=p1;
for(j=i+1;j<a;j++)
{
if((p1->next->num)<(tp->num))
{
tp=p1->next;
}
p1=p1->next;
}
temp=tp->num;tp->num=p2->num;p2->num=temp;
p2=p2->next;
}
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
}
void chashu(struct Biao *n)
{
int a,i,j;
struct Biao *p1,*p2,*tp;
m=m+1;
x=m;
a=m;
p1=n;
printf("請插入數字:\n");
p2=(struct Biao *)malloc(CD);
scanf("%d",&p2->num);
p1=n->next;
for(i=0;i<a-1;i++)
{
if(p1->num<=p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(p1->num<p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(n->next->num>p2->num)
{
tp=n->next;
n->next=p2;
p2->next=tp;
break;
}
p1=p1->next;
}
p1=n;//
for(i=0;i<a-1;i++)
{
p1=p1->next;
}
if(p1->num<=p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
}//演算法不簡便
printf("插入後的數據:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("數據個數:%d\n",a);
}
void shanshu(struct Biao *n)
{
int a,i,j;
a=x;
struct Biao *p1,*p2;
printf("請輸入要刪除的數:\n");
scanf("%d",&j);
for(;;)
{
p1=n;
for(i=0;i<a;i++)
{
if(p1->next->num==j)
{
p2=p1->next;
p1->next=p1->next->next;
a-=1;
x-=1;
break;
}
p1=p1->next;
}
if(i==a)
break;
}
printf("結果:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("剩餘數據個數:%d\n",x);
}
void qiupingjun(struct Biao *n)
{
int s,i;
struct Biao *p1;
s=0;
p1=n->next;
for(i=0;i<x;i++)
{
s+=p1->num;
p1=p1->next;
}
printf("平均值為:%f\n",s*1.0/x);
}
void tuichu()
{
exit(0);
}