当矩阵实现为C中的列表时,重新排列矩阵行

时间:2013-04-18 22:41:00

标签: c list matrix

我的任务是使用C给出两个简单规则来重新排列矩阵的行。 1.如果最后一列中的数字为正数,则该行应移至顶部。 2.如果最后一列中的数字为零,则该行应移至底部。

例如,如果我有{{1,2,0},{4,5,-6},{7,8,9},{1,1,1}}我会{{1,1} ,1},{7,8,9},{4,5,-6},{1,2,0}}

为此,我将矩阵实现为单链接列表,其中节点表示矩阵的行。然后我创建了一个名为“重新排列”的函数,它应该为我完成任务。我已经能够为所有情况解决问题,除非第一行的最后一列中的数字为0.然后我得到的全部是0.例如{{0} {2} {3}}或{ {1,2,0},{4,5,6} {7,8,9}}都给出0.如果有人可以查看我的代码并且可能给我输入som,我会很高兴。这是我的代码:

typedef struct node node;
typedef struct list list;

struct list{
node* first;
node* last;
};

struct node{                                                               
  int* data;        
  node* next;       
};      

list* newList(int* data);   
static node* newNode(int* data);       
list* newList(int* data);                                              
void insertFirst(list* head, int* data);
void insertLast(list* head, int* data);
void rearrange(list* head,int rows, int cols);
void printList(list* head,int cols);


int main(){
int a[1] = {0};
int b[1] = {-2};
int c[1] = {-3};
//int d[1] = {-4}; // if we want more rows
//int e[1] = {-5};
//Create empty list
list* head = newList(a);
//Add rows
insertLast(head,b);
insertLast(head,c);
//insertLast(head,d);
//insertLast(head,e);
rearrange(head,3,1);
//Print all elements
printList(head,1);
return 0;
}

void printList(list* head,int cols){
int i;
node* currentEl = head->first;
while(currentEl != NULL) {
    for(i=0;i<cols;i++){
        printf("%d, ",currentEl->data[i]);
    }
    printf("\n");
    currentEl = currentEl->next;
}
}

void rearrange(list* head,int rows,int cols){
head->last->next = head->first;
node* currentEl = head->last;
node* nextEl = head->first;
node* temp;
int count = 1;

while(count != rows+1){
    int a = nextEl->data[cols-1];
    temp = nextEl->next;
    if(a>0 && count != 1){
        nextEl->next = head->first; 
        head->first = nextEl;
        currentEl->next = temp; 
    }else if(a==0 && head->first->data[cols-1] != 0){ 
        head->last->next = nextEl;
        head->last = nextEl;
        nextEl->next = NULL;
        currentEl->next = temp; 
     }else if(a==0 && head->first->data[cols-1] == 0){ // this needs to be changed
        head->last->next = nextEl;
        head->last = nextEl;
        nextEl->next = NULL;
        currentEl->next = temp;
     }else {
        currentEl = nextEl;
    }
    //nextEl = nextEl->next; 
    nextEl = currentEl->next;
    if(count == 1){
        head->last->next = NULL;
    }
    count++;
}

}

void insertFirst(list* head, int* data){
node* temp;
temp = newNode(data);
temp->next = head->first;
head->first = temp;

}

void insertLast(list* head, int* data){
node* temp;
temp = newNode(data);
head->last->next = temp;
head->last = temp;
}

static node* newNode(int* data){
node* new;
new = (node*)malloc(sizeof(node));
new->next = NULL;
new->data = data;
return new;
}

list* newList(int* data){
list* head;
node* node = newNode(data);
head = (list*)malloc(sizeof(list));
head->first = node;
head->last = node;
return head;
}

1 个答案:

答案 0 :(得分:0)

不确定这是否是您所需要的,但以下代码将转为:  {{1,2,0},{4,5,-6},{7,8,9},{1,1,1}} {{1,1,1},{7,8,9},{4,5,-6},{1,2,0}} {{1,2,0},{4,5,-6},{7,8,9},{1,1,1}} 但是,您需要将此C#代码转换为C,并使用1D数组而不是2D数组。即代替{1,2,0,4,5,-6,7,8,9,1,1,1},您只需elementsPerGroup。 您的案例中的 private static byte[] ReverseArray(int elementsPerGroup, byte[] forwardsArray) { int length = forwardsArray.Length; byte[] reversedArray = new byte[length]; int groupIdentifier = 0; for (int i = 0; i < length; i++) { if (i != 0 && i % elementsPerGroup == 0) { groupIdentifier += 2 * elementsPerGroup; } int index = length - elementsPerGroup - groupIdentifier + i; reversedArray[i] = forwardsArray[index]; } return reversedArray; } 为3。

{{1}}