?? 實(shí)驗(yàn)二.txt
字號(hào):
# include <stdio.h>
# include<malloc.h>
# define ND sizeof(struct List)
struct List
{int data;
struct List *next;
};
void main()
{ int n,i=1,e,a;
struct List *head;
struct List *p,*q;
q=head=p=(struct List *)malloc(ND);
printf("輸入鏈表的節(jié)點(diǎn)數(shù):");
scanf("%d",&n);
printf("輸入第1個(gè)結(jié)點(diǎn)的數(shù)據(jù):");
scanf("%d",&p->data);
i++;
while(i<=n)
{ q=(struct List *)malloc(ND);
p->next=q;
printf("輸入第%d個(gè)結(jié)點(diǎn)的數(shù)據(jù):",i);
scanf("%d",&q->data);
p=q;
i++;
} /*從鍵盤輸入鏈表數(shù)據(jù)*/
q->next=NULL;
q=p=head;
printf("所輸入的鏈表數(shù)據(jù):");
while(p!=NULL)
{ printf("%6d",p->data);
p=p->next;
}
printf("\n");
printf("輸入要?jiǎng)h除第幾個(gè)結(jié)點(diǎn):");
scanf("%d",&e);
p=head;
i=1;
if(e>n||e<1)
{ printf("ERROR!!!!!!!\n");
return;
}
else
{ if(e==1)
{ p=p->next;
free(head);
head=p;
}
else
{ while(p->next&&i<e-1)
{ p=p->next;
i++;
}
q=p->next;
p->next=q->next;
free(q);
p=head;
}
} /*刪除鏈表中指定的結(jié)點(diǎn)*/
printf("刪除后的鏈表數(shù)據(jù):");
while(p!=NULL)
{ printf("%6d",p->data);
p=p->next;
}
printf("\n輸入要在第幾個(gè)結(jié)點(diǎn)前插入:");
scanf("%d",&e);
printf("輸入所要插入的數(shù)據(jù):");
scanf("%d",&a);
p=head;
i=1;
if(e>n||e<1)
{ printf("ERROR!!!!!!!\n");
return;
}
else
{ if(e==1)
{ q=(struct List *)malloc(ND);
q->data=a;
q->next=p;
head=p=q;
}
else
{ while(p->next&&i<e-1)
{ p=p->next;
i++;
}
q=(struct List *)malloc(ND);
q->data=a;
q->next=p->next;
p->next=q;
p=head;
}
} /*在指定結(jié)點(diǎn)前插入新結(jié)點(diǎn)*/
printf("插入結(jié)點(diǎn)后的鏈表:");
while(p!=NULL)
{ printf("%6d",p->data);
p=p->next;
}
printf("\n");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -