该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
实现代码
#include
#include
#include
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
typedef int ElementType;
struct Node{
Position previous;
ElementType element;
Position next;
};
void InitList(List l, int iNum);
void OutPeople(List l, int iCount, int iPeopleNum);
void printfList(List l, int i);
int main(int argc, char **argv){
int iPeopleNum, iDeliverCount;
List l = (List)malloc(sizeof(struct Node));
if(argc != 3){
perror("input parameters error");
exit(2);
}
iPeopleNum = atoi(argv[1]);
iDeliverCount = atoi(argv[2]);
InitList(l, iPeopleNum);
printfList(l, iPeopleNum);
OutPeople(l, iDeliverCount, iPeopleNum);
printfList(l, 5);
return 0;
}
void InitList(List l, int iNum){
List tmp = l;
Position pos = tmp, BeginPos, LastPos;
PtrToNode node;
int i;
pos->previous = NULL;
pos->element = 1;
pos->next = NULL;
for(i = 1; i < iNum; i++){
node = (PtrToNode)malloc(sizeof(struct Node));
node->element = i+1;
node->next = NULL;
node->previous = pos;
pos->next = node;
pos = pos->next;
}
BeginPos = tmp;
pos = tmp;
while(pos->next != NULL){
pos = pos->next;
}
LastPos = pos;
LastPos->next = BeginPos;
BeginPos->previous = LastPos;
}
void OutPeople(List l, int iCount, int iPeopleNum){
List tmp = l;
Position pos = tmp;
Position PreviousPos, NextPos;
int i, j;
for(j = 0; j < iPeopleNum - 1; j++){
for(i = 0; i <= iCount; i++){
PreviousPos = pos->previous;
NextPos = pos->next;
if(i == iCount){
PreviousPos->next = NextPos;
NextPos->previous = PreviousPos;
printf("out element %d\n", pos->element);
free(pos);
pos = NextPos;
}else{
pos = pos->next;
}
}
}
}
void printfList(List l, int n){
List tmp = l;
Position pos = tmp;
printf("has %d elements\n", n);
int i;
printf("element: %d, next element: %d\n", pos->element, pos->next->element);
for(i = 0; i < n; i++){
printf("%d ", pos->element);
pos = pos->next;
}
printf("\n");
}