1. 程式人生 > >7-1 jmu-ds-單鏈表的基本運算 (15 分)

7-1 jmu-ds-單鏈表的基本運算 (15 分)

實現單鏈表的基本運算:初始化、插入、刪除、求表的長度、判空、釋放。
(1)初始化單鏈表L,輸出L->next的值;
(2)依次採用尾插法插入元素:輸入分兩行資料,第一行是尾插法需要插入的字元資料的個數,第二行是具體插入的字元資料。
(3)輸出單鏈表L;
(4)輸出單鏈表L的長度;
(5)判斷單鏈表L是否為空;
(6)輸出單鏈表L的第3個元素;
(7)輸出元素a的位置;
(8)在第4個元素位置上插入‘x’元素;
(9)輸出單鏈表L;
(10)刪除L的第3個元素;
(11)輸出單鏈表L;
(12)釋放單鏈表L。

輸入格式:

兩行資料,第一行是尾插法需要插入的字元資料的個數,第二行是具體插入的字元資料。

輸出格式:

按照題目要求輸出

輸入樣例:

5
a b c d e

  

輸出樣例:

0
a b c d e
5
no
c
1
a b c x d e
a b x d e

  

#第一遍做
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

//函式狀態碼定義
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int  Status;
typedef char  ElemType; //假設線性表中的元素均為整型

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList; //迴圈單鏈表型別定義與單鏈表定義相同,區別在尾節點next取值
Status BeginList(LinkList &L)
{
    L = (LNode *)malloc(sizeof(LNode));
    L->next = NULL;
    return OK;
}
Status CreatList(LinkList &L, int n)
{
    LNode *p = L;
    LNode *temp;
    getchar();
    while(n>0){
        temp = (LNode *)malloc(sizeof(LNode));
        temp->next = NULL;
        if(n == 1)
            scanf("%c", &(temp->data));
        else
            scanf("%c ", &(temp->data));
        p->next = temp;
        p = temp;
        //printf("%c\n", temp->data);
        --n;
    }
    p->next = NULL;
}
void PrintList(LinkList &L)
{
    LNode *p;
    p = (LNode *)malloc(sizeof(LNode));
    p = L->next;
    while(p!=NULL){
        if(p->next != NULL)
            printf("%c ", p->data);
        else
            printf("%c\n", p->data);
            p = p->next;
    }
}
void PrintLen(LinkList &L)
{
    int len =0;
    LNode *p = L->next;
    while(p!=NULL){
        ++len;
        p = p->next;
    }
    printf("%d\n", len);
}
void IsEmpty(LinkList L)
{
    if(L->next == NULL)
        printf("yes\n");
    else
        printf("no\n");
}
void PrintElem(LinkList L, int pos)
{
   // L = L->next;
    while(pos--){
        L = L->next;
    }
    printf("%c\n", L->data);
}
void PrintPos(LinkList L, char elem)
{
    int len = 1;
    L = L->next;
    while(L->data != elem){
        len++;
        L = L->next;
    }
    printf("%d\n", len);
}
void Insert(LinkList &L, int pos, char elem)
{
    int len = 1;
    LNode *p = L->next;
    LNode *pre_p = L;
    LNode *temp;
    temp = (LNode *)malloc(sizeof(LNode));
    temp->data = elem;
    while(len<pos){
        p = p->next;
        pre_p = pre_p->next;
        len++;
    }
    pre_p->next = temp;
    temp->next = p;
}
void DeleteElem(LinkList &L, int pos)
{
    int len = 1;
    LNode *p = L->next;
    LNode *pre_p = L;
    while(len<pos){
        p = p->next;
        pre_p = pre_p->next;
        ++len;
    }
    pre_p->next = p->next;
    free(p);
    p = NULL;
}

int main()
{
    LinkList L;
    int num;

    BeginList(L);//初始化列表
    printf("%d\n", L->next);

    scanf("%d", &num);
    CreatList(L,num);//儲存資料

    PrintList(L);//輸出列表

    PrintLen(L);// 輸出長度

    IsEmpty(L);//判斷是否為空

    PrintElem(L,3);//輸出第三個位置元素

    PrintPos(L,'a');// 輸出對應位置的資料

    Insert(L,4,'x');// 在相應位置插入相應的資料

    PrintList(L);//輸出列表

    DeleteElem(L, 3);// 刪除相應位置的資料

    PrintList(L);//輸出列表
    free(L);
}

  第二次做:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode
{
    char data;
    struct LNode* next;
}LNode, *List;
void InitList(List &L)//初始化連結串列
{
    L = (LNode *)malloc(sizeof(LNode));
    L->next = NULL;
    printf("%d\n", L->next);
}
void CreatList(List &L)//構造連結串列
{
    int N;
    scanf("%d", &N);
    LNode *p, *temp;
    p = L;
    getchar();
    for(int i=0; i<N; i++){
        temp = (LNode*)malloc(sizeof(LNode));
        temp->next = NULL;
        scanf("%c", &temp->data);getchar();
        p->next = temp;
        p = temp;
    }
}
void CoutList(List &L)//輸出連結串列
{
    LNode *p = L->next;
    while(p != NULL){
        if(p->next != NULL)
            printf("%c ", p->data);
        else
            printf("%c\n", p->data);
        p = p->next;
    }
}
void CoutLen(List L)//輸出連結串列長度
{
    int len = 0;
    while(L->next != NULL){
        len++;
        L = L->next;
    }
    printf("%d\n", len);
}
void JudgEmpty(List L)//判斷是否為空
{
    if(L->next == NULL)
        printf("yes\n");
    else
        printf("no\n");
}
void CoutEemOfPos(List L, int pos)//輸出對應位置的元素
{
    int len = 1;
    L = L->next;
    while(L!=NULL && len<pos){
        len++;
        L = L->next;
    }
    printf("%c\n", L->data);
}
void CoutPosOfEem(List L, char c)//輸出對應元素的位置
{
    int len = 1;
    L = L->next;
    while(L->data != c && L!=NULL){
        L = L->next;
        len++;
    }
    if(L == NULL)
        printf("Don'have this char!\n");
    else
        printf("%d\n", len);
}
void InsertInPos(List &L, int pos, char c)//在某個位置插入某個元素
{
    LNode *p = L->next;
    LNode *temp;
    temp = (LNode *)malloc(sizeof(LNode));
    temp->data = c; temp->next = NULL;
    int len = 1;
    while(p!=NULL && len<pos-1){
        p = p->next;
        len++;
    }
    if(p == NULL)
        printf("Can' insert in this position!\n");
    else{
        temp->next = p->next;
        p->next = temp;
    }
}
void DeleEemOfPos(List &L, int pos)//刪除某個元素的位置
{
    LNode *p = L->next;
    int len = 1;
    while(p!=NULL && len<pos-1){
        len++;
        p = p->next;
    }
    if(p == NULL)
        printf("Can't Delete!\n");
    else
        p->next = p->next->next;
}
int main()
{
    List L;
    InitList(L);
    CreatList(L);
    CoutList(L);
    CoutLen(L);
    JudgEmpty(L);
    CoutEemOfPos(L, 3);
    CoutPosOfEem(L, 'a');
    InsertInPos(L, 4, 'x');
    CoutList(L);
    DeleEemOfPos(L, 3);
    CoutList(L);
}
//垃圾題目,無任何意義,但我還是做了。。。