PAST
W5 线性表
OJ _(:з」∠)_
第5周上机
Y 1199 表达式计算
Y 1728 交换节点(线性表)
N 1729 建立链表(线性表)
1730 链表排列(线性表)
Y 1731链表插入(线性表)
Y 1734 删除线性表节点(线性表)
第5周课后
1744 求链表交集(线性表)
1746 差集运算(线性表)
N 1748 算法问题(线性表)
1749 子序列问题(线性表)
N 1724 删除相同元素(线性表)
Y 1741 找相同元素(线性表)
1780 字符串的修改(串)
【链表的基本操作】
#include <stdio.h>
#include <stdlib.h>
typedef int ET;
typedef enum {F, T} bool; //重命名枚举类型,枚举值为FALSE(0)和TRUE(1)
typedef struct node { ET data; struct node *next;} NODE, *List; //单链表的结构体类型定义
//初始化
void Init(List *list){
NODE *head; head = (NODE *)malloc(sizeof(NODE)); //if (!s) exit(1); //头结点空间分配失败,则退出程序
*list = head; head->next = NULL;}
//销毁操作,销毁单链表
void Destroy(List *list){
NODE *pre = *list;
while (pre){
NODE *pro = pre;
pro = pro->next;
free(pre);}
*list = NULL;} //将单链表头指针置为空
//将单链表清空,销毁首元结点及之后的所有结点,仅保留头结点
void Clear(List *list){Destroy(&((*list)->next));}
//判断单链表是否为空
bool IsEmpty(List list) {if (list->next == NULL) return T; else return F;}
//返回单链表的长度
int Length(List list){
NODE *p = list->next; int len = 0; //初始化长度计数器为0
while (p) {len++; p = p->next;}
return len;}
//插入
bool Insert(List list, int pos, ET e){
NODE *pre = list, *new; int i = 0;
while (pre && i < pos-1) { pre = pre->next; i++; } //if (!p || pos < 1) return F;
new = (NODE *)malloc(sizeof(NODE)); //if (!s) exit(1); //新结点空间分配失败,则退出程序
new->data = e;
new->next = pre->next;
pre->next = new;
return T;}
//删除
bool Delete(List l, int pos, ET *e){
NODE *pre = l, *delete; int i = 0;
while (pre->next && i < pos-1) {pre = pre->next; i++;}//if (!(pre->next) || pos < 1) return F;
delete = pre->next; *e = delete->data;
pre->next = delete->next; free(delete);
return T;}
//读取,用e返回值
bool GetPos(List l, int pos, ET *e){
NODE *p = l; int i = 0;
while (p && i < pos) {p = p->next; i++;} //if (!p || pos < 1) return F;
*e = p->data; return T;}
//查找,返回位置或-1
int Find(List l, ET e){
NODE *p = l->next; int i = 1;
while (p && p->data != e) {p = p->next; i++;}
if (p) return i; return -1;}
//打印单链表
void Prints(List l){
NODE *p = l->next;
while (p) {printf("%d ", p->data); p = p->next;}
printf("\n");}
int main(){
int i; List l; ET e; Init(&l);
for (i = 1; i <= 10; i++) Insert(l, i, i); //Prints(l); //尾插法pos=i, 头插法pos=1
Insert(l, 11, 11); //printf("插入元素11到表中第11个位置,当前表长=%d\n", Length(l));
Insert(l, 1, 0); //printf("插入元素0到表中第1个位置,当前表长=%d\n", Length(l));
//Prints(l);
if ((i = Find(l, 8)) != -1) //printf("值为8的元素位置=%d\n", i); else printf("找不到值为8的元素\n");
if (GetPos(l, 1, &e)) //printf("第1个位置的元素值=%d\n", e);
if (Delete(l, 10, &e)) printf("正常\n"); //printf("第10个位置的元素删除成功,其值=%d,当前表长=%d\n", e, Length(l));
Clear(&l); if (IsEmpty(l)) //printf("此时表为空\n");
Destroy(&l);
}
1199 中缀-表达式计算
输入包括多组数据, 每组测试数据占一行, 包含一个字符串(长度不超过100), 表示要运算的表达式.
//REF https://blog.csdn.net/qq_61676465/article/details/123822501
#include<stdio.h>
int prime(int n){int prime=1;
for(int i=2;i<n;i++) if(n%i==0){ prime=0; break; }
return prime;}
int i;
double jiajian(char a[]);
double chen(char a[]);
double f(char a[]);
int main(){
char a[100] = { 0 };
double x;
while (gets(a) != NULL){
i = 0;
x=jiajian(a);
printf("%.3lf\n", x);}}
double jiajian(char a[]){
double s = chen(a);
int flag = 1;
while (flag){
if (a[i] == '+' || a[i] == '-'){
char c = a[i]; i++;
double value = chen(a);
if (c == '+') s=s+value;
else s=s-value; }
else flag = 0; }
return s; }
double chen(char a[]) {
double s = f(a);
while (1) {
char c = a[i];
if (c == '*' || c == '/') {
i++;
double value = f(a);
if (c == '*') s=s*value;
else s=s/value; }
else break; }
return s; }
double f(char a[]){
double s = 0, value=0;
char c = a[i];
int flag = 1, point = 1, j = 1;
if (c == '(') { i++; s=jiajian(a); i++; }
else {
if (i>0&&a[i-1]=='-') flag = -1;
while (((a[i]>='0')&&(a[i]<='9'))||a[i]=='.') {
if (a[i]=='.') {point=0; i++; }
if (point) s=s*10+a[i++]-'0';
else {value = value * 10 + a[i++] - '0';j *= 10;}}}
return s+value/j;}
1728 交换节点(线性表)
编写算法交换单链表中指针P所指结点与其后继结点,
HEAD是该链表的头指针,P指向该链表中某一结点。
输入链表长度:6
输入链表:1 2 3 4 5 6
输入p指向的节点:5
输出 1 2 3 4 5 6 5
//REF 陌路寒暄 https://www.jlqwer.com/posts/5281.html
#include <stdio.h>
#include <stdlib.h>
struct node{int data; struct node *next; }node;
struct node* create(int n){ int i;
struct node *head,*p1,*p2;
for(i=1;i<=n;i++){
p1=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p1->data);
if(i==1) head=p1;
else p2->next=p1;
p2=p1;}
p1->next=0;
return head;}
void fun(struct node *head,int n){
struct node *p,*q1=head,*q2=0;
int t=1;
while(t!=n){t++; q2=q1; q1=q1->next;}
if(n==1){
head=head->next;
q1->next=head->next;
head->next=q1;}
else{
p=q1->next;
q2->next=p;
q1->next=p->next;
p->next=q1;}
printf("%d",head->data);
head=head->next;
while(head!=0){
printf(" %d",head->data);
head=head->next;}}
int main(){int n; struct node *head;
scanf("%d",&n); head=create(n);
scanf("%d",&n); fun(head,n);}
1729 建立链表(线性表)
设键盘输入n个英语单词,输入格式为n, w1, w2, …,wn,其中n表示随后输入英语单词个数,试编一程序,建立一个单向链表,实现:如果单词重复出现,则只在链表上保留一个。
输入 4 now com now please
输出 now come please
#采用C++方式提交(太坑爹了吧) 点该啊
#REF https://blog.csdn.net/u014295602/article/details/100176603
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Lnode {char *a; struct Lnode *next;} LinkNode;
void Init(LinkNode *&L){//初始化链表
L=(LinkNode *)malloc(sizeof(LinkNode));
L->next=NULL;}
int Check(LinkNode *L,char *word){ //检查当前输入的单词是否已经存在
LinkNode *node = L->next;
while (NULL != node && strcmp(word, node->a)) node = node->next;
if (NULL != node) return 1; return 0;}
void Creat(LinkNode *&L,char *word) {
if(Check(L,word)) return;
LinkNode *p,*r=L;
p=(LinkNode *)malloc(sizeof(LinkNode));
p->a=word;
p->next=L->next; //头插法
L->next=p;}
void Print(LinkNode *head){ //因为采用头插法,所以此处采用递归输入
if(head==NULL) return ;
Print(head->next);
printf("%s ",head->a);}
int main() {LinkNode *head; Init(head);
int n,i; scanf("%d",&n);
for(i=0;i<n;i++) {
char *word=(char *)malloc(sizeof(char));
scanf("%s",word);
Creat(head,word);}
Print(head->next);}
1731 链表插入(线性表)
已知一单链表,从第二个结点至表尾递增有序(设a1<x<an, “第二个结点至表尾”指a1…an )。
试编写程序,将第一个结点删除并插入表中适当位置,使整个链表递增有序。
//REF https://blog.csdn.net/qq_43506138/article/details/85009674
#include <stdio.h>
#include <string.h>
typedef struct a { int node; struct a *next;} *LinkList, Node;
void CreatListTail(LinkList *T,int n) {int i;
LinkList p,r; *T=(LinkList)malloc(sizeof(Node)); r=*T;
for(i=0;i<n;i++){
p=(LinkList)malloc(sizeof(Node));
scanf("%d",&(p->node));
r->next=p;
r=p;}
r->next=NULL;}
int main(){
LinkList head,T,pre,NODE;
int i,j,k; int n;
while(~scanf("%d",&n)){
CreatListTail(&head,n);
T=head->next->next;
NODE=head->next;
pre=head->next;
// printf("T->node=%d\n",T->node);
while(T!=NULL&&NODE->node>T->node) {pre=T; T=T->next;}
//printf("T->node=%d\n",T->node);
if(NODE->next!=T){
head->next=NODE->next;
NODE->next=T;
pre->next=NODE;}
head=head->next;
while(head!=NULL){
printf("%d ",head->node);
head=head->next;}}}
1734 删除线性表节点(线性表)
//REF https://blog.csdn.net/m0_63932157/article/details/126715607
#include<stdio.h>
#include<string.h>
#define MAX 100
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct {ElemType* elem; int length;} SqList;
Status InitSqList(SqList* L){
L->elem = (ElemType*)malloc(sizeof(ElemType)*MAX);
if(!