Mobile wallpaper 1Mobile wallpaper 2Mobile wallpaper 3Mobile wallpaper 4Mobile wallpaper 5Mobile wallpaper 6
178 字
1 分钟
数据结构链表复习

code:

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 2
#define OVERFLOW 3
#define INIT_LIST_SIZE 100
typedef int Status;
typedef int ElemType;
typedef struct Lnode{
ElemType data;
struct Lnode *next;
}Lnode,*Linklist;
Status Initsq(Linklist &L){
L=(Linklist)malloc(sizeof(Lnode));
if(!L){
return ERROR;
}
L->next=NULL;
return OK;
}
//获取
Status GetElem(Linklist L,int i,ElemType &e){
Linklist p;
int j;
p=L->next;
j=1;
while(p&&j<i){
p=p->next;
j++;
}
if(!p||j>i){
return ERROR;
}
e=p->data;
return OK;
}
//插入
Status Insertsq(Linklist &L,int i,ElemType e){
Linklist p,q;
int j;
p=L;
j=0;
while(p&&j<i-1){
p=p->next;
j++;
}
if(!p||j>i){
return ERROR;
}
q=(Linklist)malloc(sizeof(Lnode));
q->data=e;
q->next=p->next;
p->next=q;
return OK;
}
//删除
Status Deletesq(Linklist &L,int i,ElemType &e){
Linklist p,q;
int j;
p=L;
j=0;
while(p->next&&j<i-1){
p=p->next;
j++;
}
if(!p->next||j>i){
return ERROR;
}
q=p->next;
e=q->data;
p->next=q->next;
free(q);
return OK;
}
int main(){
Linklist list;
ElemType x;
int i;
if(!Initsq(list)){
return OVERFLOW;}
printf("Insert 10 elements:\n");
for(i=1;i<=10;i++){
x=rand()%100;
printf("%d\n",x);
Insertsq(list,i,x);
}
printf("Delete 10 elements:\n");
for(i=10;i>=1;i--){
Deletesq(list,i,x);
printf("%d\n",x);
}
return OK;
}

运行结果如下则无误

运行结果

祝大家数据结构考试顺利!!!

数据结构链表复习
https://sldblog.top/posts/markdown_lianbiao/
作者
十裡冬
发布于
2025-12-12
许可协议
C++

部分信息可能已经过时

封面
Sample Song
Sample Artist
封面
Sample Song
Sample Artist
0:00 / 0:00