当前位置:网站首页>Experiment 3: design and verify all operations represented by linear table sequence on the computer
Experiment 3: design and verify all operations represented by linear table sequence on the computer
2022-06-11 18:03:00 【Hold on to the idea and cut the river】
This blog is from teacher YanWeiMin's book 《 data structure 》 Today, you need to verify the single linked list in the book , I have nothing to do to help them directly .
Topic reproduction
1、 The main basic operations of the current single linked list , And write a main program to verify ;
2、 In a single linked list L Two data elements of a and b Insert between x, Write insert x Algorithm implementation of , And write a program to verify
Idea of problem solving
- subject 1 The code on the book is mainly typed once
- The second question is given directly O(n) To insert
subject 1:
Status GetElem_L(LinkList L,int i,ElemType &e){
LinkList p = L->next;
int j = 1;
while(p && j<i){
p = p->next;
++j;
}
if(!p || j>i) return ERROR;
e = p->data;
return OK;
}
Status ListInsert_L(LinkList &L,int i,ElemType e){
LinkList p = L;
int j = 0;
while(p && j<i-1){
p = p->next;
++j;
}
if(!p || j>i-1) return ERROR;
LinkList s = (LinkList)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return OK;
}
Status ListDelete_L(LinkList &L,int i,ElemType &e){
LinkList p = L;
int j = 0;
while(p->next && j<i-1){
p = p->next ;
++j;
}
if(!(p->next)||j>i-1) return ERROR;
LinkList q = p->next;
p->next = q->next;
e = q->data;
free(q);
return OK;
}
void CreateList_L(LinkList&L,int n ){
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
for(int i=n;i>0;i--){
LinkList p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = L->next;
L->next = p;
}
}
subject 2
Status LinkList_ab(LinkList&L,ElemType a,ElemType b,ElemType x){
LinkList p = L;
p = p->next;
while(p){
if(p->data == a) break;
p = p->next;
}
LinkList q = p->next;
if(p==NULL || q==NULL) return ERROR;
else{
LinkList tmp = (LinkList)malloc(sizeof(LNode));
tmp->data = x;
p->next = tmp;
tmp->next = q;
}
return OK;
}
summary
No difficulty. , Just follow the book .
Complete code
#include<iostream>
#define ERROR -1
#define OK 1
using namespace std;
typedef int ElemType;
typedef int Status;
typedef struct LNode{
ElemType data;
struct LNode*next;
}LNode,*LinkList;
Status GetElem_L(LinkList L,int i,ElemType &e){
LinkList p = L->next;
int j = 1;
while(p && j<i){
p = p->next;
++j;
}
if(!p || j>i) return ERROR;
e = p->data;
return OK;
}
Status ListInsert_L(LinkList &L,int i,ElemType e){
LinkList p = L;
int j = 0;
while(p && j<i-1){
p = p->next;
++j;
}
if(!p || j>i-1) return ERROR;
LinkList s = (LinkList)malloc(sizeof(LNode));
s->data = e;
s->next = p->next;
p->next = s;
return OK;
}
Status ListDelete_L(LinkList &L,int i,ElemType &e){
LinkList p = L;
int j = 0;
while(p->next && j<i-1){
p = p->next ;
++j;
}
if(!(p->next)||j>i-1) return ERROR;
LinkList q = p->next;
p->next = q->next;
e = q->data;
free(q);
return OK;
}
void CreateList_L(LinkList&L,int n ){
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
for(int i=n;i>0;i--){
LinkList p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = L->next;
L->next = p;
}
}
// In a single linked list L Two data elements of a and b Insert between x, Write insert x Algorithm implementation of
// Assume adjacency
Status LinkList_ab(LinkList&L,ElemType a,ElemType b,ElemType x){
LinkList p = L;
p = p->next;
while(p){
if(p->data == a) break;
p = p->next;
}
LinkList q = p->next;
if(p==NULL || q==NULL) return ERROR;
else{
LinkList tmp = (LinkList)malloc(sizeof(LNode));
tmp->data = x;
p->next = tmp;
tmp->next = q;
}
return OK;
}
Status Print(LinkList L){
if(L==NULL) return ERROR;
LinkList p = L;
p=p->next;
while(p){
cout << " " << p->data;
p=p->next;
}
cout << endl;
}
int main(){
LinkList L;
cout << "test1:createList:" << endl;
CreateList_L(L,5);
Print(L);
//Delete L
//1 2 3 4 5
int x;
int flag = ListDelete_L(L,1,x);
cout << "test2: deleted: " << x << endl;
Print(L);
cout << "test3: geted:" ;
GetElem_L(L,2,x);
cout << x << endl;
cout << "test4:ListInsert_L:5" << endl;
ListInsert_L(L,2,5);
Print(L);
cout << "test5:insert [5,3] into 4"<< endl;
LinkList_ab(L,5,3,4);
Print(L);
return 0;
}
边栏推荐
- Winter vacation daily question 2022 [week1 not finished]
- Mysql8 installation, Navicat installation, sqli labs setup
- Summary of clustering methods
- How ZABBIX can customize MySQL monitoring items and trigger alarms
- Service learning notes 03 front desk service practice
- Delete the penultimate node of the linked list ---2022/02/22
- Test basis: black box test
- 合并K个升序链表---2022/02/26
- Initial experience of MariaDB spider sharding engine
- After class, I looked at the document and went back to the lab. I picked up the forgotten SQL operators again
猜你喜欢

社会工程学实战入门

如何学习和自学

adb 命令学习笔记

Seeing the sudden death of a 28 year old employee, I was silent

网络安全威胁情报体系
![[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (III)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (III)

Valid parentheses ---2022/02/23
![Spring 2021 daily question [end of week4]](/img/b3/2f5a66b0d4374db3d4db0b71d72f7e.jpg)
Spring 2021 daily question [end of week4]
![Spring 2021 daily question [week5 not finished]](/img/bd/35a8e0ded3b1a0727415c4cd95e781.jpg)
Spring 2021 daily question [week5 not finished]

Online excel file parsing and conversion to JSON format
随机推荐
upload-labs通关未半而中道崩殂
Tidb GC related problems
Seeing the sudden death of a 28 year old employee, I was silent
Service学习笔记02-实战 startService 与bindService
测试基础之:黑盒测试
【先收藏,早晚用得到】100个Flink高频面试题系列(二)
Mathematical basis of information security Chapter 1 - Division
tidb-cdc创建任务报错 Unknown or incorrect time zone
Service学习笔记01-启动方式与生命周期
7-1 are prime numbers
Merge two ordered linked lists ---2022/02/24
Tle6389-2g V50's unique pwm/pfm control scheme has a duty cycle of up to 100%, forming a very low differential pressure - keshijin mall
jsfinder,wafw00f安装,nmap配置(缺少msvcr120.dll文件)
Hello go (XV). Go language common standard library V
TestPattern error
tidb-cdc日志tables are not eligible to replicate
【先收藏,早晚用得到】49个Flink高频面试题系列(二)
Service learning notes 04 other service implementation methods and alternative methods
6-8 reading and writing of structured files 1
tidb-ddl的速度的调整