|
发表于 2023-10-3 14:52:33
|
显示全部楼层
1. 根据输入表的长度n和各个数据元素值建立一个带头结点的单链表,并输出链表中各元素值,观察输入的内容与输出的内容是否一致;
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct lnode{
elemtype date;
struct lnode *next;
}lnode,*linklist;
void initlist(linklist &l, int n) {
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
for(int i = 0; i < n; i++) {
linklist p = (linklist)malloc(sizeof(lnode));
scanf("%d", &(p->date));
p->next = l->next;
l->next = p;
}
}
void output(linklist l) {
linklist p = l->next;
while(p) {
printf("%d ", p->date);
p = p->next;
}
printf("\n");
}
int main() {
int n;
printf("请输入你想建立的长度:\n");
scanf("%d", &n);
linklist l;
initlist(l, n);
output(l);
return 0;
}
2. 在链表的第i个元素之前插入一个值为x的元素,并输出插入后的链表中各元素值;
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct lnode{
elemtype date;
struct lnode *next;
}lnode,*linklist;
void initlist(linklist &l, int n) {
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
for(int i = 0; i < n; i++) {
linklist p = (linklist)malloc(sizeof(lnode));
scanf("%d", &(p->date));
p->next = l->next;
l->next = p;
}
}
void output(linklist l) {
linklist p = l->next;
while(p) {
printf("%d ", p->date);
p = p->next;
}
printf("\n");
}
void insert(linklist &l, elemtype x, int i) {
linklist p = l;
for(int j = 0; j < i - 1; j++) {
p = p->next;
}
linklist q = (linklist)malloc(sizeof(lnode));
q->date = x;
q->next = p->next;
p->next = q;
}
int main() {
int n, i, x;
printf("请输入你想建立的长度:\n");
scanf("%d", &n);
linklist l;
initlist(l, n);
output(l);
printf("请输入你想插入的位置和数值:\n");
scanf("%d %d", &i, &x);
insert(l, x, i);
output(l);
return 0;
}
3. 删除链表中第i个元素,并输出被删除的元素值和删除后的链表中各元素值;
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct lnode{
elemtype date;
struct lnode *next;
}lnode,*linklist;
void initlist(linklist &l, int n) {
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
for(int i = 0; i < n; i++) {
linklist p = (linklist)malloc(sizeof(lnode));
scanf("%d", &(p->date));
p->next = l->next;
l->next = p;
}
}
void output(linklist l) {
linklist p = l->next;
while(p) {
printf("%d ", p->date);
p = p->next;
}
printf("\n");
}
void delete1(linklist &l, int i) {
linklist p = l, q;
for(int j = 0; j < i - 1; j++) {
p = p->next;
}
q = p->next;
p->next = q->next;
printf("被删除的元素值为:%d\n", q->date);
free(q);
}
int main() {
int n, i;
printf("请输入你想建立的长度:\n");
scanf("%d", &n);
linklist l;
initlist(l, n);
output(l);
printf("请输入你想删除的位置:\n");
scanf("%d", &i);
delete1(l, i);
output(l);
return 0;
}
4. 在链表中查找第i个数据元素结点,如果查找成功,则显示“查找成功!”和该元素的值,否则显示“查找失败!”;
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct lnode{
elemtype date;
struct lnode *next;
}lnode,*linklist;
void initlist(linklist &l, int n) {
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
for(int i = 0; i < n; i++) {
linklist p = (linklist)malloc(sizeof(lnode));
scanf("%d", &(p->date));
p->next = l->next;
l->next = p;
}
}
void output(linklist l) {
linklist p = l->next;
while(p) {
printf("%d ", p->date);
p = p->next;
}
printf("\n");
}
bool search(linklist l, int i, elemtype &e) {
linklist p = l;
for(int j = 0; j < i; j++) {
p = p->next;
if(p == NULL) {
return false;
}
}
e = p->date;
return true;
}
int main() {
int n, i;
elemtype e;
printf("请输入你想建立的长度:\n");
scanf("%d", &n);
linklist l;
initlist(l, n);
output(l);
printf("请输入你想查找的位置:\n");
scanf("%d", &i);
if(search(l, i, e)) {
printf("查找成功!该元素的值为:%d\n", e);
} else {
printf("查找失败!\n");
}
return 0;
}
5. 根据输入的n和n个递增的有序数据建立一个带头结点的有序单链表L1,并输出有序链表中各元素值;
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct lnode{
elemtype date;
struct lnode *next;
}lnode,*linklist;
void initlist(linklist &l, int n) {
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
for(int i = 0; i < n; i++) {
linklist p = (linklist)malloc(sizeof(lnode));
scanf("%d", &(p->date));
linklist q = l;
while(q->next && q->next->date < p->date) {
q = q->next;
}
p->next = q->next;
q->next = p;
}
}
void output(linklist l) {
linklist p = l->next;
while(p) {
printf("%d ", p->date);
p = p->next;
}
printf("\n");
}
int main() {
int n;
printf("请输入你想建立的长度:\n");
scanf("%d", &n);
linklist l;
initlist(l, n);
output(l);
return 0;
}
6. 根据输入的m和m个递增的有序数据建立一个带头结点的有序单链表L2,并输出有序链表中各元素值;
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct lnode{
elemtype date;
struct lnode *next;
}lnode,*linklist;
void initlist(linklist &l, int n) {
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
for(int i = 0; i < n; i++) {
linklist p = (linklist)malloc(sizeof(lnode));
scanf("%d", &(p->date));
linklist q = l;
while(q->next && q->next->date < p->date) {
q = q->next;
}
p->next = q->next;
q->next = p;
}
}
void output(linklist l) {
linklist p = l->next;
while(p) {
printf("%d ", p->date);
p = p->next;
}
printf("\n");
}
int main() {
int m;
printf("请输入你想建立的长度:\n");
scanf("%d", &m);
linklist l;
initlist(l, m);
output(l);
return 0;
}
7. 将有序链表L1和L2合并到单链表L1中,要求使用原链中的结点,并使L1保持其原有序性,最后输出合并后的有序链表中各结点的元素值。
#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct lnode{
elemtype date;
struct lnode *next;
}lnode,*linklist;
void initlist(linklist &l, int n) {
l = (linklist)malloc(sizeof(lnode));
l->next = NULL;
for(int i = 0; i < n; i++) {
linklist p = (linklist)malloc(sizeof(lnode));
scanf("%d", &(p->date));
linklist q = l;
while(q->next && q->next->date < p->date) {
q = q->next;
}
p->next = q->next;
q->next = p;
}
}
void merge(linklist &l1, linklist l2) {
linklist p1 = l1->next, p2 = l2->next, q = l1;
while(p1 && p2) {
if(p1->date <= p2->date) {
q = p1;
p1 = p1->next;
} else {
q->next = p2;
p2 = p2->next;
q = q->next;
q->next = p1;
}
}
if(p2) {
q->next = p2;
}
free(l2);
}
void output(linklist l) {
linklist p = l->next;
while(p) {
printf("%d ", p->date);
p = p->next;
}
printf("\n");
}
int main() {
int n, m;
printf("请输入L1的长度:\n");
scanf("%d", &n);
linklist l1;
initlist(l1, n);
printf("请输入L2的长度:\n");
scanf("%d", &m);
linklist l2;
initlist(l2, m);
merge(l1, l2);
output(l1);
return 0;
}
8. 已知两个一元多项式A(x)=a1xe1+ a2xe2+……+ amxem,B(x)= b1xe1+ b2xe2+……+ bmxen,其中ei为整数指数且递增有序。要求编写算法实现两个一元多项式A和B的加法运算A(x)= A(x)+ B(x),并编写程序测试其正确性。
```c++
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int coef;
int expn;
struct node *next;
}node, *linklist;
void initlist(linklist &l, int n) {
l = (linklist)malloc(sizeof(node));
l->next = NULL;
for(int i = 0; i < n; i++) {
linklist p = (linklist)malloc(sizeof(node));
scanf("%d %d", &(p->coef), &(p->expn));
linklist q = l;
while(q->next && q->next->expn > p->expn) {
q = q->next;
}
if(q->next && q->next->expn == p->expn) {
q->next->coef += p->coef;
free(p);
} else {
p->next = q->next;
q->next = p;
}
}
}
void output(linklist l) {
linklist p = l->next;
while(p) {
printf("%dx^%d", p->coef, p->expn);
if(p->next) {
printf("+");
}
p = p->next;
}
printf("\n");
}
void add(linklist &A, linklist B) {
linklist p = A->next, q = B->next, r = A;
while(p && q) {
if(p->expn < q->expn) {
r->next = p;
r = p;
p = p->next;
} else if(p->expn == q->expn) {
p |
|