學習筆記——C語言實現單鏈表的基本操作:建立、輸出、插入結點、刪除結點、逆序鏈表

來源:互聯網
上載者:User

鏈表是最簡單的一種資料結構,是每個軟體開發人員必須掌握的,也是企業招聘時最常考的內容,因此,在這裡總結一下單鏈表的一些基本操作。

註:這裡是單向無環鏈表,頭結點中存放了資料,換句話說就是頭結點和其他結點沒什麼區別,你也可以說成不帶頭結點的鏈表。下面就是鏈表操作的原始碼:

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

typedef struct Node
{
    int data;
    struct Node *next;
}Lnode;

void printList(Lnode *head);
Lnode *createList();
Lnode *insertList(Lnode *head,int elem,int position);
Lnode *deleteElem(Lnode *head,int elem);
Lnode *reverseList(Lnode *head);

 

int main()
{
    Lnode *head;
    int elem,position;

    head = createList();
    printList(head);

    printf("Input the element and the position:/n");
    scanf("%d %d", &elem, &position);
    head = insertList(head,elem,position);
    printList(head);

    head = reverseList(head);
    printf("After reversed:/n");
    printList(head);
   
    printf("Input the element you want to delete:/n");
    scanf("%d",&elem);
    head = deleteElem(head, elem);
    printList(head);
   
    return 0;
}

 

/*傳入表頭指標,列印鏈表*/
void printList(Lnode *head)
{
    Lnode *p = head;

    if (p == NULL)
    {
        printf("List is empty!/n");
        return;
    }
    while (p != NULL)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("/n");
}

 

/*建立一個新鏈表,返回表頭指標*/
Lnode *createList()
{
    Lnode *head, *current, *p;
    int data;

    head = (Lnode *)malloc(sizeof(Lnode));  
    p = head;

    printf("Input integers,0 to break:/n");
    scanf("%d",&data);
   
    if (data != -1)
    {
        p->data = data;
    }
    else
    {
        head->next = NULL;   //只有頭結點的情況
        return head;
    }

    while (1)
    {
        scanf("%d", &data);
        if (data == -1)
        {
            break;
        }

        current = (Lnode *)malloc(sizeof(Lnode));
        current->data = data;
        p->next = current;
        p = p->next;
    }
    p->next = NULL;

    return head;
}

 

/*鏈表的插入操作,head為鏈表表頭,elem為要插入的元素,position為插入的位置
 *將elem插入至第position個結點後,position<=0時,插入至頭結點後
*/
Lnode *insertList(Lnode *head,int elem,int position)
{
    Lnode *current, *p;
    int i = position - 1;
    current = head;

    /*找到第position個節點*/
    while (i > 0 && current->next != NULL)
    {
        current = current->next;
        i--;
    }

    if (i > 0)   //結點數小於輸入的位置position
    {
        printf("Position is wrong!/n");
        return head;
    }

    p = (Lnode *)malloc(sizeof(Lnode));
    p->data = elem;
    p->next = current->next;
    current->next = p;
   
    return head;
}

 

/*刪除鏈表head中所有的元素elem,返回頭結點head*/
Lnode *deleteElem(Lnode *head,int elem)
{
    Lnode *p = head;
    Lnode *ptemp;
    int flag = 0;  //記錄便表中elem的個數

    while (1)
    {
        if (head->data == elem)  //刪除頭結點的情況
        {
            flag++;
            head = head->next;
            free(p);
            p = head;
        }
        else
        {
            break;
        }
    }

    while (p->next != NULL)
    {
        if (p->next->data == elem )
        {
            flag++;
            ptemp = p->next;
            if (p->next->next != NULL)
            {
                p->next = p->next->next;
                free(ptemp);
                ptemp = NULL;
            }
            else //最後一個結點
            {
               free(ptemp);
               ptemp =NULL;
               p->next = NULL;
               break;
            }
        }
        else  //沒有刪除結點時,查詢下一個結點
        {
            p = p->next;
        }
    }
    printf("%d number '%d' was found and deleted./n", flag, elem);
    return head;
}

 

/*將鏈表逆序,該鏈表不帶頭結點。*/
Lnode *reverseList(Lnode *head)
{
    Lnode *current, *pnext, *ptemp;

    current = head;
    pnext = head->next;
    head->next =NULL;

    while (pnext != NULL)
    {
        ptemp = pnext->next;
        pnext->next = current;
        current = pnext;
        pnext = ptemp;
    }

    head = current;
    return head;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.