當前位置:首頁 » 編程語言 » c語言鏈表的創建

c語言鏈表的創建

發布時間: 2022-01-15 01:58:00

A. c語言創建鏈表

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

B. c語言 建立鏈表

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

#define ok 1
#define error 0
#define flag 0

typedef int status;

//結構體定義
typedef struct LNode{
char name[16];
int id;
int grade;
struct LNode *next;
}LNode , *LinkList;
int n=0; //n為節點數

//函數聲明
status CreatList_L(LinkList &L); //創建鏈表
status ShowList_L(LinkList L); //展示鏈表

//主函數
void main()
{
printf("\n\n");;
printf("===============zzb鏈表系統.cpp=============\n\n");
LinkList L;
CreatList_L(L);
int b;
int flag1=1;
while (flag1!=0)
{
printf("請選擇進行哪項操作:1.展示鏈表2.刪除指定位置學生信息3.增加指定位置學生信息4.查找指定id學生信息.5.修改學生信息\n");
scanf("%d",&b);
switch (b)
{
case 1:
ShowList_L(L);
break;
break;
default: printf("enter number error!");
}
printf("是否繼續操作(否0/是任意常數):\n");
scanf("%d",flag1);
}
printf("all down!\n");
}//main()

//各功能函數定義
status CreatList_L(LinkList &L)
{
if(!(L=(LinkList)malloc(sizeof(LNode))))
{
printf("error!\n"); return (error);
} //頭指針L
L->next=NULL; //L->next指向NULL,頭結點一般不儲存信息
LinkList p,tail; //指向當前節點指針P與尾指針tail
tail=L; //定義尾指針指向
printf("輸入姓名,學號,成績創建鏈表(以/0 0 0/結束):\n");
p=(LinkList)malloc(sizeof(LNode));
scanf("%c",&p->name);
scanf("%d",&p->id);
scanf("%d",&p->grade);
while(p->id!=flag) //進行對節點數值送數,並以id為0作為結束標志
{
tail->next=p;
p->next=NULL;
tail=p;
n++;
p=(LinkList)malloc(sizeof(LNode)); //生成新節點
scanf("%c",&p->name);
scanf("%d",&p->id);
scanf("%d",&p->grade);
}
printf("創建鏈表成功!\n");
return(ok);
}//CreateLisk_L()

status ShowList_L(LinkList L)
{
LinkList m; //m為指向L->next的指針
m=L->next;
int i=0;
printf("學生信息如下:\n");
printf("姓名\t學號\t成績:\n");
while(i<n)
{
printf("%c\t%d\t%d",m->name,m->id,m->grade);
i++;
m=m->next;
}
printf("輸出完畢!\n");
return (ok);
}//ShowList_L()

應該完成你的要求,你還可以在之後加功能~

C. 怎樣創建一個線性鏈表(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;
}

D. 創建鏈表的C語言程序

#include <stdlib.h>
#include <stdio.h>
struct list
{
int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
void main()
{
link ptr,head;
int num,i;
ptr=(link)malloc(sizeof(node));
head=ptr; //這里搞反了
printf("please input 5 numbers==>\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num; //應該加上這一句
ptr->next=(link)malloc(sizeof(node));
if(i==4)ptr->next=NULL;
else ptr=ptr->next;
}
ptr=head;
while(ptr!=NULL)
{
printf("The value is ==>%d\n",ptr->data); //加換行符好看些
ptr=ptr->next;
}
}

E. c語言鏈表建立

#include
#include
struct
chain
{
int
value;
struct
chain
*next;
};
struct
chain
*create()
{
struct
chain
*head,*tail,*p;
int
x;
head
=
tail
=
null;
while(scanf("%d",&x)==1)
{
p=(struct
chain*)malloc(sizeof(struct
chain));
p->value=x;
p->next=null;
if(head==null)
head
=
tail
=
p;
else
tail=tail->next=p;
}
return
head;
}
struct
chain
*inlink(struct
chain
*head,int
a,int
b)
//int
a代表要插入的節點,int
b代表創建節點的數據域
{
struct
chain
*p,*q,*s;
s
=
(struct
chain
*)malloc(sizeof(struct
chain));
s->value=b;
if(head==null)
{
head
=
s;
head->next
=
null;
}
if(head->value
==
a)
{
s->next=head;
head
=
s;
}
else
{
p=head;
while((p->value!=a)&&(p->next!=null))
{
q=p;
p=p->next;
}
if(p->value
==
a)
{
q->next
=
s;
s->next
=
p;
}
else
{
p->next=s;
s->next=null;
}
}
return
(head);
}
struct
chain
*dellink(struct
chain
*head,int
a)
//int
a代表要刪除的節點
{
struct
chain
*q,*p;
if(head
==
null)
printf("找不到節點!\n");
else
if(head->value
==
a)
{
p
=
head;
head
=
head->next;
}
else
{
p=head;
while((p->value!=a)&&(p->next!=null))
{
q=p;
p=p->next;
}
if(p->value
!=
a)
printf("鏈表不存在此節點!\n");
else
{
q->next
=
p->next;
free(p);
}
}
return
(head);
}
void
main()
{
struct
chain
*p,*q;
q=create();
//鏈表的創建;
//q=inlink(create(),3,1);
//鏈表的插入;
//q=dellink(create(),2);
//鏈表的刪除;
while(q){
//輸出鏈表;
printf("%d\n",q->value);
p=q->next;
free(q);
q=p;
}
}

F. 關於C語言建立鏈表的代碼

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

G. 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;
}

