#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