2012-05-11 3 views
0

시도한 바를 수행하는 더 좋은 방법이 있다는 것을 알고 있지만 내가 C++을 배우기 위해 사용하고있는 책이며, 내가 계속 진행하기 전에 몇 가지 기본 사항을 이해하는 데 도움이 될 것입니다. 내가 프로그램을 테스트하기 위해 임의의 값을 사용하고링크 된 목록의 구조체를 삭제하는 함수를 구현하려고 시도하기 전에 구조체를 지정하기 전에 구조체를 지정하는 중

#include <iostream> 

using namespace std; 

struct EnemySpaceShip 
{ 
    int weapon_power; 
    int xcoord; 
    int ycoord; 
    EnemySpaceShip *nextEnemy; 
}; 

EnemySpaceShip* getNewEnemy(EnemySpaceShip* p_enemies) // Creates a new EnemySpaceShip in linked list p_enemies 
{ 
    EnemySpaceShip *p_ship = new EnemySpaceShip; 
    p_ship->xcoord = 0; 
    p_ship->ycoord = 0; 
    p_ship->weapon_power = 10; 
    p_ship->nextEnemy = p_enemies; 
    p_enemies = p_ship; 
    return p_ship; 
} 

EnemySpaceShip* findPreRemove(EnemySpaceShip* p_enemies, int x_attack, int y_attack) // finds the element that is before the ship to be removed or returns NULL 
{ 
    EnemySpaceShip *p_current = p_enemies; 
    EnemySpaceShip *initialShip = p_enemies; 
    int i= 0; 
    while (p_current != NULL) 
    { 
     i++; 
     if (p_current->xcoord == x_attack && p_current->ycoord == y_attack) 
     { 
      if (i == 1) 
      { 
       delete initialShip; 
       delete p_current; 
       return NULL; 
      } 
      else 
      { 
       for (int j = 1; j < i - 1; j++) 
       { 
        initialShip = initialShip->nextEnemy; 
       } 
       delete p_current; 
       return initialShip; 
      } 
     } 
     p_current = p_current->nextEnemy; 
    } 
    return NULL; 
} 

EnemySpaceShip* findRemove(EnemySpaceShip* p_enemies, int x_attack, int y_attack) 
{ 
    EnemySpaceShip *p_current = p_enemies; 
    while (p_current != NULL) 
    { 
     if (p_current->xcoord == x_attack && p_current->ycoord == y_attack) 
     { 
      return p_current; 
     } 
     p_current = p_current->nextEnemy; 
    } 
} 

EnemySpaceShip* removeEnemyShip(EnemySpaceShip *p_ship) // deletes the ship parameter and returns the ship after it in the list 
{ 
    EnemySpaceShip *enemyAfterRemove = new EnemySpaceShip; 
    enemyAfterRemove = p_ship->nextEnemy; 
    delete p_ship; 
    return enemyAfterRemove; 
} 

int main() 
{ 
    EnemySpaceShip *p_enemies = NULL; 
    EnemySpaceShip *Ship1 = getNewEnemy(p_enemies); 
    EnemySpaceShip *Ship2 = getNewEnemy(p_enemies); 
    EnemySpaceShip *Ship3 = getNewEnemy(p_enemies); 

    Ship3->xcoord = 5; //arbitrary numbers to test the code 
    Ship3->ycoord = 5; 

    EnemySpaceShip *ShipBeforeRemove = findPreRemove(p_enemies, 5, 5); 
    EnemySpaceShip *ShipToRemove = findRemove(p_enemies, 5, 5); 
    ShipBeforeRemove->nextEnemy = removeEnemyShip(ShipToRemove); 
} 

, 나는 완전히 분명히에 사용되는 것이 게임의 기능으로이를 구현 할 필요가 없습니다 : 어쨌든, 여기 내 코드입니다. 어떤 도움이라도 대단히 감사합니다. 나는 다음과 같은 희망

+0

그래서, – superM

+0

귀하의 구조는 앞으로 점) 질문을 얻을 수 없다 'before this one'-'p_last'와 같은 포인터를 유지하십시오. 양방향 (전후 포인터)으로 만들지 않으면 이전 컨텍스트가없는 요소를 삭제할 수 없습니다. – Greyson

답변

0

은 ... 당신이 단일 연결 목록에서 요소를 제거 ... 원하는 것입니다

EnemySpaceShip* removeFromList(EnemySpaceShip* p_enemies, int x_attack, int y_attack) 
{ 
    if(p_enemies == NULL) 
     return NULL; 

    EnemySpaceShip *p1 = p_enemies; 
    EnemySpaceShip *p2 = p1->next; 

    if(p2 == NULL) 
    { 
     // Trivial case.. 

     if(p1->xcoord == x_attack && p1->ycoord == y_attack) 
     { 
      delete p_enemies; 
      p_enemies = NULL; 
     } 

     return NULL; 
    } 

    while(p2 != NULL) 
    { 
     if(p2->xcoord == x_attack && p2->ycoord == y_attack) 
     { 
      // Element found, remove it and assign its previous pointer 
      // as the next pointer of the deleted one.. 
      p1->next = p2->next; 
      delete p2; 
      return p1; 
     } 
     else 
     { 
      p1 = p2; 
      p2 = p2->next; 
     } 
    } 

    return NULL; 

}

+0

입력에 감사드립니다. 문제는 매개 변수로 제거 된 배를 가져 오는 기능 때문이었습니다. 나는 그 문제가 너무 복잡하고 일종의 미스가 될 필요가없는 상황을 운동의 요점으로 만든다고 생각한다. 당신이 링크 한 코드는 그것을하는보다 현실적인 방법이며, 나는 그 것을 중요하게 받아들이지 않고 그 질문의 중요하지 않은 부분에 매달리는 대신에 계속 움직일 수 있다고 생각한다. 통찰력과 시간을 가져 주셔서 감사합니다. –

+0

@BlakeMadden : 신경 쓰지 마세요 .. :) – Ammar

관련 문제