首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

数据结构-初学者不会初始化

2012-02-24 
数据结构--菜鸟不会初始化!#includeiostreamusingnamespacestdstructseqlist{intlist[max]intlength}

数据结构--菜鸟不会初始化!
#include   <iostream>
using   namespace   std;

struct   seqlist
        {   int   list[max];
            int   length;  
        };   //顺序表的类型
void   insert   (struct   seqlist   *L   ,int   i,int   new_data)//插入一个元素
{   int     j   ;
        if   (L-> length==max)     exit(1)   ;
        else
                {   for(j=L-> length-1;j> =i;j--)
                            L-> list[j+1]=L-> list[j];
                            L-> list[i]=new_data;
                            L-> length++;  
                }
}
void     PrintList(seqlist   *list)//输出函数  
{    
            int       i;    
            printf( "\nNOW       begin       to       print       :\n ");    
            for(i=0;i <list-> length;i++)
            {    
                    if(i%8==0)printf( "\n ");    
                    printf( "%5d ",list-> list[i]);    
            }    
            printf( "\n\nThe   program   has   printed   the   result.\n ");    
}  
int       main()
{           int   location,value;//location表示插入位置,value表示插入值    
            printf( "\nThis   is   a   list   program.\n ");
           
                        ????????
            printf( "\nInput   the   location?\n ");    
            scanf( "%d ",&location);    
            printf( "\nInput   the   value?\n ");    
            scanf( "%d ",&value);    
            insert(list,location,value);    
            PrintList(list);    
            getchar();    
}      
我太穷了,先给点哈,以后有了再给大家补起,多谢!!!

[解决办法]
#include <stdio.h>
#include <stdlib.h>


#define MAX 30

typedef struct {
int array [MAX];
int last;
} Line;


void create_line (Line *A);
void del_elem(Line *A, int elem);
void insert_elem(Line *A, int item, int elem);


int max_elem(Line *A);
void select_elem (Line *A,int elem);
void disp (Line *A);


main ()
{
int Max;
int item;
int elem;
Line L;
create_line (&L);
disp(&L);
printf ( "input want insert item: " );
scanf ( "%d ",&item);
printf ( "input a number insert the array: ");
scanf ( "%d ",&elem);
insert_elem(&L,item,elem);
disp(&L);
printf ( "input want delete elem 's value: ");
scanf ( "%d ",&elem);
del_elem(&L,elem);
disp(&L);
printf ( "input want select elem 's value: ");
scanf ( "%d ",&elem);
select_elem (&L,elem);
Max=max_elem(&L);
printf ( "Max elem is :%d\n ",Max);
}

void disp (Line *A)
{
int i;
for (i=0;i <A-> last;i++)
{
printf ( "%d ",A-> array[i]);
if ((i+1)==10)
printf ( "\n ");
}
printf ( "\n ");
}

void create_line (Line *A)
{
int i,n;
A-> last=0;
n=rand()%30+1;
for (i=0;i <n;i++)
{
// srand((unsigned)time(NULL));
A-> array[i] = (rand()%20)+1;
A-> last=i+1;
}
}

void del_elem(Line *A, int elem)
{
int i,j;
if (A-> last==0)
{
printf ( "The array is null! ");

}
else
for (i=0;i <A-> last;i++)
{
if (A-> array[i]==elem)
{
for (j=i;j <A-> last;j++)
A-> array[j]=A-> array[j+1];
A-> last--;
i--;
}
}

}

void insert_elem(Line *A, int item, int elem)
{
int i;
if (A-> last==MAX)
{
printf ( "The array is full! ");
}
if (item> =MAX||item <=0)
{
printf ( "Input error! ");
}
else
{
for (i=(A-> last+1);i> =item;i--)
{
A-> array[i]=A-> array[i-1];
}
A-> array[i]=elem;
A-> last++;
}
}

void select_elem (Line *A,int elem)
{
int i;
if (A-> last==0)
{
printf ( "The array is null! ");
}
else
{
for (i=0;i <A-> last;i++)
{
if (A-> array[i]==elem)
printf ( "Have %dth elem equal to %d\n ",i+1,elem);
}
}
}

int max_elem(Line *A)
{
int i,Max,t;
if (A-> last==0)
{
printf ( "The array is null! ");
return -1;
}
else
{
Max=A-> array[0];
for (i=1;i <A-> last;i++)
{
if (A-> array[i]> Max)
{
Max=A-> array[i];
t=i;
}
}
}
printf ( "the %dth is max_elem_number!\n ",t+1);
return Max;
}

热点排行