天天看點

資料結構單項連結清單複習

#include<stdio.h>
#include<stdlib.h>
//函數執行後傳回的狀态值
#define OK 1
#define ERROR 0

//函數執行後傳回的狀态值
typedef int Status;
//元素的資料類型,這裡假設為int
typedef int ElemType;

//組成連結清單的單個結點的結構體
typedef struct node
{
    //存放資料
    ElemType data;
    //指向下一個結點
    struct node *next;
}Node;

//函數聲明
Node * findNodeAtX(Node *head,int x);
void loopLinkList(Node *head);

/**
 * 在連結清單第x個位置上插入元素e
 * */
Status LiskInsert(Node **Phead, int x, ElemType e)
{
    if(x == 1)
    {
        Node *p = (Node *)malloc(sizeof(Node));
        p->data = e;
        p->next = *Phead;
        *Phead = p;
        return OK;
    }
    Node *s =  findNodeAtX(*Phead,x-1);
    if(s == NULL)
    {
        printf("參數i錯誤");
        return ERROR;
    }
    else
    {
        Node *p = (Node*)malloc(sizeof(Node));
        p->data = e;
        //A->B->C
        p->next = s->next;
        s->next = p;
        return OK;
    }
}
/**
 * 删除連結清單第x個元素,有錯誤
 * */
Status removeAtX(Node **Phead, int x, ElemType *e)
{
    Node *p,*s;
    //如果删除的是第一個結點
    if(x == 1)
    {
        s = *Phead;
        if(*Phead == NULL)
            return ERROR;
        else
        {
            *Phead = (*Phead)->next;
            free(s);
            //loopLinkList(*Phead);
            return OK;
        }
    }
    p = findNodeAtX(*Phead,x-1);
    if(p==NULL)
    {
        printf("第%d個結點不存在\n",x-1);
        return ERROR;
    }
    else if(p->next == NULL)
    {
        printf("第%d個結點不存在\n",x);
        return ERROR;
    }
    else
    {
        //A->B->C
        s = p->next;
        *e = s->data;
        p->next = s->next;
        free(s);
        return OK;
    }
}
/**
 * 建立連結清單
 * */
Node * CreateList()
{
    Node *head=NULL, *p,*q;
    int length;
    printf("輸入結點個數");
    scanf("%d",&length);
    for(int i = 0; i < length; i++)
    {
        q = (Node *)malloc(sizeof(Node));
        q->next = NULL;

        if(head == NULL)
            head = q;
        else
            p->next = q;
        printf("輸入該結點存放的資料");
        scanf("%d",&q->data);
        p = q;
    }
    return head;
}
/**
 * 周遊連結清單
 * */
void loopLinkList(Node *head)
{
    Node *p = head;
    printf("連結清單中的資料\n");
    while(p!=NULL)
    {
        printf("%d\n",p->data);
        p = p->next;
    }
}
/**
 * 找到連結清單第X個節點
 * */
Node * findNodeAtX(Node *head,int x)
{
    int j = 1;
    Node *findNode = head;
    while(findNode!=NULL && j<x)
    {
        
        findNode = findNode->next;
        j++;
    }
    //如果改成if(p==NULL)return NULL;esle return p;
    //不可以如果x為負數則傳回的是head
    if(j == x)
        return findNode;
    else
        return NULL;
}
/**
 * 按值查找
 * */
Node * findElem(Node *head,ElemType find)
{
    //空指針的意思就是什麼也不指,而指向空的指針不是空指針
    Node *p = head;
    while(p!=NULL && p->data!=find)
    {
        p = p->next;
    }
    return p;
}

int  main(int argc, char const *argv[])
{
    Node *myList;
    ElemType *e;

    myList = CreateList();
    LiskInsert(&myList,1,9);
    loopLinkList(myList);
    removeAtX(&myList,1,e);    
    loopLinkList(myList);
    return 0;
}

           

運作結果

資料結構單項連結清單複習

繼續閱讀