第7章19题关键程序

int Delete(node* head, int index, int length)

// 删除函数, index从1开始

// 返回一个表示操作成功与否的数

// 注意不能使用delete作为函数名

{

      int i = 0;                  // i 是for循环的指示器

 

      if (head == NULL)

      {

            printf("原列表为空!n");

            return -1;

      }

 

      if (index <= 0)

      {

            printf("超出字符串下界!n");

            return -1;

      }

 

      if (index > head->length)

      {

            printf("超出字符串上界!n");

            return -1;

      }

 

      if (length <= 0)

      {

            printf("长度不正确!n");

            return -1;

      }

 

      if (i + length – 1 >= head->length)

      {

 

            printf("提示:字符串长度不足,将删除第%d个元素之后的所有元素!n", index – 1);

 

            for (i= index – 1; i < head->length; i ++ )

            {

                  head->data[i] = ”;

            }

            head->length = index – 1;

 

            return 0;

      }

      // 长度不足,将第index个元素之后全部清空

 

      for (i = 0; i < head->length – (index) – length; i++)

      {

            head->data[i + index – 1] = head->data[i + index + length- 1];

      }

      head->length -= length;

      // 长度充足,将第i + index + length个元素前移length位

 

      return 0;

}

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: