| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2869 人关注过本帖
标题:[原创]顺序表的基本操作
只看楼主 加入收藏
hywok1216
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-10-29
收藏
得分:0 
深有感触啊,我也是已经大三了,但是也是什么都不会,唉,心寒啊,以后该怎么找工作.....
2006-10-29 14:49
wangdong1027
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2007-1-20
收藏
得分:0 

自己写的链表的操作:



#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
#define N 5
#include<stdlib.h>
struct student
{long num;
char name[10];
float score;
struct student *next;
};

int n;
struct student *creat(void)
{struct student *p,*q;
int i=0;
n=1;
q=(struct student *)malloc(LEN);
q->num=100;
q->next=NULL;
for(i=1;i<=N;i++)
{p=(struct student *)malloc(LEN);
scanf("%ld,%f,%s",&p->num,&p->score,p->name);
p->next=q->next;
q->next=p;
n++;
}
return(p);
}


void print(struct student *q)
{struct student *p;
printf("there are %d student in the list ,they are:\n",(n-1));
p=q;
if(q!=NULL)
do{
printf("%ld %.2f %s\n",p->num,p->score,p->name);
p=p->next;
}while(p!=NULL);
}


struct student *insert(struct student *q,int i)
{struct student *p,*s;
int j=0;
p=q;
while(p&&j<(i-1)) {p=p->next;j++;}
if(!p||j>i-1) printf("wrong,input again");
else
{ printf("input the student's message which you want to insert:\n");
s=(struct student *)malloc(LEN);
scanf("%ld,%f,%s",&s->num,&s->score,s->name);
s->next=p->next;
p->next=s;
n=n+1;
}
return(q);
}

struct student *search(struct student *q,int i)
{struct student *p;
int j=1;
p=q;
while(p&&j<i)
{p=p->next;
j++;
}
if(!p||j>i)
printf("error!");
else
{printf("the number of %d student's message are:\n",i);
printf("%ld,%.2f,%s\n",p->num,p->score,p->name);}
}

struct student *deleted(struct student *L,int i)
{struct student *p,*q;
int j=0;
p=L;
while(p->next&&j<(i-1))
{p=p->next;
j++;}
if(!(p->next)||j>(i-1)) printf("error!");
q=p->next;
p->next=q->next;
printf("the num of %ld studnet was deleted:\n",q->num);
n=n-1;
}

int main()
{struct student *head,*stu;
int i,j;
printf("**************\n");
printf("**************\n");
for(i=1;i<=4;i++)
{
for(j=1;j<=4-i;j++)
printf(" ");
for(j=1;j<=(2*i-1);j++)
printf("*");
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=1;j<=4-i;j++)
printf(" ");

for(j=1;j<=(2*i-1);j++)
printf("*");

printf("\n");
}
printf("**************\n");
printf("**************\n");

printf("input the student's message:num,score,name(end with 0,0,0)\n");
head=creat();
print(head);
printf("\n\n\n");

printf(" 1-charu\n");
printf(" 2-shanchu\n");
printf(" 3-chazhao\n");
printf(" 4-dayin\n");

printf("input a number(1-4) to make a choose\n");
scanf("%d",&i);
switch(i)
{ case 1:printf("input before which studnet you want to insert:\n");
scanf("%d",&i);
while(i)
{head=insert(head,i);
print(head);
printf("input before which studnet you want to insert:\n");
scanf("%d",&i);
} break;
case 2:printf("which number of the student do you want to deleted:\n");
scanf("%d",&i);
while(i)
{head=deleted(head,i);
print(head);
printf("which number of the stuent do you want to deleted:\n");
scanf("%d",&i);
}
break;
case 3:printf("which number of the student do you want to search:\n");
scanf("%d",&i);
while(i)
{head=search(head,i);
print(head);
printf("which of the studnet do you want to search:\n");
scanf("%d",&i);
} break;
case 4:print(head); break;
default :printf("the number you input are wrong:\n");
}
system("pause");
return 0;
}

2007-01-20 20:50
快速回复:[原创]顺序表的基本操作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014359 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved