关于初始化顺序表,程序崩溃问题

下面程序是错误程序


/**********************************************************、
** 文件: MergeSeqList.c 
**问题描述:利用顺序存储实现线性表的功能 
**          2、已有两个升序排列顺序表AB,融合新的顺序表C 
**********************************************************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define MAXSIZE 100 typedef int  DataType;

/* 定义顺序表为一个结构体*/ typedef struct node{
    int data[MAXSIZE];
    DataType length;             }SeqList,*PseqList;

/********顺序表初始化***************
** 函数名; Init_SeqList
** 入口参数: 无
** 返回值; 指向顺序表的指针  
** 作用:初始化顺序表 
**  
*************************************/ PseqList Init_SeqList(){ 
     PseqList PL;
     PL= (PseqList)malloc(sizeof(SeqList)); //也可以通过PL = &L实现  

     if(PL)
         PL->length = 0;
     return PL;
      } 

/********冒泡排序***************
** 函数名; Bubble_Sort
** 入口参数: 顺序表,数据个数 
** 返回值;  空 
** 作用:将顺序表排序 
** 平均时间复杂度为 O(n^2);  
*************************************/ void Bubble_Sort(PseqList PL, int n) {
    int i, j, temp;
    for (j = 0; j < n - 1; j++)
        for (i = 0; i < n - 1 - j; i++)
        {
            if(PL->data[i] > PL->data[i + 1])
            {
                temp = PL->data[i];
                PL->data[i] = PL->data[i + 1];
                PL->data[i + 1] = temp;
            }
        } }

/********AB融合为C排序****************
** 函数名; Merge_SeqList 
** 入口参数: 顺序表A,顺序表B,
**           顺序表C的二级指针 
** 返回值;  0 失败 ;1 成功 
** 作用:AB融合为C排序 
** 平均时间复杂度为 O(m+n); m为A的长度 
                            n为B的长度  
*************************************/ int Merge_SeqList(PseqList A, PseqList B,PseqList *C) {    int i = 0,j = 0,k = 0;
       if(!(*C)){
       printf("C表示不存在");
       return 0;            }    if( A->length + B->length >= MAXSIZE ){
       printf("C表空间不足");
       return 0;        }    printf("\nC表示不存在");    while(i < A->length && j < B->length){
       if(A->data[i] < B->data[j])
           (*C)->data[k++] = A->data[i++];
       else
           (*C)->data[k++] = B->data[j++];     }   
      while(i < A->length)
       (*C)->data[k++] = A->data[i++];    while(j < B->length)
       (*C)->data[k++] = B->data[j++];  
           (*C)->length = k;    return 1;           }

/*******MAIN主函数***************
** 函数名; main
** 入口参数: 无
** 返回值; 1  
** 作用:函数入口 
**  
*************************************/ int main(){
    int i,statu;

    PseqList PL1,PL2,*PL;

    PL1 = Init_SeqList();
    PL2 = Init_SeqList(); 

    PL1->length = 9;
    PL2->length = 12;  


    for(i=0; i < PL1->length ; i++){
        PL1->data[i] = rand()%100+1;;  
    } 

    printf("\n------------------------------\n");       Bubble_Sort(PL1,PL1->length);
    for(i=0; i < PL1->length; i++)
      printf("%d\t",PL1->data[i] );


        for(i=0; i < PL2->length ; i++){
        PL2->data[i] = rand()%100+1;;
    }
    printf("\n-------------------------------\n");       Bubble_Sort(PL2,PL2->length);
    for(i=0; i < PL2->length; i++)
      printf("%d\t",PL2->data[i] );


    **(*PL) = Init_SeqList();**

    statu = Merge_SeqList(PL1,PL2,PL);



    getch();
    return 0;     }

原因是*PL未进行初始化,要先为指针分配一片内存或者让指针指向一个已有的对象,再去解引指针。
修改为:
PseqList pp = Init_SeqList();
PL = &pp;

猜你喜欢

转载自blog.csdn.net/sinat_30990593/article/details/52234008