当前位置:网站首页>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;
}
边栏推荐
- Upload labs failed to pass the customs halfway and the middle road collapsed
- 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
- System learning typescript (V) - joint type
- R language to find missing value location of data set
- Hwang
- Winter vacation daily question (improvement group) [end of week 4]
- Online excel file parsing and conversion to JSON format
- Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall
- 6-3 batch sum (*)
- Global and Chinese markets of solid polymer aluminum capacitors 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Test basis: black box test

Service learning notes 01 start method and life cycle

Merge K ascending linked lists ---2022/02/26
![Spring 2021 daily question [week6 not finished]](/img/32/74cff6f057ef9a7941fd6a41dc6635.jpg)
Spring 2021 daily question [week6 not finished]

10 ways to reset any user password

【先收藏,早晚用得到】100个Flink高频面试题系列(二)

Kubernetes deploys elk and collects container logs using filebeat

mariadb spider分片引擎初体验

智能化整体图例,布线、安防、广播会议、电视、楼宇、消防、电气图的图例【转自微信公众号弱电课堂】

Hello go (XV). Go language common standard library V
随机推荐
網絡安全威脅情報體系
Service learning notes 03 front desk service practice
【先收藏,早晚用得到】100个Flink高频面试题系列(二)
"College entrance examination" volume sent to the big model: 442 people put forward 204 tasks to the big model, led by Google
[piecemeal knowledge] [network composition] the mobile phone can be connected to the campus network, but the computer can't
Why OKR needs to be challenging
Rtsp/onvif protocol easynvr video platform arm version cross compilation process and common error handling
Using packstack to quickly install openstack
Global and Chinese markets for private internet access 2022-2028: Research Report on technology, participants, trends, market size and share
Mathematical basis of information security Chapter 1 - Division
Bracket generation ---2022/02/25
Service learning notes 04 other service implementation methods and alternative methods
Jsfinder, wafw00f installation, nmap configuration (msvcr120.dll file is missing)
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)
Global and Chinese markets for ultra high speed printers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of solid polymer aluminum capacitors 2022-2028: Research Report on technology, participants, trends, market size and share
Acwing game 40 [End]
Comparison of mongoose in express, KOA and egg
Mysql8 installation, Navicat installation, sqli labs setup
Global and Chinese market of high frequency bipolar junction transistors 2022-2028: Research Report on technology, participants, trends, market size and share