| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1191 人关注过本帖
标题:这个算法怎么向源程序转换啊
只看楼主 加入收藏
mb01
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-12-20
收藏
得分:0 
我以前的
2006-12-20 16:27
pupilxd
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-12-18
收藏
得分:0 

#include<stdio.h>
#include<malloc.h>

//线性链表上的结点类型
typedef struct node{
int key;
struct node* next;//指向下一个结点
}LNode;

typedef LNode* SQLIST;//链表类型

//构造链表
SQLIST createlist(SQLIST L ,int k){
LNode* flag = NULL;
if(L == NULL){//链表为空
if((L = (LNode*)malloc(sizeof(LNode))) == NULL){
printf("不能分配内存");
}
flag = L;
flag->next = NULL;
flag->key = k;
}
else{
flag = L;
while((flag->next) != NULL){
flag = flag->next;
}
if((flag->next = (LNode*)malloc(sizeof(LNode))) == NULL){
printf("不能分配内存");
}
flag->next->key = k;
flag->next->next = NULL;
flag = flag->next;
}
return L;
}

//查找指定结点,返回匹配结点的位置
int SqSearch(SQLIST L,int aidkey){
int flag = -1;
int count = 1;//初始化为链表的第一个结点标号
LNode* index = L;//指向链表头
while(index != NULL){
if(index->key == aidkey){
flag = count;
}
index = index->next;
count++;
}
return flag;
}

void main(){
SQLIST sq = NULL, head = NULL;
printf("----------create list---------------\n");
for(int i = 0 ; i < 100; i++){
sq = createlist(sq,i+1);
}
head = sq;
//打印链表结点值
for(int j = 0 ; j < 100; j++){
printf("%d ",head->key);
if(j%5 == 0)
printf("\n");
head = head->next;

}
printf("\n----------search list---------------\n");
int flag = SqSearch(sq,50);
printf("所在位置为第 %d 个结点\n",flag);
}

2006-12-21 11:41
pupilxd
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-12-18
收藏
得分:0 
提供了可对链表的增加、插入,删除等操作的支持!!
2006-12-21 11:42
hardward
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2006-10-1
收藏
得分:0 

谢谢你啊,我还是刚学这个东东


我一个人不孤单,想一个人才孤单;有伴的人在狂欢,寂寞的人怎么办?
2007-01-09 22:46
wangdong1027
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2007-1-20
收藏
得分:0 

自己写的线性表的操作
本人试过拉,可以通过运行的:
数据结构的群:16373977:
#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 12:46
快速回复:这个算法怎么向源程序转换啊
数据加载中...
 
   



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

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