已知不带头结点的单链表L,下面用函数实现的在第一个元素前面插入值为x的元素结点的算法错误的是( )
A: void insert(List *L,elemtype x){ node d=new node(x); d.next=*L; *L=&d;}
B: List * insert(List *L,elemtype x){ node d=new node(x); d.next=*L; *L=&d; return L;}
C: void insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; p->next=q; L=&q;}
D: List insert(List *L,elemtype x){ node d=new node(x); d.next=*L; return &d;}
A: void insert(List *L,elemtype x){ node d=new node(x); d.next=*L; *L=&d;}
B: List * insert(List *L,elemtype x){ node d=new node(x); d.next=*L; *L=&d; return L;}
C: void insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; p->next=q; L=&q;}
D: List insert(List *L,elemtype x){ node d=new node(x); d.next=*L; return &d;}
举一反三
- 已知不带头结点的单链表L,下面用函数实现的在第一个元素前面插入值为x的元素结点的算法错误的是( ) A: void insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; q->next=p; L=&q;} B: List * insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; q->next=p; L=&q; return L;} C: void insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; p->next=q; L=&q;} D: List * insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; q->next=p; return &q;}
- 已知不带头结点的单链表L,下面用函数实现的在第一个元素前面插入值为x的元素结点的算法错误的是( ) A: void insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; q->next=p; L=&q;} B: List * insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; q->next=p; L=&q; return L;} C: void insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; p->next=q; L=&q;} D: List * insert(List *L,elemtype x){ ptr p=*L; node d=new node(x); ptr q=&d; q->next=p; return &q;}
- 函数功能: 在带头结点单链表中删除一个值为x的结点,函数返回值为头指针。请选择正确的选项链式表定义如下:typedef int datatype;typedef struct link_node{ datatype info; struct link_node *next; }node;函数实现如下:node *dele(node *head,datatype x) { node *pre= (1) ,*q; q=head->next; while( (2) ) { pre=q; q=q->next; } if(q) { pre->next=q->next; //删除q free(p); } return head; }
- 下列算法的功能是:删除单链表L(含两个或两个以上的数据结点)中第一个值为x的结点的前驱结点。请在空白处选择正确的语句。 int deleteFirstX(LinkList &L, ElemType x) { LinkList prepre = L, pre = prepre->next, p; if (pre->data == x) return 0; p = pre->next; while (p != NULL && p->data != x) { prepre = pre; ____________________; p = p->next; } if (p != NULL) { prepre->next = p; free(pre); return 1; } else return 0; } A: pre = p B: prepre->next = p C: p = pre->next D: p = prepre->next
- 对于带头节点的单链表L1,其节点类型为LinkList,指出以下算法的功能。 void fun(LinkList *&L,ElemType x,ElemType y) { LinkList *p=L->next; while (p!=NULL) { if (p->data==x) p->data=y; p=p->next; } }