1.2.c

来自「自己写的数据结构课程程序」· C语言 代码 · 共 89 行

C
89
字号
#include<stdio.h>
#define LENGTH 100
#define INCREASE 10
#define NULL 0
struct linklist
{int data;
 struct linklist *next;
};
struct linklist L;
void Init_linklist()
{int i;
 char c;
 struct linklist *s,*r;
 L.next=NULL;
 r=&L;
 for(i=0;i<LENGTH;i++)
   {printf("please input the data:\n");
    s=(struct linklist *)malloc(sizeof(L));
    scanf("%d",&s->data);
    getchar();
    s->next=NULL;
    r->next=s;
    r=s;
    printf("continue?(y/n)\n");
    scanf("%c",&c);
    if(c=='n')
    i=LENGTH;
   }
}
struct linklist* Seek_linklist(int e)
{struct linklist *pre,*p;
 pre=&L;
 p=L.next;
 while(p!=NULL&&p->data!=e)
   {pre=p;
    p=p->next;
   }
 if(p==NULL)
   return NULL;
 else
   return pre;
}
void Insert_linklist()
{int e;
 struct linklist *s,*pre;
 printf("please input the element before which you want insert:\n");
 scanf("%d",&e);
 pre=Seek_linklist(e);
 if(pre==NULL)
   printf("the element is wrong!");
 else
   {s=(struct linklist*)malloc(sizeof(L));
    printf("please input the data:\n");
    scanf("%d",&s->data);
    s->next=pre->next;
    pre->next=s;
   }

}
void Delete_linklist()
{int e;
 struct linklist *pre,*p;
 printf("please input the element which you want delete:\n");
 scanf("%d",&e);
 getchar();
 pre=Seek_linklist(e);
 p=pre->next;
 pre->next=pre->next->next;
 free(p);
}

void Display_linklist()
{struct linklist *p=L.next;
 while(p!=NULL)
   {printf("%d ",p->data);
    p=p->next;
   }
 printf("\n");
}
void main(void)
{Init_linklist();
 Display_linklist();
 Insert_linklist();
 Display_linklist();
 Delete_linklist();
 Display_linklist();
 getchar();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?