c语言链表的问题
请解释一下这段代码#include<stdio.h>#include<malloc.h>#include<stdlib.h>structLNode{intdata;str...
请解释一下这段代码
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct LNode{
int data;
struct LNode *next;
};
void creat(struct LNode *head,int size){
struct LNode *p,*new_node;
int i,n;
p = head;
for(i = 1;i <= size;i++){
scanf("%d",&n);
new_node = (struct LNode *)malloc(sizeof(struct LNode));
new_node -> data = n;
new_node -> next = NULL;
p -> next = new_node;
p = p -> next;
}
}
void print(struct LNode *head){
struct LNode *p;
p = head -> next;
while(p != NULL){
printf("%d",p -> data);
p = p -> next;
}
}
void ListInsert(struct LNode *head,int i,int e){
struct LNode *p,*new_node;
int k;
new_node = (struct LNode*)malloc(sizeof(struct LNode));
new_node -> data = e;
new_node -> next = NULL;
p = head;
for(k = 2;k <= i && p -> next;k++) p = p -> next;
if(p -> next = NULL)
p -> next = new_node;
else{
new_node -> next = p -> next;
p -> next = new_node;
}
}
void ListDelete(struct LNode *head,int i){
struct LNode *p,*q;
int k;
p = head;
for(k = 2;k <= i && p -> next;k++) p = p -> next;
if(p -> next){
q = p -> next;
p -> next = p -> next -> next;
free(q);
}
}
int LocateElem(struct LNode *head,char n){
int i = 0;
struct LNode *p;
p = head -> next;
while(p){
i++;
if(p -> data == n) return i;
p = p -> next;
}
return -1;
}
int main(){
struct LNode *head = NULL;
char e,dele;
int sel,i,n;
head = (struct LNode*)malloc(sizeof(struct LNode));
head -> next = NULL;
sel = -1;
while(sel != 0){
printf("\n请选择操作(1.创建 2.输出 3.插入 4.删除 5.查找 0.退出)");
scanf("%d",&sel);
switch(sel){
case 1:
printf("请输入要创建链表的元素的个数:");
scanf("%d",&n);
creat(head,n);
break;
case 2:
printf("所有链表元素如下:");
print(head);
break;
case 3:
printf("请输入要插入的元素:");
scanf("%d",&n);
printf("请输入要插入的位置");
scanf("%d",&i);
ListInsert(head,i,n);
break;
case 4:
printf("请输入要删除元素的位置:");
scanf("%d",&i);
ListDelete(head,i);
break;
case 5:
printf("请输入要查找的元素:");
scanf("%d",&n);
i = LocateElem(head,n);
if(i != -1)
printf("%d是链表的第%d个元素\n",n,i);
else
printf("查找失败\n");
break;
case 0:
break;
}
}
return 0;
} 展开
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct LNode{
int data;
struct LNode *next;
};
void creat(struct LNode *head,int size){
struct LNode *p,*new_node;
int i,n;
p = head;
for(i = 1;i <= size;i++){
scanf("%d",&n);
new_node = (struct LNode *)malloc(sizeof(struct LNode));
new_node -> data = n;
new_node -> next = NULL;
p -> next = new_node;
p = p -> next;
}
}
void print(struct LNode *head){
struct LNode *p;
p = head -> next;
while(p != NULL){
printf("%d",p -> data);
p = p -> next;
}
}
void ListInsert(struct LNode *head,int i,int e){
struct LNode *p,*new_node;
int k;
new_node = (struct LNode*)malloc(sizeof(struct LNode));
new_node -> data = e;
new_node -> next = NULL;
p = head;
for(k = 2;k <= i && p -> next;k++) p = p -> next;
if(p -> next = NULL)
p -> next = new_node;
else{
new_node -> next = p -> next;
p -> next = new_node;
}
}
void ListDelete(struct LNode *head,int i){
struct LNode *p,*q;
int k;
p = head;
for(k = 2;k <= i && p -> next;k++) p = p -> next;
if(p -> next){
q = p -> next;
p -> next = p -> next -> next;
free(q);
}
}
int LocateElem(struct LNode *head,char n){
int i = 0;
struct LNode *p;
p = head -> next;
while(p){
i++;
if(p -> data == n) return i;
p = p -> next;
}
return -1;
}
int main(){
struct LNode *head = NULL;
char e,dele;
int sel,i,n;
head = (struct LNode*)malloc(sizeof(struct LNode));
head -> next = NULL;
sel = -1;
while(sel != 0){
printf("\n请选择操作(1.创建 2.输出 3.插入 4.删除 5.查找 0.退出)");
scanf("%d",&sel);
switch(sel){
case 1:
printf("请输入要创建链表的元素的个数:");
scanf("%d",&n);
creat(head,n);
break;
case 2:
printf("所有链表元素如下:");
print(head);
break;
case 3:
printf("请输入要插入的元素:");
scanf("%d",&n);
printf("请输入要插入的位置");
scanf("%d",&i);
ListInsert(head,i,n);
break;
case 4:
printf("请输入要删除元素的位置:");
scanf("%d",&i);
ListDelete(head,i);
break;
case 5:
printf("请输入要查找的元素:");
scanf("%d",&n);
i = LocateElem(head,n);
if(i != -1)
printf("%d是链表的第%d个元素\n",n,i);
else
printf("查找失败\n");
break;
case 0:
break;
}
}
return 0;
} 展开
1个回答
展开全部
总体来说,这是一段单链表维护的代码。
每个结点的结构用结构体给出定义,包含一个整数的数据域和一个指向下一个结点的指针域:
struct LNode{
int data;
struct LNode *next;
};
具体的函数中,create用于创建链表,创建过程中会要求用户输入每个结点中存储的数据;print用于打印整个链表;ListInsert用于在特定位置插入新结点;ListDelete用于删除特定位置的结点;LocateElem用于查找等于输入值的结点位置。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询