亚洲免费人人妻人人,cao78在线视频,福建一级毛片,91精品视频免费观看,高清另类图片操逼,日本特黄特色大片免费看,超碰欧美人人澡曰曰澡夜夜泛

單鏈表 -電腦資料

電腦資料 時(shí)間:2019-01-01 我要投稿
【www.msguai.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; // 記住括號(hào)不能少}int ListLength(SLNode *head)//返回結(jié)點(diǎn)的個(gè)數(shù),不算頭結(jié)點(diǎn){        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有問(wèn)題!\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有問(wèn)題!\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)//最后一個(gè)節(jié)點(diǎn)中的數(shù)據(jù)與x相等    {        p = head;        while (p->next->next != NULL)        {            p = p->next;        }        p->next = NULL;    }}void ListSort(SLNode *head) //就地排序不開(kāi)辟新的內(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é)點(diǎn)        p = p->next;           q->next = pre->next; //將結(jié)點(diǎn)q插入到結(jié)點(diǎn)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)//銷(xiāo)毀單鏈表中所有的結(jié)點(diǎn){    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個(gè)位置插入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鏈表的長(zhǎng)度 : %d \n",ListLength(list));    Destory(&list);    system("pause");    return 0;}

最新文章