單向加頭連結串列的[構建、插入、刪除、查詢、輸出]

小萌新新嚶嚶嚶發表於2020-06-13
  1 #include <stdio.h>
  2 #include <malloc.h>
  3 typedef struct lianbiao *ptr;
  4 struct lianbiao
  5 {
  6     int data;
  7     ptr next;
  8 };
  9 
 10 int main(void)
 11 {
 12     ptr create();
 13     void out(ptr p);
 14     ptr search(ptr p, int x);
 15     ptr del(ptr h, int x);
 16     ptr add(ptr head, int x);
 17     int delEl, searchEl, addEl, delResult, searchResult;
 18 
 19     ptr l = create(); //構建初始連結串列
 20 
 21     printf("初始連結串列為:"); //輸出初始連結串列
 22     out(l);
 23     printf("\n");
 24 
 25     //查詢元素
 26     printf("輸入待查詢元素:");
 27     scanf("%d", &searchEl);
 28 
 29     searchResult = search(l, searchEl);
 30     if (searchResult)
 31     {
 32         searchResult = search(l, searchEl)->data;
 33         printf("元素%d查詢成功\n", searchResult);
 34     }
 35 
 36     else
 37         printf("待查詢元素不存在!\n");
 38     printf("\n");
 39 
 40     //刪除元素
 41     printf("輸入待刪除元素:");
 42     scanf("%d", &delEl);
 43     delResult = del(l, delEl);
 44     if (delResult)
 45         printf("刪除成功!\n");
 46     else
 47         printf("待刪除元素不存在!\n");
 48     printf("\n");
 49 
 50     //插入元素
 51     printf("輸入待插入元素:");
 52     scanf("%d", &addEl);
 53     add(l, addEl);
 54 }
 55 
 56 ptr create() // 構建連結串列
 57 {
 58     ptr p, head, last;
 59     int x;
 60     //建立頭結點
 61     head = (ptr)malloc(sizeof(ptr));
 62     head->data = 0;
 63     last = head;
 64     last->next = NULL;
 65 
 66     //新增連結串列元素
 67     printf("構建初始連結串列(輸入0以結束):");
 68     scanf("%d", &x);
 69     while (x != 0)
 70     {
 71         p = (ptr)malloc(sizeof(ptr));
 72         p->data = x;
 73 
 74         //把新元素插入連結串列
 75         if (x > last->data) //待插入元素大於所有節點
 76         {
 77             last->next = p;
 78             p->next = NULL;
 79             last = p;
 80         }
 81         else if (x < head->next->data) //待插入元素小於所有節點
 82         {
 83             p->next = head->next;
 84             head->next = p;
 85         }
 86         else //一般情況
 87         {
 88             ptr f = head, s = f->next;
 89             while (x > s->data)
 90             {
 91                 f = s;
 92                 s = s->next;
 93             }
 94             p->next = s;
 95             f->next = p;
 96         }
 97 
 98         scanf("%d", &x);
 99     }
100     return head;
101 }
102 
103 void out(ptr p) //輸出連結串列
104 {
105     p = p->next;
106     while (p != NULL)
107     {
108         printf("%5d", p->data);
109         p = p->next;
110     }
111     printf("\n");
112 };
113 
114 ptr search(ptr p, int x) //搜尋元素
115 {
116     while (p != NULL)
117     {
118         if (p->data == x)
119             return p;
120         p = p->next;
121     }
122     return 0;
123 }
124 
125 ptr del(ptr h, int x) //刪除元素
126 {
127     ptr f, s;
128     f = h;
129     s = h->next;
130     while (s->data != x)
131     {
132         f = s;
133         s = s->next;
134         if (s->next == NULL)
135             break;
136     }
137 
138     if (s->data == x)
139     {
140         f->next = s->next;
141         free(s);
142         return 1;
143     }
144     else
145         return 0;
146 }
147 
148 ptr add(ptr head, int x)
149 {
150     ptr p, last, yyy;
151     yyy = head;
152     while (yyy->next != NULL)
153         yyy = yyy->next;
154     last = yyy;
155     last->next = NULL;
156 
157     p = (ptr)malloc(sizeof(ptr));
158     p->data = x;
159 
160     //把新元素插入連結串列
161     if (x > last->data) //待插入元素大於所有節點
162     {
163         last->next = p;
164         p->next = NULL;
165         last = p;
166     }
167     else if (x < head->next->data) //待插入元素小於所有節點
168     {
169         p->next = head->next;
170         head->next = p;
171     }
172     else //一般情況
173     {
174         ptr f = head, s = f->next;
175         while (x > s->data)
176         {
177             f = s;
178             s = s->next;
179         }
180         p->next = s;
181         f->next = p;
182     }
183 
184     printf("新增完成!\n現在的連結串列為:");
185     out(head);
186     printf("\n");
187 }

 

