環狀串列(資料結構)~~~急?

(建立的那邊意思是如果n是5就會自動建立1>2>3>4>5這樣的串列

typedef struct node {

struct node *next; // pointer to left subtree

int value; // node value

}node;

node * create(int n) {

node *temp = NULL;

node *current = NULL;

for (int j = 1; j <= n; j++) {

temp = (node*)malloc(sizeof(node));

temp->value = j;

temp->next = NULL;

}

else {

while (current->next != NULL) {

current = current->next;

}

current->next = temp;

}

}

}

void delete(node * head,int m) {

node * current, *del;

for (int i = 1; i < m - 1;i++) {

current = current->next;

}

del = current->next;

current->next = del->next;

free(del);

}

1 個解答

• 4 年前
最佳解答

增加的幫你標上/*new*/

typedef struct node {

struct node *next; // pointer to left subtree

int value; // node value

}node;

node * create(int n) {

node *temp = NULL;

node *current = NULL;

for (int j = 1; j <= n; j++) {

temp = new node();

temp->value = j;

temp->next = NULL;

}

else {

while (current->next != NULL) {

current = current->next;

}

current->next = temp;

}

}

}

/*new*/node* mdelete(node * head,int m) {

node * current, *del;

for (int i = 1; i < m-1 ;i++) {

current = current->next;

}

/*new*/if(m == 1) while (current->next != head) current = current->next;

del = current->next;

current->next = del->next;

delete(del);

}

while (current->next != NULL) {

cout<<" "<<current->value;

current = current->next;

}

cout<<endl;

}