|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct student
- {
- int num;
- int score;
- struct student *next;
- };
- struct student *chuangjian() {
- struct student *head;
- struct student *p1, *p2;
- head = (struct student*)malloc(sizeof(struct student));
- int n;
- head->next = NULL;
- p2 = head;
- printf("input the num:");
- scanf("%d", &n);
- while(n != 0) {
- p1 = (struct student*)malloc(sizeof(struct student));
- p1->num = n;
- printf("input the score:");
- scanf("%d", &p1->score);
- printf("\n");
- p1->next = NULL;
- p1 = p1->next;
- p2 = p1;
- printf("input the num:");
- scanf("%d", &n);
- }
- return head;
- }
- void print(struct student *head)
- {
- struct student *a;
- a=head->next;
- while(a)
- {printf("%d\t%d\n",a->num,a->score);
- a->next=a;}
- }
- struct student *del(struct student *head, int num) {
- struct student *p1, *p2;
- if(head = NULL) {
- printf("\nthis is null!\n");
- goto END;
- }
- p1 = head;
- while(p1->num != num && p1->next != NULL) {
- p2 = p1;
- p1 = p1->next;
- }
- if(num == p1->num) {
- p2->next = p1->next;
- printf("\nDelete num:%d succeed!\n", num);
- }
- else
- {
- printf("%d not been found!\n", num);
- }
- END:
- return head;
- }
- int main() {
- struct student *head=NULL;
- int n;
- head = chuangjian();
- print(head);
- printf("input the num for delete:");
- scanf("%d", &n);
- del(head, n);
- return 0;
- }
复制代码 |
|