① 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語言如何創建單鏈表
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語言鏈表問題
#include
"malloc.h"
struct
num
{
int
a;
struct
num
*next;
};
void
main()
{
int
i,n=0;
struct
num
*head,*p1,*p2;
head=(struct
num
*)
malloc(sizeof(struct
num));
p1=head;
/*p2=head;*/
for
(i=0;i<5;i++)
{
p2=(struct
num
*)
malloc(sizeof(struct
num));
scanf("%d",&n);
p2->a=n;
p1->next=p2;
p1=p2;
}
p1->next=NULL;
p1=head->next;
while(p1!=NULL)
{
printf("%d
",p1->a);
p1=p1->next;
}
printf("\n");
}
***********************************************************
#include
"malloc.h"
struct
num
{
int
a;
struct
num
*next;
};
void
main()
{
int
i,n=0;
struct
num
*head,*p1,*p2;
head=(struct
num
*)
malloc(sizeof(struct
num));
p1=head;
/*p2=head;*/
for
(i=0;i<5;i++)
{
p2=(struct
num
*)
malloc(sizeof(struct
num));
scanf("%d",&(p2->a));
p1->next=p2;
p1=p2;
}
p1->next=NULL;
p1=head->next;
while(p1!=NULL)
{
printf("%d
",p1->a);
p1=p1->next;
}
printf("\n");
}
{
http://chinastar.uueasy.com/read-htm-tid-292-fpage-6.html}
可以看一下這篇文章,鏈表創建輸出
④ c語言鏈表
指針在x86系統里大小是32位4個位元組,在64位系統是8位元組大小。指針好比一個盒子,盒子里有個東西也就是它指向的內容,內容是一個實體對象的首地址。盒子本身不能存放實體對象,就好比盒子里有張名片,通過名片你可以找到這個人,總不能把人放盒子里吧。
得有這個人然後把找到這個人的名片放盒子里。
⑤ c語言單向鏈表的問題
完善了一下,頭結點沒存數,print時要從第二個開始:
#include<stdio.h>
#include<stdlib.h>
struct
node{
int
num;
struct
node
*next;
};
//構建空的鏈表
void
InitList(struct
node
*&L){//修改
L
=
(struct
node*)malloc(sizeof(struct
node));
L->next
=
NULL;
L->num=0;
printf_s("InitList
sucess!");
}
//創建單鏈表
void
CreateList(struct
node
*&L,int
n){//修改
struct
node
*temp,*p;
L
=
(struct
node*)malloc(sizeof(struct
node));
L->next
=
NULL;
L->num=0;
temp=L;
for
(int
i
=
0;
i
<
n;
i++){
p
=
(struct
node*)malloc(sizeof(struct
node));
scanf_s("%d",
&p->num);
temp->next
=
p;
temp
=
p;
}
temp->next
=
NULL;
}
void
PrintList(struct
node
*L){
struct
node
*temp
=
L->next;//修改,頭結點不使用
while
(temp
!=
NULL){
printf("%d",
temp->num);
temp
=
temp->next;
}
}
void
PrintMenu(){
printf_s("------Menu------\n");
printf_s("0
InitList\n");
printf_s("1
CreateList\n");
printf_s("2
PrintList\n");
}
void
main(){
int
n,c;
struct
node
*La;
c=1;
while(c>=0)
{
PrintMenu();
printf_s("Enter
the
command:
");
scanf_s("%d",
&c);
switch
(c){
case
0:
InitList(La);
break;
case
1:
printf_s("Enter
the
number
of
LinkList:
");
scanf_s("%d",
&n);
CreateList(La,
n);
break;
case
2:
PrintList(La);
break;
default:
printf_s("ERROR,Enter
again:
");
break;
}
}
struct
node
*te;//增加釋放空間
if(La)
{
te=La->next;
free(La);
La=te;
}
system("pause");
}
⑥ C語言單鏈表合並
#include<stdio.h>#include<stdlib.h>structlist{intdata;structlist*next;};//兩個鏈表融合,插入排序函數voidsort(structlist*l1,structlist*l2);//輸出鏈表voidoutput(structlist*head);//輸入鏈表voidinput(structlist*head,intnum);intmain(){intn;list*h1,*h2;//兩個鏈表的頭,下面四行初始化鏈表h1=(structlist*)malloc(sizeof(structlist));h2=(structlist*)malloc(sizeof(structlist));h1->next=NULL;h2->next=NULL;//兩個鏈表輸入printf("請輸入第一個鏈表節點數: ");scanf("%d",&n);input(h1,n);printf("請輸入第二個鏈表節點數: ");scanf("%d",&n);input(h2,n);//合並鏈表並排序sort(h1,h2);//輸出合並後的鏈表output(h1);}voidinput(structlist*head,intnum){structlist*tmp;structlist*end;end=head;printf("請輸入鏈表節點: ");for(inti=0;i!=num;i++){tmp=(structlist*)malloc(sizeof(structlist));scanf("%d",&tmp->data);end->next=tmp;tmp->next=NULL;end=tmp;}}voidsort(structlist*l1,structlist*l2){structlist*p1,*p2,*tmp;p1=l1;p2=l2->next;while(p1->next&&p2){if(p1->next->data>p2->data){tmp=p2->next;p2->next=p1->next;p1->next=p2;p2=tmp;}elsep1=p1->next;}if(p2)p1->next=p2;}voidoutput(structlist*head){while(head->next){printf("%d",head->next->data);head=head->next;}}⑦ C語言鏈表···
struct date{…}/鏈表結構體
p1/定義鏈表結構體的一個指針名插入點/
p1=(struct date *)malloc(sizeof(struct date))/分配鏈表內存
free(newnode)/釋放節點內存
刪除全部節點定義兩個鏈表指針名p1,p2循環刪除
while(p1->next!=NULL)
{p2=p1;
p1->…/p1指向下鏈表一節點
free(p2);
}
⑧ c語言創建鏈表
1、你使用了malloc函數但沒有導入頭文件malloc.h。
2、函數DATA *create(int n);沒有申明就直接調用。
(另外main函數中DATA* head;給個初值NULL,避免野指針。)
修改以上內容,親測可運行。
⑨ C語言鏈表相關
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#defineMAXSIZE100
typedefstructnode{
charname[21];
intnumber;
charsex;
structnode*next;
}linklist;
linklist*CreateList(){
charname[21]="