#include <stdio.h>#include <malloc.h>typedef struct lianbiao *ptr;struct lianbiao{    int data;    ptr next;};
int main(void){    ptr create();    void out(ptr p);    ptr search(ptr p, int x);    ptr del(ptr h, int x);    ptr add(ptr head, int x);    int delEl, searchEl, addEl, delResult, searchResult;
    ptr l = create(); //構建初始連結串列
    printf("初始連結串列為:"); //輸出初始連結串列    out(l);    printf("\n");
    //查詢元素    printf("輸入待查詢元素:");    scanf("%d", &searchEl);
    searchResult = search(l, searchEl);    if (searchResult)    {        searchResult = search(l, searchEl)->data;        printf("元素%d查詢成功\n", searchResult);    }
    else        printf("待查詢元素不存在!\n");    printf("\n");
    //刪除元素    printf("輸入待刪除元素:");    scanf("%d", &delEl);    delResult = del(l, delEl);    if (delResult)        printf("刪除成功!\n");    else        printf("待刪除元素不存在!\n");    printf("\n");
    //插入元素    printf("輸入待插入元素:");    scanf("%d", &addEl);    add(l, addEl);}
ptr create() // 構建連結串列{    ptr p, head, last;    int x;    //建立頭結點    head = (ptr)malloc(sizeof(ptr));    head->data = 0;    last = head;    last->next = NULL;
    //新增連結串列元素    printf("構建初始連結串列(輸入0以結束):");    scanf("%d", &x);    while (x != 0)    {        p = (ptr)malloc(sizeof(ptr));        p->data = x;
        //把新元素插入連結串列        if (x > last->data) //待插入元素大於所有節點        {            last->next = p;            p->next = NULL;            last = p;        }        else if (x < head->next->data) //待插入元素小於所有節點        {            p->next = head->next;            head->next = p;        }        else //一般情況        {            ptr f = head, s = f->next;            while (x > s->data)            {                f = s;                s = s->next;            }            p->next = s;            f->next = p;        }
        scanf("%d", &x);    }    return head;}
void out(ptr p) //輸出連結串列{    p = p->next;    while (p != NULL)    {        printf("%5d", p->data);        p = p->next;    }    printf("\n");};
ptr search(ptr p, int x) //搜尋元素{    while (p != NULL)    {        if (p->data == x)            return p;        p = p->next;    }    return 0;}
ptr del(ptr h, int x) //刪除元素{    ptr f, s;    f = h;    s = h->next;    while (s->data != x)    {        f = s;        s = s->next;        if (s->next == NULL)            break;    }
    if (s->data == x)    {        f->next = s->next;        free(s);        return 1;    }    else        return 0;}
ptr add(ptr head, int x){    ptr p, last, yyy;    yyy = head;    while (yyy->next != NULL)        yyy = yyy->next;    last = yyy;    last->next = NULL;
    p = (ptr)malloc(sizeof(ptr));    p->data = x;
    //把新元素插入連結串列    if (x > last->data) //待插入元素大於所有節點    {        last->next = p;        p->next = NULL;        last = p;    }    else if (x < head->next->data) //待插入元素小於所有節點    {        p->next = head->next;        head->next = p;    }    else //一般情況    {        ptr f = head, s = f->next;        while (x > s->data)        {            f = s;            s = s->next;        }        p->next = s;        f->next = p;    }
    printf("新增完成!\n現在的連結串列為:");    out(head);    printf("\n");}

相關文章