1.单链表统计负数个数
要求实现一个函数,返回带头结点的单链表中负整数的个数。
函数接口定义:
int NegativeInt(LinkList L);
L是带头结点的单链表的头指针,函数NegativeInt返回L中负整数的个数。如果单链表为空,返回0。
其中LinkList结构定义如下:
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
输入格式:输入数据为1行,给出以-1结束的单链表元素(-1不属于单链表元素),所有数据之间用空格分隔。
输出格式:输出一个整数,表示负数个数。
代码如下:
#include<stdio.h>
#include<stdlib.h>typedef int DataType;
struct Node
{DataType data; struct Node* next;
};
typedef struct Node* PNode;
typedef struct Node* LinkList; LinkList SetNullList_Link()
{LinkList head = (LinkList)malloc(sizeof(struct Node));if (head != NULL) head->next = NULL;else printf("alloc failure");return head;
}void CreateList_Tail(PNode head)
{PNode p = NULL;PNode q = head;DataType data;scanf("%d", &data);while (data != -1){ p = (struct Node*)malloc(sizeof(struct Node));p->data = data;p->next = NULL;q->next = p;q = p;scanf("%d", &data);}
}void print(LinkList head)
{PNode p = head->next;int i;while (p){if(p->data<0)//返回data的值 {i++;}p = p->next;//将next的值赋给p }printf("NegativeInt=%d ",i++);
}
void DestoryList_Link(LinkList head)
{PNode pre = head; PNode p = pre->next;while (p){free(pre);pre = p;p = pre->next;}free(pre);
}int main()
{LinkList head = NULL;head = SetNullList_Link();CreateList_Tail(head);/*To Do List*/print(head);DestoryList_Link(head);return 0;
}
2.移动链表中的最大值到尾部
编写函数MoveMaxToTail(),实现查找单链表中值最大的结点,并将其移动到链表尾部,注意其他结点的相对次序不变。要求尽量具有较高的时间效率。
函数接口定义:
void MoveMaxToTail (LinkList H );
typedef int DataType;
struct Node
{
DataType data;
struct Node* next;
};
typedef struct Node *PNode;
typedef struct Node *LinkList;
输入格式:8 12 46 30 5
输出格式:8 12 30 5 46
代码如下:
#include<stdio.h>
#include<stdlib.h>typedef int DataType;
struct Node
{DataType data; struct Node* next;
};
typedef struct Node* PNode;
typedef struct Node* LinkList; LinkList SetNullList_Link()
{LinkList head = (LinkList)malloc(sizeof(struct Node));if (head != NULL) head->next = NULL;else printf("alloc failure");return head;
}void CreateList_Tail(PNode head)
{PNode p = NULL;PNode q = head;DataType data;scanf("%d", &data);while (data != -1){ p = (struct Node*)malloc(sizeof(struct Node));p->data = data;p->next = NULL;q->next = p;q = p;scanf("%d", &data);}
}void MoveMaxToTail(LinkList head)
{LinkList s = head; // 当前节点的前一个节点指针LinkList p = head->next; // 当前节点指针/定义两个指针p,s LinkList max = p; // 最大值节点指针LinkList maxPrev = head; // 最大值节点的前一个节点指针// 用于保存 ,找到最大值节点和其前一个节点while (p != NULL){if (max->data < p->data){max = p;//最大节点 maxPrev = s;//最大节点前驱 }s = p;//更新前驱节点 p = p->next;}// 如果最大值节点已经在链表尾部,则无需移动if (max->next == NULL){return;}// 将最大值节点从链表中删除maxPrev->next = max->next;// 将最大值节点移动到链表尾部p = head;while (p->next != NULL){p = p->next;}p->next = max;//max节点添加 max->next = NULL;
}void print(LinkList head)
{PNode p = head->next;while (p){printf("%d ", p->data);p = p->next;}
}
void DestoryList_Link(LinkList head)
{PNode pre = head; PNode p = pre->next;while (p){free(pre);pre = p;p = pre->next;}free(pre);
}int main()
{LinkList head = NULL;head = SetNullList_Link();CreateList_Tail(head);MoveMaxToTail( head);//实现查找单链表中值最大的结点,并将其移动到链表尾部,注意其他结点的相对次序不变/*To Do List*/print(head);DestoryList_Link(head);return 0;
}
3.删除单链表的重复结点
实现一个函数,pur_LinkList(LinkList L)函数是删除带头结点单链表的重复结点。
函数接口定义:
void pur_LinkList(LinkList L);
其中L是用户传入的参数。L是带头结点单链表的头指针。
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}LNode, *LinkList;
输入格式:1 2 3 3 3 4 5 6 7 8 -1
输出格式:1 2 3 4 5 6 7 8
代码如下:
#include<stdio.h>
#include<stdlib.h>typedef int DataType;
struct Node
{DataType data; struct Node* next;
};
typedef struct Node* PNode;
typedef struct Node* LinkList; LinkList SetNullList_Link()
{LinkList head = (LinkList)malloc(sizeof(struct Node));if (head != NULL) head->next = NULL;else printf("alloc failure");return head;
}void CreateList_Tail(PNode head)
{PNode p = NULL;PNode q = head;DataType data;scanf("%d", &data);while (data != -1){ p = (struct Node*)malloc(sizeof(struct Node));p->data = data;p->next = NULL;q->next = p;q = p;scanf("%d", &data);}
}void pur_LinkList(LinkList L)
{LinkList p = L->next; // p为当前节点LinkList q = L; // q为p的前驱节点LinkList x; while (p != NULL){q = p;x = q->next;while (x != NULL){if(x->data == p->data){q->next = x->next;}else{q = q->next;}x = x->next;}p =p->next;}
}void print(LinkList head)
{PNode p = head->next;while (p){printf("%d ", p->data);p = p->next;}
}
void DestoryList_Link(LinkList head)
{PNode pre = head; PNode p = pre->next;while (p){free(pre);pre = p;p = pre->next;}free(pre);
}int main()
{LinkList head = NULL;head = SetNullList_Link();CreateList_Tail(head);pur_LinkList(head);/*To Do List*/print(head);DestoryList_Link(head);return 0;
}
4.链表合并
有两个单链表A、B,其中元素递增有序,本题要求实现一个函数merge(LinkList A, LinkList B),将A、B归并成一个按元素值递减(允许有相同值)有序的链表C,要求用A、B中的原结点形成,不能重新申请结点。
函数接口定义:
LinkList merge(LinkList A, LinkList B);
其中 A 和 B 都是用户传入的参数。 A 是单链表A的头指针; B 是单链表B的头指针。函数须返回单链表C的头指针。
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}LNode, *LinkList;
输入格式:1 3 5 7 9 -1
2 4 6 8 8 10 -1
输出格式:10 9 8 8 7 6 5 4 3 2 1
代码如下:
#include<stdio.h> #include<stdlib.h>typedef int DataType; struct Node {DataType data; struct Node* next; }; typedef struct Node* PNode; typedef struct Node* LinkList; LinkList SetNullList_Link() {LinkList head = (LinkList)malloc(sizeof(struct Node));if (head != NULL) head->next = NULL;else printf("alloc failure");return head; }void CreateList_Tail(PNode head) {PNode p = NULL;PNode q = head;DataType data;scanf("%d", &data);while (data != -1){ p = (struct Node*)malloc(sizeof(struct Node));p->data = data;p->next = NULL;q->next = p;q = p;scanf("%d", &data);} }void merge(LinkList A, LinkList B) {LinkList C;PNode p,q,s;p = A->next;q = B->next;C = A;//C表的头结点C->next = NULL;while ( p && q ){if ( p->data < q->data ){s = p;p = p->next;}else {s = q;q = q->next;}//从原AB表中取出较小者s->next = C->next;//插入到 C表的头部C->next = s;}if ( p==NULL ) p=q;while(p)//将剩余的结点一个个取出 ,插入到 C表的头部{s = p;p = p->next;s->next = C->next;C->next = s; } }void print(LinkList head) {PNode p = head->next;while (p){printf("%d ", p->data);p = p->next;} }void DestoryList_Link(LinkList head) {PNode pre = head; PNode p = pre->next;while (p){free(pre);pre = p;p = pre->next;}free(pre); }int main() {LinkList A = NULL;LinkList B = NULL;LinkList C = NULL;A = SetNullList_Link();CreateList_Tail(A);B = SetNullList_Link();CreateList_Tail(B);// MoveMaxToTail(LinkList L);/*To Do List*/merge(A,B);//MoveMaxToTail(C);print(A);DestoryList_Link(C);return 0; }