H. C語言鏈表創建和輸入

#include "link.h"
//實現類似於strlen
struct string_linkinfo BL_Stringlen(BLString *link)
{
struct string_linkinfo st_string = {0, 0};
char *p;
Block *pnode = NULL;
if(NULL == link){
printf("Invalid arg...\n");
return st_string;
}
//鏈表為空
if(NULL == link->head){
return st_string;
}
else{
pnode = link->head;
while(pnode != link->tail)
{
st_string.length += N;
st_string.count_node++;

pnode = pnode->next;
}//當while循環執行完後,pnode一定是最後一個節點
p = pnode->buffer;
st_string.count_node++;
while('\0' != *p)
{
st_string.length++;
p++;
}
}
return st_string;
}

//實現類似於strcmp
int BL_Stringcmp(BLString *link1, BLString *link2)
{
assert(NULL != link1 && NULL != link2);//斷言
int i;
Block *pnode1 = link1->head;
Block *pnode2 = link2->head;
while(pnode1 != link1->tail && pnode2 != link2->tail)
{
for(i = 0; i < N; i++)
{
if((pnode1->buffer)[i] != (pnode2->buffer)[i])
return (pnode1->buffer)[i] - (pnode2->buffer)[i];
}
pnode1 = pnode1->next;
pnode2 = pnode2->next;
}
return strcmp(pnode1->buffer, pnode2->buffer);
}
//實現類似於strcpy
/*BLString *BL_Stringcpy(BLString *link1, BLString *link2)
{

}*/
//類似於strcat
BLString *BL_Stringcat(BLString *link1, BLString *link2)
{
//函數入口檢測
if(NULL == link1 || NULL == link2)
{
printf("Invalid arg...\n");
return NULL;
}
if(NULL == link1->head){
link1->head = link2->head;
}
else//都不為空
{
int i, tmp = 0;
Block *pnode1 = link1->head;
Block *pnode2 = link2->head;
while(pnode1 != link1->tail)//將pnode1定位到末尾
pnode1 = pnode1->next;
//先看看缺多少個字元
tmp = strlen(pnode1->buffer);
//尾首鏈接
pnode1->next = pnode2;
while(pnode2 != link2->tail)
{
for(i = 0; i < N - tmp; i++)
(pnode1->buffer)[tmp + i] = (pnode2->buffer)[i];
for(i = 0 ; i < tmp; i++)
(pnode2->buffer)[i] = (pnode2->buffer)[N-tmp+i];
pnode1 = pnode1->next;
pnode2 = pnode2->next;
}
if(strlen(pnode2->buffer) < N-tmp)
{
for(i = 0; i < strlen(pnode2->buffer); i++)
(pnode1->buffer)[tmp + i] = (pnode2->buffer)[i];

(pnode1->buffer)[tmp+i] = '\0';
free(pnode2);
link1->tail = pnode1;
}
else{
for(i = 0; i < N - tmp; i++)
(pnode1->buffer)[tmp + i] = (pnode2->buffer)[i];
strcpy(pnode2->buffer, (pnode2->buffer)+(N-tmp));
link1->tail = pnode2;
}
link1->tail->next = NULL;
}

return link1;
}
void BL_Printstring(BLString *plink)
{
int i;
if(NULL == plink){
printf("Invalid arg...\n");
return ;
}
if(NULL == plink->head){
printf("The string is empty...\n");
return ;
}
Block *pnode = plink->head;
while(pnode != plink->tail)
{
for(i = 0; i < N; i++)
printf("%c", (pnode->buffer)[i]);

pnode = pnode->next;
}
printf("%s\n", pnode->buffer);

return;
}

I. 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;
}

J. C語言創建一個鏈表並輸出

你現在的問題是不是不能創建鏈表,只能輸入一個數據。
我遇到過這種情況,高手應該知道是什麼原因,但我不知道,只是了解除了什麼問題
你用循環getchar(),一次都結束了,說明是在輸入一個的時候順便也把「回車」接收了。
所以循環停止了。
解決方法:你可以再循環體內多寫一個getchar(),讓這個去接收多餘的『\n』
或者你用這個函數 fflush(stdin);清空輸入緩沖區。
你程序還有問題,你自己調吧

熱點內容
訪問攔截怎麼解除安卓 發布:2024-09-20 17:28:48 瀏覽:275
蘿卜干存儲 發布:2024-09-20 17:21:37 瀏覽:715
蘋果手機如何遷移軟體到安卓手機 發布:2024-09-20 17:21:34 瀏覽:692
查看伺服器ip限制 發布:2024-09-20 16:56:27 瀏覽:389
p搜系統只緩存1頁為什麼 發布:2024-09-20 16:48:51 瀏覽:839
上網的賬號和密碼是什麼東西 發布:2024-09-20 16:31:31 瀏覽:612
安卓手機王者榮耀如何調超高視距 發布:2024-09-20 16:31:30 瀏覽:428
安卓G是什麼app 發布:2024-09-20 16:23:09 瀏覽:81
iphone怎麼壓縮文件 發布:2024-09-20 16:08:18 瀏覽:356
linux查看用戶名密碼是什麼 發布:2024-09-20 16:03:20 瀏覽:744