📄 line.c
字号:
#include<stdio.h>
#include<alloc.h>
typedef struct node
{
int num;
struct node *next;
};
struct node *create_list();
void display_list(struct node *head);
struct node *insert_list(struct node *head,int num);
struct node *delete_list(struct node *head,int num);
int main()
{
struct node *head;
int num;
head=create_list();
display_list(head);
printf("Inputn the insert num:\n");
scanf("%d",&num);
head=insert_list(head,num);
display_list(head);
printf("Input the delete num:\n");
scanf("%d",&num);
head=delete_list(head,num);
display_list(head);
getch();
return 0;
}
struct node *create_list()
{
int k=1;
int j;
struct node *head,*new,*p;
head=(struct node *)malloc(sizeof(struct node));
if(head!=NULL)
{
scanf("%d",&head->num);
head->next=NULL;
p=head;
}
else
{
printf("Cannot create new node\n");
exit(0);
}
printf("Create new node?(if input(-1)end create)");
scanf("%d",&k);
while(k!=-1)
{
new=(struct node *)malloc(sizeof(struct node));
if(new!=NULL)
{
new->num=k;
new->next=NULL;
p->next=new;
p=new;
}
else
{
printf("Cannot create new node\n");
exit(0);
}
printf("Continue create next node?(if input(-1)end loop)");
scanf("%d",&k);
}
return(head);
}
struct node *insert_list(struct node *head,int num)
{
struct node *p,*new,*q;
new=(struct node *)malloc(sizeof(struct node));
if(new!=NULL)
{
new->num=num;
new->next=NULL;
if(new->num<head->num||head==NULL)
{
new->next=head;
head=new;
}
else
{
p=head;
while(p!=NULL&&p->num<new->num)
{
q=p;
p=p->next;
}
new->next=p;
q->next=new;
}
return(head);
}
else
{
printf("Cannot create new node\n");
exit(0);
}
}
struct node *delete_list(struct node *head,int num)
{
struct node *p,*q;
q=head;
p=head->next;
while(p&&(p->num!=num))
{
q=p;
p=p->next;
}
if(p)
{
q->next=p->next;
free(p);
}
else printf("\nNot find.");
return (head);
}
void display_list(struct node *head)
{
struct node *p;
p=head;
while(p!=NULL)
{
printf("%d->",p->num);
p=p->next;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -