程序填空题:Delete a node from a linked list
The program below contains the **finding** and **deleting** operations of the Linked List **without** dummy head node.
The definition of the linked list is:
```
struct Node{
int data;
struct Node *next;
};
```
The nodes of linked list are numbered sequentially from `1`. (It means we count the nodes as `1,2,3,...` not `0,1,2,...`)
The functions are described as:
`struct Node* findByIndex(struct Node *head, int index)` ,search for the node whose index number is `index` and return its **address**. Return `NULL` if failed.
`int deleteByIndex(struct Node **phead, int index, int *pitem)` , copy the value of node whose index number is `index` to the variable which `pitem` points to, and then delete the node. Return `1` if succeeded and `0` if failed.
e.g. for a linked list **head->12->34->56** ,
`findByIndex(head, 2)` will return the address of node `34`, and `deleteByIndex(&head, 2, &item)` wil make the list become **head->12->56** and copy `34` into variable `item`.
Fill in the blanks to complete the function:
```c++
#include
#include
struct Node{
int data;
struct Node *next;
};
struct Node* initList(){ return NULL; }
void printList(struct Node* head){
/* details omitted */
}
struct Node* createNode(int data){
struct Node *p;
p = (struct Node*)malloc(sizeof(struct Node));
if(!p) return NULL;
p->data = data;
p->next = NULL;
return p;
};
struct Node* findByIndex(struct Node *head, int index){
int i;
if(head==NULL || index < 1) return NULL;
for(i=1; @@[ head && inext,i++);
return head;
}
int deleteByIndex(struct Node **phead, int index, int *pitem){
struct Node *tmp;
if(*phead==NULL || index < 1) return 0;
else if(index == 1){
tmp = *phead;
*phead = @@[ tmp->next ](1) ;
}
else{
struct Node *p;
p = findByIndex(*phead, @@[ index-1](1) );
if(@@[ !p || p->next==NULL ](2)) return 0;
tmp = @@[ p->next ](1) ;
@@[ p->next ](1) = tmp->next;
}
*pitem = tmp->data;
free(tmp);
return 1;
}
```
答案:
第1空: head && i
第2空: tmp->next
第3空: index-1
第4空: !p || p->next==NULL
第5空: p->next
第6空: p->next
The definition of the linked list is:
```
struct Node{
int data;
struct Node *next;
};
```
The nodes of linked list are numbered sequentially from `1`. (It means we count the nodes as `1,2,3,...` not `0,1,2,...`)
The functions are described as:
`struct Node* findByIndex(struct Node *head, int index)` ,search for the node whose index number is `index` and return its **address**. Return `NULL` if failed.
`int deleteByIndex(struct Node **phead, int index, int *pitem)` , copy the value of node whose index number is `index` to the variable which `pitem` points to, and then delete the node. Return `1` if succeeded and `0` if failed.
e.g. for a linked list **head->12->34->56** ,
`findByIndex(head, 2)` will return the address of node `34`, and `deleteByIndex(&head, 2, &item)` wil make the list become **head->12->56** and copy `34` into variable `item`.
Fill in the blanks to complete the function:
```c++
#include
#include
struct Node{
int data;
struct Node *next;
};
struct Node* initList(){ return NULL; }
void printList(struct Node* head){
/* details omitted */
}
struct Node* createNode(int data){
struct Node *p;
p = (struct Node*)malloc(sizeof(struct Node));
if(!p) return NULL;
p->data = data;
p->next = NULL;
return p;
};
struct Node* findByIndex(struct Node *head, int index){
int i;
if(head==NULL || index < 1) return NULL;
for(i=1; @@[ head && i
return head;
}
int deleteByIndex(struct Node **phead, int index, int *pitem){
struct Node *tmp;
if(*phead==NULL || index < 1) return 0;
else if(index == 1){
tmp = *phead;
*phead = @@[ tmp->next ](1) ;
}
else{
struct Node *p;
p = findByIndex(*phead, @@[ index-1](1) );
if(@@[ !p || p->next==NULL ](2)) return 0;
tmp = @@[ p->next ](1) ;
@@[ p->next ](1) = tmp->next;
}
*pitem = tmp->data;
free(tmp);
return 1;
}
```
答案:
第1空: head && i
第2空: tmp->next
第3空: index-1
第4空: !p || p->next==NULL
第5空: p->next
第6空: p->next