1. 关于c语言建立链表的代码
L -> data[i] = a[i]; 位于for循环中,是给线性表中的各个元素赋值;
L -> length用来说明线性表长度,也就是元素的个数
2. c语言创建单链表
#include<stdio.h>
#include<stdlib.h>
/*线性表*/
struct TLink {
 int data;
 struct TLink * next;
};/*end struct TLink*/
/*生成新元素*/
struct TLink * new_item(int number)
{
 struct TLink * r = 0;
 r = (struct TLink *)malloc(sizeof(struct TLink));
 r->data = number; 
 r->next = 0;
 return r;
}/*end new_item*/
/*在线性表中查询数据*/
struct TLink * lookup(struct TLink * root, int number)
{
 struct TLink * h = root;
 while(h) {
  if (h->data == number) return h;
  h = h->next ;
 }/*end lookup*/
 return 0;
}
/*在线性表中追加一个数据*/
void append(struct TLink * * root, int number)
{
 struct TLink * r = 0, * n = 0;
 if (!root) return ;
 /*不记录重复元素*/
 if (lookup(*root, number)) return;
 /*如果表为空则新建表*/
 r = *root;
 if (!r) {
  *root = new_item(number);
  return ;
 }/*end if*/
 /*为保证为有序线性表,如果数据比表头还小则作为表头*/
 if (number < r->data ) {
  n = new_item(number);
  n->next = r;
  *root = n;
  return ;
 }/*end if*/
 /*在有序线性表中查找位置插入元素*/
 while(r) {
  n = r->next ;
  /*如果已经是表尾则直接追加*/
  if (!n) {
   n = new_item(number);
   r->next = n;
   return ;
  }/*end if*/
  /*在中央某处插入*/
  if (number < n->data ) {
   r->next = new_item(number);
   r->next->next = n;
   return ;
  }/*end if*/
  r = n;
 }/*end while*/
}/*end append*/
/*打印有序线性表*/
void print(struct TLink * root)
{
 struct TLink * r = root;
 printf("【");
 while(r) {
  printf("%d ", r->data );
  r = r->next ;
 }/*end while*/
 printf("\b】\n");
}/*end print*/
/*将有序线性表h1合并至有序线性表h0,并销毁线性表h1*/
void merge(struct TLink ** h0, struct TLink ** h1)
{
 struct TLink * h = 0, * k = 0;
 if (!h0 || !h1) return ;
 h = *h1;
 while(h) {
  append(h0, h->data );
  k = h;
  h = h->next ;
  free(k);
 }/*end h*/
 h1 = 0;
}
int main(void)
{
 int i = 0; struct TLink * x=0, *y = 0;
 int a[] = {8,4,3,9,5,1};
 int b[] = {7,2,1,5,6,0};
 printf("原数据为:\n数组A:【");
 for(i = 0; i < 6; i++) {
  printf("%d ", a[i]);
  append(&x, a[i]);
 }/*next*/
 printf("\b】\n数组B:【");
 for(i = 0; i < 6; i++) {
  printf("%d ", b[i]);
  append(&y, b[i]);
 }/*next*/
 printf("\b】\n转换为有序线性表\nA:");
 print(x);
 printf("B:");
 print(y);
 printf("AB合并后为:");
 merge(&x, &y);
 print(x);
 return 0;
}
/*以上是顺序线性表的合并程序,逆序只需将插入条件从小于改为大于即可。
合并结果如果要保留,把合并函数的lookup调用删除即可*/
3. 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;
}
4. 怎样创建一个线性链表(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;
}
5. C语言创建一个链表并输出
你现在的问题是不是不能创建链表,只能输入一个数据。
我遇到过这种情况,高手应该知道是什么原因,但我不知道,只是了解除了什么问题
你用循环getchar(),一次都结束了,说明是在输入一个的时候顺便也把“回车”接收了。
所以循环停止了。
解决方法:你可以再循环体内多写一个getchar(),让这个去接收多余的‘\n’
或者你用这个函数 fflush(stdin);清空输入缓冲区。
你程序还有问题,你自己调吧
6. 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;
}
}
7. 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;
}
8. 用c语言创建链表
void CreateList_H(Linklist L,int n)中的L是局部变量,你生成的头结点L不能被返回,应该改为:
Linklist CreateList_H(int n)
调用方式和函数内部返回值都需要相应改动。
9. 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;
}
10. c语言创建链表
1、你使用了malloc函数但没有导入头文件malloc.h。
2、函数DATA *create(int n);没有申明就直接调用。
(另外main函数中DATA* head;给个初值NULL,避免野指针。)
修改以上内容,亲测可运行。
