c語言如何創建鏈表
Ⅰ c語言中怎樣建立鏈表
參考以前寫的這個吧,寫的不好,你修改吧
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define CD sizeof(struct Biao)
struct Biao
{
int num;
struct Biao *next;
};
int m,x;
int main()
{
struct Biao *jianli();
void paixu(struct Biao *n);
void chashu(struct Biao *n);
void shanshu(struct Biao *n);
void qiupingjun(struct Biao *n);
void tuichu();
int n;
struct Biao *t;
printf("1.建立鏈表\n2.排序\n3.插數\n4.刪數\n5.求平均值\n");
printf("請輸入選項:\n");
for(;;)
{
scanf("%d",&n);
switch(n)
{
case 1:t=jianli();break;
case 2:paixu(t);break;
case 3:chashu(t);break;
case 4:shanshu(t);break;
case 5:qiupingjun(t);break;
case 6:tuichu();break;
default:printf("輸入錯誤!請重新輸入\n");
}
}
}
struct Biao *jianli()
{
int i,j;
printf("建立鏈表,請輸入數據:\n");
struct Biao *head,*p1,*p2;
p1=p2=(struct Biao * )malloc(CD);//if(p1==NULL)建立鏈表失敗
for(i=0;i<=100;i++)
{
if(i==0)
{
head=p1;
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
continue;
}
if(p1==NULL)
break;
else
{
scanf("%d",&p1->num);
if(p1->num==-1)
{
p2->num=NULL;
break;
}
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
}
}
if(head->next->num==NULL)
printf("您建立了一個空鏈表\n");
else
{
printf("共有數據:%d\n",i-1);
m=i-1;
printf("輸出鏈表:\n");
p1=head->next;
for(j=0;j<i-1;j++)
{
printf("%d ",p1->num);
p1=p1->next;
}
}
printf("\n");
return(head);
}
void paixu(struct Biao *n)
{
int i,j,a,temp;
a=m;
struct Biao *p1,*p2,*tp;
p2=p1=n->next;
printf("從小到大排序結果:\n");
for(i=0;i<a-1;i++)
{
p1=p2;
tp=p1;
for(j=i+1;j<a;j++)
{
if((p1->next->num)<(tp->num))
{
tp=p1->next;
}
p1=p1->next;
}
temp=tp->num;tp->num=p2->num;p2->num=temp;
p2=p2->next;
}
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
}
void chashu(struct Biao *n)
{
int a,i,j;
struct Biao *p1,*p2,*tp;
m=m+1;
x=m;
a=m;
p1=n;
printf("請插入數字:\n");
p2=(struct Biao *)malloc(CD);
scanf("%d",&p2->num);
p1=n->next;
for(i=0;i<a-1;i++)
{
if(p1->num<=p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(p1->num<p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(n->next->num>p2->num)
{
tp=n->next;
n->next=p2;
p2->next=tp;
break;
}
p1=p1->next;
}
p1=n;//
for(i=0;i<a-1;i++)
{
p1=p1->next;
}
if(p1->num<=p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
}//演算法不簡便
printf("插入後的數據:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("數據個數:%d\n",a);
}
void shanshu(struct Biao *n)
{
int a,i,j;
a=x;
struct Biao *p1,*p2;
printf("請輸入要刪除的數:\n");
scanf("%d",&j);
for(;;)
{
p1=n;
for(i=0;i<a;i++)
{
if(p1->next->num==j)
{
p2=p1->next;
p1->next=p1->next->next;
a-=1;
x-=1;
break;
}
p1=p1->next;
}
if(i==a)
break;
}
printf("結果:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("剩餘數據個數:%d\n",x);
}
void qiupingjun(struct Biao *n)
{
int s,i;
struct Biao *p1;
s=0;
p1=n->next;
for(i=0;i<x;i++)
{
s+=p1->num;
p1=p1->next;
}
printf("平均值為:%f\n",s*1.0/x);
}
void tuichu()
{
exit(0);
}
Ⅱ c語言創建鏈表
1、你使用了malloc函數但沒有導入頭文件malloc.h。
2、函數DATA *create(int n);沒有申明就直接調用。
(另外main函數中DATA* head;給個初值NULL,避免野指針。)
修改以上內容,親測可運行。
Ⅲ c語言鏈表建立
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
typedef struct
{
char sno[10];
char name[10];
float score[3];
}Student;
typedef struct node
{
Student data;
struct node *next;
}LinkList;
Student s;
LinkList *head=NULL;
LinkList *last=NULL;
void Create(Student s)
{
LinkList *p;
p=(LinkList*)malloc(sizeof(LinkList));
p->data=s;
p->next=NULL;
if(head!=NULL)
{
last->next=p;
last=p;
}
else
{
head=p;
last=p;
}
}
void OutPut()
{
LinkList *p;
p=head;
while(p!=NULL)
{
printf("\n%5s%5s%5.1f%5.1f%5.1f",p->data.sno,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2]);
p=p->next;
}
}
void Delete(LinkList *p) //*p 是要刪除的節點
{
LinkList *q;
q=head;
while(q!=NULL && q->next!=p)
q=q->next;
if(q==NULL)printf("\則敗nNode not exist!");
else
{
printf("刪除的節點是:");
printf("\n%5s%5s%5.1f%5.1f%5.1f",p->data.sno,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2]);
q->next=p->next;
free(p);
}
}
void Append(Student s)
{
LinkList *p;
LinkList *q;
p=last; //尾插法
q=(LinkList*)malloc(sizeof(LinkList));
q->data=s;
q->next=p->next;
p->next=q;
}
void Save()
{
FILE *fp;
LinkList *p;
p=head;
// char filename="student.txt"
if(fp=fopen("filename","wb")==NULL)printf("\nfile open error");
while(p)
{
if(fwrite(&(p->data),sizeof(Student),1,fp)!=1)
printf("\nfile write error");
p=p->next;
}
fclose(fp);
}
LinkList * Query(Student s)
{
LinkList *q;
q=head;
while(q!=NULL)
{
if(strcmp(q->data.sno,s.sno)!=0)
q=q->next;
else break;
}
return q;
}
int main()
{
LinkList *p;
int ch;
do
{
system("cls");
printf("\n1.創建 2.添加 3.刪除 4.查詢 5.保拍盯槐存文件 6.輸出 0.退出\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n請輸入:學號,姓名,三門分數\n");
scanf("%s%s%f%f%f",&s.sno,&s.name,&s.score[0],&s.score[1],&s.score[2]);
Create(s);break;
case 2:printf("\n請輸入:學號,姓名,三門分數\n");
scanf("%s%s%f%f%f",&s.sno,&s.name,&s.score[0],&s.score[1],&s.score[2]);
Append(s);break;
case 3:printf("\n請輸襲友入要刪除的學號:"); //按學號刪除;
scanf("%s",&s.sno);
p=Query(s);
Delete(p);break;
case 4:printf("\n請輸入要查詢的學號:"); //按學號查詢
scanf("%s",&s.sno);
p=Query(s);
printf("\n%5s%5s%5.1f%5.1f%5.1f",p->data.sno,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2]);
break;
case 5:Save();break;
case 6:OutPut();break;
case 0:exit(0);
}
printf("\npress any key return to menu...");
getch();
}while(1);
return 0;
}
.....
Ⅳ 如何用C語言創建一個鏈表,實現增、刪、改、查
#include
Ⅳ 用c語言創建鏈表
主函數這里
LinklistList;
printf("輸入創建鏈表的長度:");
scanf("%d",&num);
CreateList_H(List,num); //創建鏈表
改為
LNodeList;
printf("輸入創建鏈表的長度:");
scanf("%d",&num);
CreateList_H(&List,num); //創建鏈表
函數內在堆上分配好內存,但是 沒有傳遞到棧上
另外你的變數名很迷人
Ⅵ 如何實現C語言鏈表
鏈表的創建:
#include 「stdlib.h」
#include 「stdio.h」
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf(「%ld,%f」,&p1->num,&p1->score);
head=NULL;
while(p1->num != 0)
{
n=n+1;
if(n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct student *)malloc(LEN);
scanf(「%ld,%f」,&p1->num,&p1->score);
}
p2->next = NULL;
return(head);
}
void main()
{
creat();
}
這樣便可創建鏈表
Ⅶ 如何用C語言編寫一個鏈表
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
struct Node
{
int data;//數據域
struct Node * next;//指針域
};
/*************************************************************************************
*函數名稱:Create
*函數功能:創建鏈表.
*輸入:各節點的data
*返回值:指針head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函數名稱:insert
*函數功能:在鏈表中插入元素.
*輸入:head 鏈表頭指針,p新元素插入位置,x 新元素中的數據域內容
*返回值:無
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函數名稱:del
*函數功能:刪除鏈表中的元素
*輸入:head 鏈表頭指針,p 被刪除元素位置
*返回值:被刪除元素中的數據域.如果刪除失敗返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函數名稱:print
*函數功能:列印鏈表中的元素
*輸入:head 鏈表頭指針
*返回值:無
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函數名稱:main
*函數功能:主函數創建鏈表並列印鏈表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}