当前位置:网站首页>链表的基本操作
链表的基本操作
2022-07-22 10:40:00 【LH2HA3】
以下用一个程序的来简要介绍链表的基本操作
包括构造链表、遍历链表、删除某结点、向链表中插入一个结点等等。
#include<stdio.h>
#include<stdlib.h>
int i=0;
#define LINE for(i;i<80;i++)\
printf("-")
typedef struct LNode{
int data; //数据域
struct LNode *next; //指针域
} LNode,*LinkList;
LinkList L;//创建头指针 (全局变量)
void CreateList(LinkList &L,int n){ //头插法
L = (LNode *)malloc(sizeof(LNode));//申请空间
L->next=NULL;
while(n!=0){
LNode *p=(LNode *)malloc(sizeof(LNode));//申请空间
scanf("%d",&(p->data));
p->next=L->next;
L->next=p;
--n;
}
}
void display(LinkList L){
LNode *p;
p=L->next;//p指向第一个节点
while(p!=NULL){
printf("%d\t",p->data);
p=p->next;
}
}
void ListDelete(LinkList &L,int i){
LNode *p,*q;
p=L;//让p指向头结点
int j=0;
while(p->next&&j<i-1){
p=p->next;
j++;
}//先要找到该结点
if(!(p->next)||j>i-1)printf("删除错误!");
q=p->next;
p->next=q->next;
free(q);
}
void ListInesrt(LinkList &L,int i){
LNode *e,*p;
int j=0;
e=(LNode *)malloc(sizeof(LNode));
printf("请输入数据:");
scanf("%d",&e->data);
p=L;
while(p->next&&j<i-1){
p=p->next;
}
e->next=p->next;
p->next=e;
}
void Test(){
int i;
printf("请输入结点个数:");
scanf("%d",&i);
CreateList(L,i);
printf("\n");
LINE;
printf("\n");
display(L);
ListDelete(L,2);
printf("\n");
LINE;
printf("\n");
display(L);
ListInesrt(L,1);
LINE;
printf("\n");
display(L);
}
int main(){
Test();
return 0;
}
边栏推荐
- 1038 Recover the Smallest Number (30 分)
- 【FPGA】状态机
- Deformable Detr paper accuracy, and analyze the network model structure
- Mysql连接失败解决方案
- 7-3 size of military unit (25 points)
- MySQL connection failure solution
- LeetCode - 整数反转
- "Pre training weekly" No. 38: transformer, Bert structure optimization
- 7-2 Rank a Linked List (25 分)
- DEFORMABLE DETR 论文精度,并解析网络模型结构
猜你喜欢
随机推荐
1057 stack (30 points)
DEFORMABLE DETR 论文精度,并解析网络模型结构
1045 favorite color stripe (30 points)
Automatic generation of common centroid capacitance array with arbitrary capacitance ratio
Data transfer from one Mysql to another MySQL
Reinforcement learning weekly 39: approximate optimal depth, multi-agent generalization, character animation reinforcement learning
pytorch 动态调整学习率,学习率自动下降,根据loss下降
Aminer paper recommendation
Dense passage retrieval for open domain question answering notes
Exclusive interview with Huang Hui, a scholar of women in AI: dream of drawing shapes and find the door to breakthrough
JSON output to file line by line in format
具有非矩形布局1结构的电流镜的梯度灵敏度降低
Causal learning weekly, issue 10: introduction to the latest causal discovery related papers in iclr2022
【面试:基础篇03:选择排序】
1053 Path of Equal Weight (30 分)
Function principle of pytoch network training process: source code analysis optimizer.zero_ grad()loss.backward()optimizer.step()
数据从一台Mysql传输到另一台Mysql
Vscode关闭自动更新
1038 Recover the Smallest Number (30 分)
类的属性新建(初级理解)