?? ensure.txt
字號:
/*誠信保證:"我保證沒有抄襲他人的作業!"*/
#include"malloc.h"
#include"stdio.h"
#define LEN sizeof(struct node)
struct node
{
int data;
struct node*next;
};
int n; /*n為結點個數*/
struct node*setup() /*建立鏈表函數*/
{struct node *head,*p,*q;
n=0;
p=(struct node*)malloc(LEN); /*建立新結點*/
scanf("%d",&(p->data)); /*輸入一結點數據賦給p所指結點*/
head=NULL;
while(p->data!=0) /*輸入一結點數據不為零時*/
{n++;
if(n==1)head=p; /*把p所指結點作第一個結點*/
else q->next=p; /*把p所指結點連結到表尾*/
q=p; /*q移至表尾*/
p=(struct node*)malloc(LEN);
scanf("%d",&(p->data));
}
q->next=NULL; /*表尾結點的指針變量置空*/
return(head);
}
struct node*del(struct node*head,int data) /*刪除鏈表的結點函數*/
{struct node *p,*q;
if(head==NULL)
{printf("\nlist null!\n");}
else
{p=head;
while(data!=p->data&&p->next!=NULL)
{q=p;p=p->next;}
if(data==p->data)
{
if(p==head)
head=p->next;
else
q->next=p->next;
printf("delete:%d\n",data);
n=n-1;
}
else
printf("%d not been found!\n",data);
}
return(head);
}
void print(struct node*head) /*輸出鏈表函數*/
{
struct node*m;
printf("new,there %d records are:\n",n);
m=head;
while(m!=NULL)
{printf("%d\n",m->data);
m=m->next;
}
}
struct node*insert(struct node*head,struct node*stud)/*插入結點數據*/
{
struct node *p0,*p,*q;
p=head;
p0=stud;
if(head==NULL)
{head=p0;
p0->next=NULL;}
else while((p0->data>p->data)&&(p->next!=NULL))
{q=p;p=p->next;}
if(p0->data<p->data)
{
if(head==p)
head=p0;
else q->next=p0;
p0->next=p;
}
else
{p->next=p0;p0->next=NULL;}
n=n+1;
return(head);
}
void main()
{
struct node*head,stud;
int x;
printf("input records:\n");
head=setup(); /*建立鏈表,返回頭指針*/
printf("\ninput the delete number:");
scanf("%d",&x); /*輸入要刪除的結點數據*/
head=del(head,x); /*刪除后的頭地址*/
print(head); /*輸出全部結點*/
printf("\ninput the inserted record:");
scanf("%d",&stud.data); /*輸入要插入的記錄*/
head=insert(head,&stud); /*插入如記錄后的頭地址*/
print(head);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -