keild 2022-03-28 15:28 采纳率: 85.7%
浏览 59
已结题

这里数据结构 哪里有问题

#include "LinearList.h"

int main(int argc, char* argv[])
{
SqList List;
int i;

//
// Initialize the linear table
//
List.nLength = 8;
for (i=0; i<List.nLength; i++)
{
    List.Elements[i] = i;
}

//
// Insert the element before the ith element
//
InsertBefore(&List, 18, 4);    
InsertBefore(&List, 20, 11);    // Invalid insert position. Insert failed.

for (i=0; i<List.nLength; i++)
{
    printf("%d ", List.Elements[i]);
}

printf("\n");

return 0;

}

/*
function:
Insert an element before the ith element.

parameter:
pList -- Linear table
Elem -- Inserted element
i -- The specified location.Count from 1.

returned value:
Returns 1 if insert succeeds
Returns 0 if insert fails
/
int InsertBefore(SqList
pList, ElemType Elem, int i)
{
int nIndex; // The cursor used to move elements

//
// TODO: Add the code here
//
if((*pList).nLength >= MAX_LENGTH)
    return 0;
    //判断插入位置是否正确
if(i < 1 || i) > (*pList).nLength)
    return 0;
    //初始化游标位置
nIndex = (*pList).nLength;
    //需要循环复制元素
return 0;

}

//声明部分
#ifndef LINEARLIST_H_
#define LINEARLIST_H_

#include <stdio.h>

#define MAX_LENGTH 20 // The maximum length of a linear table

typedef int ElemType; // The types of elements in a linear table

typedef struct SqListElem
{
// An array is used to store elements in a linear table
// whose maximum length is the length of the array.
ElemType Elements[MAX_LENGTH];
// The actual length of the linear table, the number of elements in the linear table.
int nLength;
}SqList;

int InsertBefore(SqList* pList, ElemType Elem, int i);

#endif

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-03-28 17:16
    关注

    int InsertBefore(SqList pList, ElemType Elem, int i) 函数的问题,修改如下,供参考:

    int InsertBefore(SqList* pList, ElemType Elem, int i)   //修改
    {
        int nIndex; // The cursor used to move elements
    
        //
        // TODO: Add the code here
        //
        if ((*pList).nLength >= MAX_LENGTH) //修改
            return 0;
        //判断插入位置是否正确
        if (i < 1 || i > (*pList).nLength)  //修改
        return 0;
        //初始化游标位置
        nIndex = (*pList).nLength;
        //需要循环复制元素
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月13日
  • 已采纳回答 4月5日
  • 创建了问题 3月28日