導航:首頁 > 創造發明 > c創造鏈表

c創造鏈表

發布時間:2021-08-08 23:43:46

『壹』 c語言創建鏈表

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

『貳』 用c語言創建鏈表

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

『叄』 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語言中如何創建一個鏈表

typedef struct Node
{
int data;
struct Node*pNext;
}Node;
Node*pNode=(Node*)malloc(sizeof(Node));

『伍』 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語言創建鏈表

#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程序創建鏈表

實驗證明你的「感覺」是錯誤的,當然不是必須輸入0,0才可以,但是逗號前面的必須是0,逗號後面的東西可以任意,原因可以在f()函數中找到,自己研究一下下面的程序即可知曉。

//---------------------------------------------------------------------------

#include <stdio.h> /*注意這里*/
#include<malloc.h>
#define LEN sizeof(struct list)

struct list
{char number[8];
char name[10];
struct list *next;
};
int n;

struct list *f(void)
{struct list *p1,*p2,*head;
p1=(struct list *)malloc(LEN);
head=NULL;p2=p1;n=0;
scanf("%7[^,]%*c%9[^\n]%*c",p1->number,p1->name); /*注意這里*/
while(strcmp(p1->number,"0")) /*注意這里*/
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct list *)malloc(LEN);
scanf("%[^,]%*c%[^\n]%*c",p1->number,p1->name); /*注意這里*/
}
p2->next=NULL;
return(head);
}
void p(struct list *s) /*輸出鏈表*/
{
if (s!=NULL) {
printf("%s:%s\n",s->name,s->number);
p(s->next );
}
}
main()
{struct list *a;
a=f(); /*創建鏈表,輸入數據的格式為,每行一組數據,每組數據是由逗號分隔的兩個字元串,以0,0結束輸入*/
putchar('\n');
p(a); /*輸出*/
}

//---------------------------------------------------------------------------

『捌』 c語言創建鏈表

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

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;
}
}
求採納為滿意回答。

『玖』 C語言創建帶頭結點的鏈表

#include<stdio.h>
#include<stdlib.h>
#defineN8
typedefstructlist
{
intdata;
structlist*next;
}SLIST;

SLIST*creatlist(int*a)
{
/*這里僅僅是創建,不能在這里輸出數據*/
//在這里首先創建頭結點
//在這里新建有效結點
inti=0;
SLIST*head,*r,*p;
head=(SLIST*)malloc(sizeof(SLIST));
r=p=head;
for(i=0;i<N;i++)
{
p=(SLIST*)malloc(sizeof(SLIST));
p->data=*(a+i);
p->next=NULL;
r->next=p;
r=p;
}
returnhead->next;
}
voidoutlist(SLIST*h)
{
/*這里輸出鏈表中各個數據*/
while(h)
{
printf("%d",h->data);
h=h->next;
}
}

intmain(void)
{
SLIST*head;
inta[N],i;
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
head=creatlist(a);
outlist(head);
return0;
}
12345678
12345678Pressanykeytocontinue

閱讀全文

與c創造鏈表相關的資料

熱點內容
無形資產年限怎麼確定 瀏覽:542
工商登記人員工作總結2018 瀏覽:799
我要發明機器人300字 瀏覽:341
轉讓合作書 瀏覽:512
上海聯瑞知識產權代理有限公司寧波分公司 瀏覽:364
西安私人二手挖機轉讓 瀏覽:698
債務股權轉讓 瀏覽:441
食堂轉讓合同範本 瀏覽:335
廣西華航投資糾紛 瀏覽:902
萌分期投訴 瀏覽:832
金軟pdf期限破解 瀏覽:730
馬鞍山學化妝 瀏覽:41
膠州工商局姜志剛 瀏覽:786
了解到的發明創造的事例 瀏覽:391
2012年中國知識產權發展狀況 瀏覽:773
合肥徽之皇知識產權代理有限公司 瀏覽:636
天津企興知識產權待遇 瀏覽:31
二項基本公共衛生服務項目試題 瀏覽:305
基本公共衛生服務考核標准 瀏覽:543
公共衛生服務考核評估辦法 瀏覽:677