功能菜单:
******************************************************************
** 链表操作菜单 **
** 1、创建链表并输入结点数据(包含num和goal,num==0结束) **
** 2、输出各结点数据 **
** 3、查询第i个结点的值 **
** 4、在num为i的结点之后插入一个值为x的结点 **
** 5、删除num为i的结点 **
** 6、链表逆序 **
** 0、输入0退出程序 **
******************************************************************
直接上代码
#include <stdio.h>
#include <stdlib.h>
struct Student{
int num;
int goal;
struct Student *next;
};
void manu(){
printf("******************************************************************\n");
printf("**\t\t\t链表操作菜单 **\n");
printf("**\t1、创建链表并输入结点数据(包含num和goal,num==0结束) **\n");
printf("**\t2、输出各结点数据 **\n");
printf("**\t3、查询第i个结点的值 **\n");
printf("**\t4、在num为i的结点之后插入一个值为x的结点 **\n");
printf("**\t5、删除num为i的结点 **\n");
printf("**\t6、链表逆序 **\n");
printf("**\t0、输入0退出程序 **\n");
printf("******************************************************************\n");
}
struct Student *create(void){
int n=0,cacu;
struct Student *head;
struct Student *p1,*p2;
p1=p2=(struct Student *)malloc(sizeof(struct Student));
head=NULL;
scanf("%d",&p1->goal);
cacu=1;
while(cacu!=0&&(p1->goal!=0)){
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(sizeof(struct Student));
scanf("%d",&p1->goal);
}
p2->next=NULL;
return(head);
}
void print(struct Student *head){
struct Student *ph;
ph=head;
if(head!=NULL){
printf("该链表中各节点依次为:");
do{
printf("\t%d\t",ph->goal);
ph=ph->next;
}while(ph!=NULL);
printf("\n");
}
// while((ph->num!=0)&&(ph->next!=NULL)){
// printf("%d\n",ph->goal);
// ph=(ph->next);
// }
}
int geshu(struct Student *head){
int flag=0;
struct Student *ph;
ph=head;
if(head!=NULL){
do{
flag++;
ph=ph->next;
}while(ph!=NULL);
}
return flag;
}
void select(struct Student *head,int i){
int ji=0;
struct Student *ph;
ph=head;
if(head!=NULL){
while(ph!=NULL){
ji++;
if(ji==i){
printf("%d",ph->goal);
}
ph=ph->next;
}
}
}
struct Student *insert(struct Student *head, struct Student *stud ,int ii){
struct Student *p0, *p1, *p2,*pp;
int j=0;
p1=head;
pp==p1;
p0=stud;
if (head==NULL){
head=p0;
p0->next=NULL;
}
else{
if(j==ii){
//printf("!!!!!!");
head=p0;
p0->next=p1;
// while (p1->next!=NULL){
// p2=p1;
// p1=p1->next;
// }
//printf("@@\n");
}else{
while ((j<ii) && (p1->next!=NULL)){
p2=p1;
p1=p1->next;
j++;
}
if ( ii <= j){
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else {
p1->next=p0;
p0->next=NULL;
}
}
}
return(head);
}
void delet(struct Student *head,int i){
struct Student *p1, *p2;
int j;
p1 = head;
if(geshu(head)>1){
if(i!=1){
for ( j=1; j<i && p1->next!=NULL; ++j ){
p2 =p1;
p1 = p1->next;
}
if ( j==i && p1->next != NULL ){
p2->next = p1->next;
free(p1);
head=p2;
printf("已删除\n");
}
}else{
p2 =p1;
p1 = p1->next;
p2->next = p1->next;
free(p1);
head=p2;
printf("已删除\n");
}
} else if(geshu(head)==1){
free(p1);
printf("已删除,链表已空\n");
}
}
struct Student *nixu(struct Student *head){
struct Student *p,*q,*pr;
p = head;
q = NULL;
head = NULL;
while(p){
pr = p->next;
p->next = q;
q = p;
p = pr;
}
head = q;
print(head);
return head;
}
int main(int argc, char *argv[]) {
while(1){
manu();
int j;
struct Student *pt;
scanf("%d",&j);
if(j==1){
pt=create();
//print(pt);
printf("\n");
}else if(j==2){
print(pt);
printf("结点个数为%d\n",geshu(pt));
}else if(j==3){
int shu;
printf("请输入查询的结点数:");
scanf("%d",&shu);
printf("该结点对应值为:");
select(pt,shu);
printf("\n");
}else if(j==4){
int cha;
struct Student *pq;
printf("请输入要在第几个结点后插入:");
scanf("%d",&cha);
printf("插入数据:");
pq=(struct Student *)malloc(sizeof(struct Student));
scanf("%d",&pq->goal);
pt=insert(pt,pq,cha);
printf("\n");
}else if(j==5){
int dele;
printf("请输入要删除的结点数:");
scanf("%d",&dele);
delet(pt,dele);
printf("\n");
}else if(j==6){
printf("逆序后链表各结点值为:\n");
pt=nixu(pt);
}else if(j==0){
exit(0);
}else{
printf("输入有误!\n");
}
}
return 0;
}