欧美另类日韩中文色综合,天堂va亚洲va欧美va国产,www.av在线播放,大香视频伊人精品75,奇米777888,欧美日本道免费二区三区,中文字幕亚洲综久久2021

單鏈表 -電腦資料

電腦資料 時間:2019-01-01 我要投稿
【www.lotusphilosophies.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)//返回結(jié)點的個數(shù),不算頭結(jié)點{        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,相當(dāng)數(shù)組的下標(biāo){    SLNode *s,*p=head;    int j = -1;    while (p->next!=NULL && j<i-1)    {        j++;        p = p->next;    }    if (j != i-1)    {        printf("參數(shù)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,相當(dāng)數(shù)組的下標(biāo){    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("參數(shù)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é)點中的數(shù)據(jù)與x相等    {        p = head;        while (p->next->next != NULL)        {            p = p->next;        }        p->next = NULL;    }}void ListSort(SLNode *head) //就地排序不開辟新的內(nèi)存{    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指向待插入的的結(jié)點        p = p->next;           q->next = pre->next; //將結(jié)點q插入到結(jié)點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)//銷毀單鏈表中所有的結(jié)點{    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("原始數(shù)據(jù):\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(" 刪除數(shù)字9:\n");    //ListDelete(list,9, &n);    ListRemove(list,9);    print(list);    printf("\n鏈表的長度 : %d \n",ListLength(list));    Destory(&list);    system("pause");    return 0;}

最新文章