单链表中节点类型定义如下:
typedef struct link {
int data;
link* next;
}node,*linklist;
1.将一个头指针为head且不带头节点的单链表改造为一个含头节点且头指针为head的单循环链表。
void fun(linklist& head) {//将一个头指针为head且不带头节点的单链表改造为一个含头节点且头指针为head的单循环链表。
node* s = (node*)malloc(sizeof(node*));//先增加头节点
s->next = head;
head = s;
node* p = head;//用指针p遍历链表
while (p->next!= NULL) {//找到链表的尾节点
p = p->next;
}
p->next = head;
}
2.有两个按元素值递增有序排列的线性表(均不带头节点)将这两个单链表归并为一个按元素值递增次序排列的(不带头节点)单链表
linklist merge_without_head2(linklist la, linklist lb) {
//有两个按元素值递增有序排列的线性表(均不带头节点)将这两个单链表归并为一个按元素值递增次序排列的(不带头节点)单链表
linklist lc = NULL;
node* lce = NULL;//用于指向链表lc的尾节点
while (la && lb) {
if (la->data <= lb->data) {
if (lc == NULL) {//链表起始为空
lc = la;
}
else {
lce->next = la;
}
lce = la;
la = la->next;
}
else {
if (lc == NULL) {
lc = lb;
}
else {
lce->next = lb;
}
lce = lb;
lb = lb->next;
}
}
if (lc == NULL) {//链表a或者链表b为空
if (la == NULL) {
lc = lb;
}
else {
lc = la;
}
}
else {
if (la == NULL) {
lce->next = lb;
}
else {
lce->next = la;
}
}
node* p = lc;
while (lc != NULL) {
cout << lc->data << " ";
lc = lc->next;
}
return lc;
}
3.有两个按元素值递增有序排列的线性表(均不带头节点)将这两个单链表归并为一个按元素值递减次序排列的带头节点单链表
linklist merge_without_head(linklist la, linklist lb) {
//有两个按元素值递增有序排列的线性表(均不带头节点)将这两个单链表归并为一个按元素值递减次序排列的带头节点单链表
node* lc = (node*)malloc(sizeof(node*));//创建一个带头节点的单链表lc
lc->next = NULL;
node* la_tail=NULL;//防止断链
node* lb_tail=NULL;
while (la!=NULL && lb!=NULL) {
if (la->data < lb->data) {
la_tail = la->next;
la->next = lc->next;
lc->next = la;
la = la_tail;
}
else {
lb_tail = lb->next;
lb->next = lc->next;
lc->next = lb;
lb = lb_tail;
}
}
if (la == NULL) {
while (lb != NULL) {
lb_tail = lb->next;
lb->next = lc->next;
lc->next = lb;
lb = lb_tail;
}
}
else {
while (la != NULL) {
la_tail = la->next;
la->next = lc->next;
lc->next = la;
la = la_tail;
}
}
node* p = lc->next;
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
return lc;
}
4.将一个带头节点的单链表A分解为两个带头节点的单链表A和B,使得A表中含有原表中序号为奇数的元素,而B表中含有原表中序号为偶数的元素,且保持其相对顺序不变
void div(linklist& A, linklist& B) {
//将一个带头节点的单链表A分解为两个带头节点的单链表A和B,使得A表中含有原表中序号为奇数的元素,
//而B表中含有原表中序号为偶数的元素,且保持其相对顺序不变
B = (node*)malloc(sizeof(node*));//创建一个头节点;
B->next = NULL;
node* lae = A;//用于指向新链表A的尾节点
node* lbe = B;//用于指向新链表B的尾节点
node* p = A->next;
int t = 1;
while (p != NULL) {
if (t % 2 == 1) {
lae->next = p;
lae = p;
}
else {
lbe->next = p;
lbe = p;
}
p = p->next;//指向下一个节点;
t = t+1;
}
lae->next = NULL;
lbe->next = NULL;
}
5.反向输出链表L(不带头节点)的值(采用递归思想)
void R_p(linklist L) {
//反向输出链表L(不带头节点)的值(采用递归思想)
node* p = L;
if (p->next != NULL) {
R_p(p->next);
}
cout << p->data << " ";
}
6.设L为带头节点的单链表,编写算法实现从尾到头反向输出每个节点的值(头插法建表,遍历)
linklist creat_head(linklist& L) {
//设L为带头节点的单链表,编写算法实现从尾到头反向输出每个节点的值(头插法建表,遍历)
node* p = L->next;
node* pp = NULL;//防止断链,保存p节点后边的节点
L->next = NULL;
while (p != NULL) {
pp = p->next;
p->next = L->next;
L->next = p;
p = pp;
}
return L;
}
7.设计一个递归算法,删除不带头节点的单链表L中所有值为x的节点
void del(linklist& L, int x) {
//设计一个递归算法,删除不带头节点的单链表L中所有值为x的节点
if (L == NULL) {
return;
}
if (L->data == x) {
L = L->next;
del(L,x);
}
del(L->next, x);
}