qq_69544187 2022-09-24 22:30
浏览 13
已结题

数据结构链表程序阅读注解

求注解
#include <stdio.h>
#include <malloc.h>
#define N 100
typedef struct node
{int data;
struct node *next;
}node;

node * createsl()
{
node *p,*s,h;
int j=1,x;
p=s=h=(node
)malloc(sizeof(node));
h->next=NULL;
printf("please input \n");
while(x!=-1&&j<=N)
{
printf("number %d:",j);
scanf("%d",&x);
s=(node*)malloc(sizeof(node));
s->data=x;
if(h->next==NULL)
h->next=s;
else
p->next=s;
p=s;
j++;
}
p->next=NULL;
return h;
}

int access(node *h,int i)
{
node *p;int j=1;
p=h->next;
while(p!=NULL)
{
if(p->data==i)
break;
p=p->next;
j++;
}
if(p!=NULL)
{
printf("%d\n",j);
return(p->data);
}
else
{
printf("error!\n");
return -1;
}
}

void insertsl(node *h,int i,int l)
{
node *p,*t;
int j=1;
p=h->next;;
while(p->next!=NULL&&j<l-1)
{
p=p->next;
j++;
}

t=(node*)malloc(sizeof(node));
t->data=i;
t->next=p->next;
p->next=t;

}

void deletesl(node *h,int i)
{
node *p,*s,*q;
int j=1;
p=h;
while(p->next!=NULL)
{
q=p->next;
if(q->data==i)
break;
p=p->next;
j++;
}

if(p->next==NULL)
{
printf("error.\n");
return;
}
else
{
s=p->next;
p->next=s->next;
free(s);

}
}

void print(node *h)
{

node *s;
s=h->next;
if(s!=NULL)
{
while(s->next!=NULL)
{
printf(" %d ",s->data) ;
s=s->next;
}
}
else
printf("error");
printf("\n");
}

int main()
{
node *p;
int a,k;
p=createsl() ;

print(p);

scanf("%d",&a);
access(p,a);

printf("\nplease input :\n"); 
scanf("%d",&a); 
printf("\nplease input :\n");
scanf("%d",&k);
insertsl(p,a,k);
 printf("\nthe list is:\n"); 
print(p); 

printf("\nplease input :\n");
scanf("%d",&a);
deletesl(p,a);

print(p);
return 0;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 10月2日
    • 创建了问题 9月24日

    悬赏问题

    • ¥15 iOS 自定义输入法-第三方输入法
    • ¥15 很想要一个很好的答案或提示
    • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
    • ¥15 怀疑手机被监控,请问怎么解决和防止
    • ¥15 Qt下使用tcp获取数据的详细操作
    • ¥15 idea右下角设置编码是灰色的
    • ¥15 全志H618ROM新增分区
    • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
    • ¥15 NAO机器人的录音程序保存问题
    • ¥15 C#读写EXCEL文件,不同编译