急!用c语言实现链表
就是链表的创建和输出,我写的为什么运行不了啊??
先是建立个空表头,然后依次插入,构成链表,最后是输出;
谢谢~
以下是我写的程序……
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct list)
struct list
{
int num;
long data;
struct list *next;
};
struct list*head;
struct list initlist (struct list * head)
{
head=(struct list *)malloc(LEN);
head->next=NULL;
}
struct list creat (struct list *head,int n)
{
struct list *p;
int i;
initlist(head);
for(i=n;i>0;--i)
{
p=(struct list *)malloc(LEN);
scanf("%d,%f",&p->num,&p->data);
p->next=head->next;
head->next=p;
}
}
int print(struct list*head)
{
struct list *p1;
p1=head;
if (head!=0)
{
printf("%d,%f",p1->num,p1->data);
p1=p1->next;
}while(p1!=0);
}
main()
{
initlist(head);
creat(head,3);
print(head);
getchar();
getchar();
}
先是建立个空表头,然后依次插入,构成链表,最后是输出;
谢谢~
以下是我写的程序……
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct list)
struct list
{
int num;
long data;
struct list *next;
};
struct list*head;
struct list initlist (struct list * head)
{
head=(struct list *)malloc(LEN);
head->next=NULL;
}
struct list creat (struct list *head,int n)
{
struct list *p;
int i;
initlist(head);
for(i=n;i>0;--i)
{
p=(struct list *)malloc(LEN);
scanf("%d,%f",&p->num,&p->data);
p->next=head->next;
head->next=p;
}
}
int print(struct list*head)
{
struct list *p1;
p1=head;
if (head!=0)
{
printf("%d,%f",p1->num,p1->data);
p1=p1->next;
}while(p1!=0);
}
main()
{
initlist(head);
creat(head,3);
print(head);
getchar();
getchar();
}
...全文
相关推荐
C语言贪食蛇(链表实现)
【C语言与链表】C语言是一种强大的、低级的编程语言,常用于系统编程、嵌入式开发和游戏开发等。它不提供内置的数据结构,但可以通过构造数据类型来实现,如链表。
C语言单链表实现
单链表是计算机科学中数据结构的基础之一,它在C语言中的实现对于理解和掌握数据结构的概念至关重要。在本文中,我们将深入探讨C语言如何通过结构体和指针来实现单链表。首先,我们需要理解单链表的基本概念。
实现两个链表的合并(C语言)
本篇代码主要涉及C语言中的数据结构与链表操作,具体是两个链表的合并以及合并后链表的升序排列。以下是详细的知识点解析:1. **定义链表结构**: 首先,定义了一个名为`structNode`
C语言实现多种链表快速排序
在C语言中实现链表快速排序,首先需要理解链表和快速排序的基本概念。链表不同于数组,它不连续存储数据,而是通过指针连接各个节点。每个节点包含数据元素和指向下一个节点的指针。
C语言数组形链表实现
本文将深入探讨如何使用C语言实现数组形链表,并详细讲解其相关接口。首先,我们需要定义数组形链表的数据结构。它通常包含一个数组,用于存储元素,以及一个指示当前数组容量的变量。


