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);
}
//垃圾题目,无任何意义,但我还是做了。。。

  

猜你喜欢

转载自www.cnblogs.com/Jie-Fei/p/10134267.html