亚洲一区亚洲二区亚洲三区,国产成人高清在线,久久久精品成人免费看,999久久久免费精品国产牛牛,青草视频在线观看完整版,狠狠夜色午夜久久综合热91,日韩精品视频在线免费观看

單鏈表 -電腦資料

電腦資料 時間:2019-01-01 我要投稿
【www.ishadingyu.com - 電腦資料】

   

include<stdio.h>#include<stdlib.h>#define MaxSize 100#define INSERTER 10typedef struct Node{    int data;    struct Node *next;}SLNode ;void ListIntiate(SLNode **head)  //指針的指針{     *head = (SLNode *)malloc(MaxSize*sizeof(SLNode));    (*head)->next = NULL; // 記住括號不能少}int ListLength(SLNode *head)//返回結點的個數,不算頭結點{        SLNode *p;        int size = -1;        p = head;        while (p!=NULL)        {            size++;            p = p->next;        }        return size;}//void ListInsert(SLNode *head, int x)//{//  SLNode *s;//  s = (SLNode *)malloc(sizeof(SLNode));//  s->data = x;//  s->next = head->next;//  head->next = s;//}void ListInsert(SLNode *head,int i,int x)//0<=i<=ListLength-2,相當數組的下標{    SLNode *s,*p=head;    int j = -1;    while (p->next!=NULL && j<i-1)    {        j++;        p = p->next;    }    if (j != i-1)    {        printf("參數i有問題!\n");    }    s = (SLNode *)malloc(sizeof(SLNode));    s->data = x;    s->next = p->next;    p->next = s;}void ListDelete(SLNode *head,int i,int *x)//0<=i<=ListLength,相當數組的下標{    SLNode *p,*q; int j = -1;    p = head;    while ( p->next != NULL &&  p->next->next != NULL && j<i-1 )    {        j++;        p = p->next;    }    if (j != i - 1)    {        printf("參數i有問題!\n");    }    *x = p->next->data;    q = p->next;    p->next = q->next;    free(q);}void ListRemove(SLNode *head, int x){    int i=0,n=0,flag=1;    SLNode *p = head;    while (flag != 0)    {        p = head;        i = 0;        while (p->next != NULL)        {            if (x == p->data)            {                ListDelete(head, i - 1, &n);                break;            }            i++;            p = p->next;        }        if (p->next == NULL)        {            flag = 0;        }    }    if (x == p->data)//最后一個節(jié)點中的數據與x相等    {        p = head;        while (p->next->next != NULL)        {            p = p->next;        }        p->next = NULL;    }}void ListSort(SLNode *head) //就地排序不開辟新的內存{    SLNode *curr, *pre, *p, *q;    p = head->next;     head->next = NULL;    while (p != NULL)    {        curr = head->next;        pre = head;        while (curr != NULL && curr->data <= p->data)        {            pre = curr;            curr = curr->next;        }        q = p;    //指針q指向待插入的的結點        p = p->next;           q->next = pre->next; //將結點q插入到結點pre之后        pre->next = q;    }}void print(SLNode *head){    SLNode *p;    p = head->next;    while (p != NULL)    {        printf("%d ", p->data);        p = p->next;    }}void Destory(SLNode **head)//銷毀單鏈表中所有的結點{    SLNode *p1,*p;    p = *head;    while (p!=NULL)    {        p1 = p;        p = p->next;        free(p1);    }    *head = NULL; //為了避免出現(xiàn)野指針}int main(){    SLNode *list; int n=0,i=0;    ListIntiate(&list);    for (i = 0; i < 10; i++)    {        ListInsert(list, i,9-i);    }    //for (i = 0; i < 10; i++)    //{    //  ListInsert(list, i+10, i);    //}    printf("原始數據:\n");    print(list);    printf("\n");    printf("在第10個位置插入100:\n");    ListInsert(list, 10, 100);    print(list);    printf("\n");    printf("從小到大排序:\n");    ListSort(list);    print(list);    printf("\n");    printf(" 刪除數字9:\n");    //ListDelete(list,9, &n);    ListRemove(list,9);    print(list);    printf("\n鏈表的長度 : %d \n",ListLength(list));    Destory(&list);    system("pause");    return 0;}

最新文章