- 设L是一个带头结点的非递减有序单链表的表头指针,试设计一个算法,将元素e插入到链表L中的合适地方,使得该链表仍是递减有序。
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void InsertElement(LNode *L , int e){
LNode *node = (LNode*)malloc(sizeof(LNode));
node->data = e;
node->next = NULL;
LNode *p = L;
while(p->next != NULL && p->next->data <= e){
p = p->next;
}
node->next = p->next;
p->next = node;
}
- 有一个带头结点的单链表L = {a1 , b1 , a2 , b2 , … , an , bn}, 请设计一个函数将其拆分成两个带头结点的单链表A和B,正序链表A = {a1 , a2 , … , an}, 逆序链表 B = {bn , bn-1, … , b1}。要求链表A使用L的头结点。
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void DivideList(LNode *L , LNode *&A , LNode *&B){
A = L;
B = (LNode*)malloc(sizeof(LNode));
B->next = NULL;
LNode *p = L->next;
while(p != NULL && p->next != NULL){
LNode *q = p->next;
q->next = B->next;
B->next = q;
p = p->next;
}
}
- 假设用单链表方式来存储整数序列,如下形式:
0 --> 3 --> 0 --> 3 --> 1 --> 3
请你编写一个递归算法,对这样的链表进行操作,重复结点(值相同的结点)仅保留排在最前面的一个,最后返回新链表的首地址。例如,若上述链表,处理后的新链表如下:
0 --> 3 --> 1
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
LNode* DelRepeatNode(LNode *head){
if(head == NULL)
return NULL;
DelRepeatNode(head->next);
LNode *temp = head;
while(temp->next != NULL){
if(temp->next->data == head->data){
LNode *p = temp->next;
temp->next = p->next;
free(p);
}
temp = temp->next;
}
return head;
}
- 设有一个带头结点的单链表,一次存储了元素a1,b1,a2,b2,…,an,bn。写出该单链表的数据结构,并编写函数调整链表元素的顺序为an,an-1,…,a1,b1,b2,…,bn,要求不用另外的数组结点完成,写出算法思想和代码注释。
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void AjustList(LNode *L){
int i = 0;
LNode *p = L , *q = NULL;
while(p->next != NULL){
if(!(i & 1)){
q = p->next;
p->next = q->next;
q->next = L->next;
L->next = q;
}
++i;
p = p->next;
}
}
- 试设计一个结点数据类型为整型的带表头结点的有序单链表,然后设计一个算法,该算法将这个有序单链表划分成两个单链表,使得第一个单链表中包含原单链表中所有数值为奇数的结点,第二个单链表中包含原单链表中所有数值为偶数的结点,且两个单链表中结点的相对排列顺序与原单链表中相同。注意:要求使用原单链表的空间,表头结点可以另辟空间。
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void SplitList(LNode *L , LNode*& A , LNode*& B){
A = L;
LNode *p = L , *q = B;
LNode *temp = NULL;
while(p->next != NULL){
if(p->next->data & 1 == 0){
temp = p->next;
p->next = temp->next;
temp->next = q->next;
q->next = temp;
q = temp;
}
else{
p = p->next;
}
}
}
- 试设计一个带头结点的单链表,然后写一个算法将该单链表逆转,要求利用原表结点空间,不允许申请和使用新的结点空间。
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void ReverseList(LNode *L){
LNode *p = L->next;
L->next = NULL;
while(p != NULL){
LNode *q = p;
p = p->next;
q->next = L->next;
L->next = q;
}
}
- 在带头结点的非空线性链表中,试设计一算法,将链表中数据域值最小的那个结点移到链表的最前面,其余各结点的顺序保持不变。要求:不得额外申请新的链结点。
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void MoveMinLNode(LNode *L){
LNode *preNode = L , *p = L;
LNode *minNode = NULL;
while(p->next != NULL){
if(p->next->data < preNode->next->data)
preNode = p;
p = p->next;
}
if(preNode != L){
minNode = preNode->next;
preNode->next = minNode->next;
minNode->next = L->next;
L->next = minNode;
}
}
- 有一个单链表,其结点的元素值以递增顺序排列,给出数据结构,并编写一个算法删除该单链表中元素值相同的结点。
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void DelSameNode(LNode *L){
LNode *p = L;
while(p->next != NULL){
LNode *q = p->next;
while(q->next != NULL && q->next->data == q->data){
LNode *temp = q->next;
q->next = temp->next;
free(temp);
}
p = p->next;
}
}