1. 程式人生 > >C語言 連結串列的建立,插入,刪除,列印

C語言 連結串列的建立,插入,刪除,列印

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

//結構體定義
struct Node{
    char name[10];
    int score;
    struct Node *next;
};
typedef struct Node ListNode;
//函式宣告部分
ListNode* CreateList(int n);
void InsertList(ListNode *h,int i,char name[],int score, int n);
void DeleteList(ListNode *h,int i,int n);
void PrintList(ListNode *h);
//函式實現部分
ListNode* CreateList(int n){
    ListNode *head,*pre,*p;
    int i=0;
    head = (ListNode*)malloc(sizeof(ListNode));
    head->next=NULL;
    pre = head;
    for(i=0;i<=n;i++){
        printf("input the name of %d student",i);
        p = (ListNode*)malloc(sizeof(ListNode));
        scanf("%s",&p->name);
        printf("input the score of %d student",i);
        scanf("%d",&p->score);
        pre->next = p;
        pre = p;
    }
    p->next = NULL;
    return head;
}

void PrintList(ListNode *h){
    ListNode *p;
    p=h->next;
    while(p){
        printf("%s%d",p->name,p->score);
        p=p->next;
    }
}

//主函式
void main(){
    ListNode *h;
    int i=1,n,score;
    char name[10];
    while(i){
        //輸入提示資訊
        printf("1--建立新的連結串列\n");
        printf("2--新增元素\n");
        printf("3--刪除元素\n");
        printf("4--輸出當前表中的元素\n");
        printf("0--退出\n");
        scanf("%d",&i);
        switch(i){
            case 1:
                printf("n=");
                scanf("%d",&n);
                h = CreateList(n);
                printf("list elements is:\n");
                PrintList(h);
                break;
            case 2:
                printf("input the position of insert element:");
                scanf("%d",&i);  //在連結串列中的第i個位置插入
                printf("input name of the student:");
                scanf("%s",&name);
                printf("input score of the student:");
                scanf("%d",&score);
                InsertList(h,i,name,score,n);
                printf("list elements is:\n");
                PrintList(h);
                break;
            case 3:
                printf("input the position of delete element:");
                scanf("%d",&i);
                DeleteList(h,i,n);
                printf("list elelments is :\n");
                PrintList(h);
                break;
            case 4:
                printf("list elements is :\n");
                PrintList(h);
                break;
            case 0:
                return ;
                break;
            default:
                printf("ERROE! Try again!\n\n");

        }
    }
}
//插入連結串列結點
void InsertList(ListNode *h,int i,char name[],int e, int n){
    ListNode *q,*p;
    int j;
    if(i<1 || i>n+1)
        printf("ERROR! Please input again!\n");
    else
    {
        j=0;
        p=h;
        while(j<i-1){
            p=p->next;
            j++;
        }
        q = (ListNode*)malloc(sizeof(ListNode)); //為要插入的結點分配空間
        strcpy(q->name,name);
        q->score = e;
        q->next = p->next;
        p->next =q;

    }
}

//刪除連結串列結點
void DeleteList(ListNode *h,int i,int n){
    ListNode *p,*q;
    int j;
    char name[10];
    int score;
    if(i<1 || i>n)
        printf("ERROR! Try again.\n");
    else
    {
        j=0;
        p=h;
        while(j<i-1){
            p = p->next;
            j++;
        }
        q = p->next; //找到要刪除的q結點
        p->next = q->next;
        strcpy(name,q->name);
        score = q->score;
        free(q);
        printf("name=%s,score=%d\n",name,score);

    }
}