数据结构学习

数据结构学习记录

1.顺序表基本运算的实现(稍做封装)

函数:

1.初始化顺序表
2.求表长度
3.按序号取元素
4.按值查询,返回地址
5.插入元素
6.删除第i个元素
7.以重建方式输入元素
8.以追加方式输入元素
9.输出元素
10.显示系统选项

代码如下:
//在本程序中,异常返回值为-1
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 100
typedef int elementType;
typedef struct sList
{
    elementType date[MAXLEN];
    int listLen;
}seqList;

void initiaList(seqList *L);                //初始化顺序表
int listLength(seqList *L);                 //求表长度
int listGetElement(seqList *L,int i);       //按序号取元素,1<=i<=n,返回对应元素
int listLocate(seqList *L,int x);           //按值查询,返回地址
int listInsert(seqList *L,int i,int x);     //插入元素,在线性表的第i个位置上插入值为x的元素
int listDelete(seqList *L,int i);           //删除第i个元素
int listInput(seqList *L);                  //以重建方式输入元素
int listInputa(seqList *L);                 //以追加方式输入元素
void listOutput(seqList *L);                //输出元素
void systemx(void);                         //显示系统选项
int user();
int main(void)
{
    seqList L1,*L;
    int len,v,i,x;
    char szBuff[3];
    L = &L1;
    initiaList(L);

    while (1)
    {
        do
        {
                v = user();
        }
        while(v == -1);
        fflush(stdin);
        switch (v)
        {
        case 0:
        exit(1);
            break;

        case 1:
        printf("请输入要插入的位置: ");
        scanf("%d",&i);
        printf("请输入要插入的元素: ");
        scanf("%d",&x);
        if(listInsert(L,i,x) == -1)
        {
            printf("插入失败!");
        }
            break;

        case 2:
        printf("请输入要删除的元素位置: ");
        scanf("%d",&i);
        if(listDelete(L,i) == -1)
        {
            printf("删除失败!");
        }
            break;

        case 3:
        initiaList(L);
            break;

        case 4:
        listOutput(L);
            break;

        case 5:
        printf("请输入要取的元素的位置:");
        scanf("%d",&i);
        if(listGetElement(L,i) == -1)
        {
            printf("取出失败!");
        }
        else
        {
            printf("%d",listGetElement(L,i));
        }
            break;

        case 6:
        printf("%d",listLength(L));
            break;

        case 7:
        printf("请输入要查询的值: ");
        scanf("%d",&x);
        if(listLocate(L,x) == -1)
        {
            printf("未找到该元素!");
        }
        else
        {
            printf("该元素地址为: %d",listLocate(L,x));
        }
            break;

        case 8:
        if(listInputa(L) == -1)
        {
                printf("元素过多,追加输入失败!");
        }
            break;

        case 9:
        if(listInput(L) == -1)
        {
                printf("元素过多,输入失败!");
        }
            break;
        }
        system("pause");
    }
    return 0;
}

int user()
{
    system("cls");
    char szBuff[3];
    systemx();
    gets(szBuff);
    if(szBuff[0] >= '0' && szBuff[0] <= '9')
    {
        return szBuff[0] - '0';
    }
    return -1;
}
void systemx()
{
    printf("Welcome use this system!\n");
    printf("1 - 插入元素\n");
    printf("2 - 删除元素\n");
    printf("3 - 重置顺序表\n");
    printf("4 - 输出顺序表\n");
    printf("5 - 按序号取元素\n");
    printf("6 - 求取顺序表长度\n");
    printf("7 - 查询某一值的地址\n");
    printf("8 - 以追加方式输入元素\n");
    printf("9 - 以重建顺序表方式输入元素\n");
    printf("0 - 退出系统\n");
}
void initiaList(seqList *L)                //初始化顺序表
{
    L -> listLen = 0;
}

int listLength(seqList *L)                 //求表长度
{
    return L -> listLen;
}

int listGetElement(seqList *L,int i)       //按序号取元素,1<=i<=n,返回对应元素
{
    if(i < 1 || i > L -> listLen)
    {
        return -1;
    }
    else
    {
        return L -> date[i-1];
    }
}

int listLocate(seqList *L,int x)           //按值查询,返回地址
{
    int i;
    for(i = 0; i < L -> listLen; i++)
    {
        if(x == L -> date[i])
        {
            return i;
        }
    }
    return -1;
}

int listInsert(seqList *L,int i,int x)     //插入元素,在线性表的第i个位置上插入值为x的元素
{
    if(L -> listLen < i || i < 1)
    {
        return -1;
    }
    int n = L -> listLen;
    for(; n >= i; n--)
    {
        L -> date[n] = L -> date[n-1];
    }
    L -> date[n] = x;
    L -> listLen++;
    return 1;
}

int listDelete(seqList *L,int i)           //删除第i个元素
{
    int n = i - 1;
    if(i < 1 || i > L -> listLen)
    {
        return -1;
    }
    for(; n < L -> listLen; n++)
    {
        L -> date[n] = L -> date[n+1];
    }
    L -> listLen--;
    return 1;
}
int listInput(seqList *L)                 //以重建方式输入元素
{
    initiaList(L);
    int n,i,t;
    printf("Please input the number of the element you want to input:");
    scanf("%d",&n);
    if(n > MAXLEN)
    {
            return -1;
    }
    for(i = 0; i < n; i++)
    {
        scanf("%d",&t);
        L -> date[i] = t;
        L -> listLen++;
    }
    return 1;
}

void listOutput(seqList *L)                //输出元素
{
    int i;
    for(i = 0; i < L -> listLen; i++)
    {
        printf("%4d",L -> date[i]);
    }
}

int listInputa(seqList *L)                //以追加方式输入元素
{
    int i,n,t;
    printf("Please input the number of the element you want to input:");
    scanf("%d",&n);
    if(L -> listLen + n > MAXLEN)
    {
            return -1;
    }
    for(i = L -> listLen; i < L -> listLen + n; i++)
    {
        scanf("%d",&t);
        L -> date[i] = t;
    }
    L -> listLen += n;
    return 1;
}

2.含头结点链表的基本运算的实现

函数:

1.初始化链表
2.求链表长度
3.按序号取元素结点,并返回结点指针
4.按值查询元素
5.插入元素
6.删除元素
(以下为测试函数)
7.尾插法建立链表(法1,法2)
8.显示链表

代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef int elementtype;
typedef struct Node                         //作为结构体封装
{
    elementtype data;
    struct Node * next;
}node;
node * initiallist(void);                   //初始化链表
int listLength(node * L);                   //求链表长度
node * getelement(node * L, int n);         //按序号取元素结点,返回结点指针
node * listLocate(node * L, int x);         //按值查询元素,返回结点指针
int listInsert(node * L, int n, int x);     //插入元素
int listDelete(node * L,int n);             //删除元素
node * listSet1(void);                      //建立链表法1
node * listSet2(void);                      //建立链表法2
void listshow(node *L);                     //显示链表
int main(void)
{
    node * head, * L;
    head = initiallist();
    L = listSet2();
    head -> next = L;
    printf("the length of the list is %d\n",listLength(head));
    printf("%d\n",getelement(head,3) -> data);
    if(listLocate(head,520) != NULL)
    printf("%d\n",listLocate(head,520) -> data);
    else
    {
        printf("Wrong element!\n");
    }
    if(listInsert(head,8,5201314) == 0)
    printf("Insert failed!\n");
    if(listDelete(head,5) == 0)
    printf("Delete Wrong!\n");
    listshow(head);
}
node * initiallist(void) //初始化链表
{
    node * L;
    L = (node *)malloc(sizeof(node));
    L -> next = NULL;
    return L;
}
int listLength(node * L) //求链表长度
{
    int i = 0;
    node * P = L -> next;
    while(P != NULL)
    {
        P = P -> next;
        i++;
    }
    return i;
}
node * getelement(node * L, int n) //按序号取元素结点,返回结点指针
{
    int i = 0;
    node * P = L;
    while(P != NULL && i != n)
    {
        P = P -> next;
        i++;
    }
    return P;
}
node * listLocate(node * L, int x) //按值查询元素,返回结点指针
{
    node * P = L -> next;
    while(P != NULL)
    {
        if(P -> data == x)
        break;
        P = P -> next;
    }
    return P;
}
int listInsert(node * L, int n, int x) //插入元素
{
    node * P = L,* N;
    int i = 1;
    while(i != n && P != NULL)
    {
        P = P -> next;
        i++;
    }
    if(P == NULL)
    return 0;
    else
    {
        N = (node *)malloc(sizeof(node));
        N -> data = x;
        N -> next = P -> next;
        P -> next = N;
    }
    return 1;
}
int listDelete(node * L,int n) //删除元素
{
    int i = 1;
    node * P = L,* T;
    while(i != n && P != NULL)
    {
        P = P -> next;
        i++;
    }
    if(P == NULL || P -> next == NULL)
    return 0;
    else
    {
        T = P -> next;
        P -> next = T -> next;
        free(T);
    }
    return 1;
}
node * listSet1(void) //建立链表法1
{
    node * head = NULL, * p = NULL, * q = NULL;
    int n,iFlag = 1;
    head = p;
    printf("Please input the data you want to type in(end when you type -1):\n");
    scanf("%d",&n);
    while(n != -1)
    {
        p = (node *)malloc(sizeof(node));
        p -> data = n;
        if(iFlag == 1)
        {
            head = p;
            p -> next = NULL;
        }
        else
        {
            q -> next = p;
            p -> next = NULL;
        }
        iFlag = 0;
        q = p;
        scanf("%d",&n);
    }
    return head;
}
node * listSet2(void) //建立链表法2
{
    int m,n,i;
    node * p, * q, * head;
    printf("Please input the number of the element you want to type in: ");
    scanf("%d",&n);
    printf("Please input the element:\n");
    for(i = 0; i < n; i++)
    {
        scanf("%d",&m);
        p = (node *)malloc(sizeof(node));
        p -> data = m;
        p -> next = NULL;
        if(i != 0)
        {
            q -> next = p;
        }
        else
        {
            head = p;
        }
        q = p;
    }
    return head;
}
void listshow(node * L) //显示链表
{
    node * p = L -> next;
    while(p != NULL)
    {
        printf("%d ",p -> data);
        p = p -> next;
    }
}

3.不含头结点链表的基本运算的实现(部分函数用了引用)

函数:

1.求链表长度
2.按序号取元素结点,并返回结点指针
3.按值查询元素,返回结点指针
4.插入元素
5.删除元素
(以下为测试函数)
6.尾插法建立链表
7.显示链表

代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef int elementtype;
typedef struct Node                     //作为结构体封装
{
    elementtype data;
    struct Node * next;
}node;
int listLength(node * L);               //求链表长度
node * getelement(node * L, int n);     //按序号取元素结点,返回结点指针
node * listLocate(node * L, int x);     //按值查询元素,返回结点指针
int listInsert(node *&L, int n, int x); //插入元素
int listDelete(node *&L,int n);         //删除元素
node * listSet1(void);                  //建立链表法1
node * listSet2(void);                  //建立链表法2
void listshow(node *L);                 //显示链表
int main(void)
{
    node * head = NULL,*&L = head;
    head = listSet2();
    printf("length : %d\n",listLength(head));
    if(getelement(head,3) == NULL)
    printf("Get failed!\n");
    else
    printf("the element : %d\n",getelement(head,3) -> data);
    if(listLocate(head,520) == NULL)
    printf("Get Locate failed!\n");
    else
    printf("locate element : %d\n",listLocate(head,520) -> data);
    if(listInsert(L,2,520) == 0)
    printf("Insert failed!\n");
    if(listDelete(L,3) == 0)
    printf("Delete failed!\n");
    listshow(head);
}
int listLength(node * L) //求链表长度
{
    int n = 0;
    node * p = L;
    while(p != NULL)
    {
        n++;
        p = p -> next;
    }
    return n;
}
node * getelement(node * L, int n) //按序号取元素结点,返回结点指针
{
    int i = 1;
    node * p = L;
    while(p != NULL && i != n)
    {
        i++;
        p = p -> next;
    }
    return p;
}
node * listLocate(node * L, int x) //按值查询元素,返回结点指针
{
    node * p = L;
    while(p != NULL && p -> data != x)
    {
        p = p -> next;
    }
    return p;
}
int listInsert(node *&L, int n, int x) //插入元素
{
    node * p,* t;
    int i = 2;
    p = (node *)malloc(sizeof(node));
    p -> data = x;
    t = L;
    if(n == 1)
    {
        p -> next = t;
        L = p;
    }
    else
    {
        while(t != NULL && i != n)
        {
            i++;
            t = t -> next;
        }
        if(t == NULL)
        return 0;
        p -> next = t -> next;
        t -> next = p;
    }
    return 1;
}
int listDelete(node *&L,int n) //删除元素
{
    int i = 2;
    node * p = L,* t;
    if(n == 1)
    {
        L = L -> next;
        free(p);
    }
    else
    {
        while(p != NULL && i != n)
        {
            i++;
            p = p -> next;
        }
        if(p == NULL)
        return 0;
        t = p -> next;
        p -> next = p -> next -> next;
        free(t);
    }
    return 1;
}
node * listSet1(void) //建立链表法1
{
    int n,iFlag = 1;
    node * p = NULL, * q = NULL, * head = NULL;
    printf("Please input the element you want to type in(end when you type -1):\n");
    scanf("%d",&n);
    while(n != -1)
    {
        p = (node *)malloc(sizeof(node));
        p -> data = n;
        p -> next = NULL;
        if(iFlag == 0)
        {
            q -> next = p;
        }
        else
        {
            head = p;
        }
        
        iFlag = 0;
        q = p;
        scanf("%d",&n);
    }
    return head;
}
node * listSet2(void) //建立链表法2
{
    int i,m,n,iFlag = 1;
    node * p = NULL, * q = NULL, * head = NULL;
    printf("Please input the number of the element you wnat to type in:");
    scanf("%d",&n);
    for(i = 0; i < n; i++)
    {
        scanf("%d",&m);
        p = (node *)malloc(sizeof(node));
        p -> data = m;
        p -> next = NULL;
        if(iFlag == 0)
        {
            q -> next = p;
        }
        else
        {
            head = p;
        }
        iFlag = 0;
        q = p;
    }
    return head;
}
void listshow(node *L) //显示链表
{
    node * p;
    p = L;
    while(p != NULL)
    {
        printf("%d ",p -> data);
        p = p -> next;
    }
}

4.栈的基本运算的实现

函数:

1.初始化栈
2.判断栈是否为空
3.取栈顶元素,成功反1,失败反0
4.判断栈是否为空,是反1,否反0
5.入栈,成功反1,失败反0
6.出栈,成功反1,失败反0
(以下为测试函数)
7.创建栈(法2与上述类似,此处不再赘余)
8.输出栈

代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef int elementtype;
#define MAXLEN 100
typedef struct sStack
{
    elementtype data[MAXLEN];
    int top;
}seqStack;

void initialStack(seqStack * S);            //初始化栈
int stackEmpty(seqStack S);                 //判断栈是否为空,是反1,否则反0
int stackTop(seqStack S,elementtype * x);   //取栈顶元素值,成功反1,失败反0
int stackFull(seqStack S);                  //判断栈是否为满,是反1,否则反0
int pushStack(seqStack * S,elementtype x);  //入栈,成功反1,失败反0
int popStack(seqStack * S,elementtype * x); //出栈,成功反1,失败反0
seqStack * creatstack(void);                //创建栈
void outputstack(seqStack S);               //输出栈
int main(void)
{
    seqStack * S;
    elementtype * x;
    S = creatstack();
    outputstack(*S);
    if(stackEmpty(*S) == 1)
    printf("Empty!\n");
    else
    {
        printf("Not Empty!\n");
    }
    if(stackTop(*S,x) == 1)
    printf("Top of the stack is %d\n",*x);
    else
    {
        printf("Top element of the stack get failed!\n");
    }
    if(stackFull(*S) == 1)
    printf("The stack is full!\n");
    else
    {
        printf("The stack is not full!\n");
    }
    if(pushStack(S,1314) == 0)
    printf("Push failed!\n");
    outputstack(*S);
    if(pushStack(S,520) == 0)
    printf("Push failed!\n");
    outputstack(*S);
    if(popStack(S,x) == 0)
    printf("Pop failed!\n");
    else
    {
        printf("The pop element is %d\n",*x);
    }
    outputstack(*S);
    free(S);
}
void initialStack(seqStack * S)            //初始化栈
{
    S -> top = -1;              //栈顶元素从data[0]开始,故应该初始化为-1
}
int stackEmpty(seqStack S)                 //判断栈是否为空
{
    if(S.top == - 1)
    return 1;
    return 0;
}
int stackTop(seqStack S,elementtype * x)   //取栈顶元素值,成功反1,失败反0
{
    if(stackEmpty(S) == 0)
    {
        *x = S.data[S.top];
        return 1;
    }
    return 0;
}
int stackFull(seqStack S)                  //判断栈是否为满
{
    if(S.top == MAXLEN - 1)
    return 1;
    return 0;
}
int pushStack(seqStack * S,elementtype x)  //入栈,成功反1,失败反0
{
    if(stackFull(*S) == 1)
    return 0;
    S -> top ++;
    S -> data[S -> top] = x;
    return 1; 
}
int popStack(seqStack * S,elementtype * x) //出栈,成功反1,失败反0
{
    if(stackEmpty(*S) == 1)
    return 0;
    *x = S -> data[S -> top];
    S -> top--;
    return 1;
}
seqStack * creatstack(void)                //创建栈
{
    seqStack * S;
    elementtype n;
    S = (seqStack *)malloc(sizeof(seqStack));
    initialStack(S);
    printf("Please input the element you want to type in(end when you type in -1):\n");
    scanf("%d",&n);
    while (n != -1)
    {
        S -> top ++;
        S -> data[S -> top] = n;
        scanf("%d",&n);
    }
    return S;
}
void outputstack(seqStack S)               //输出栈
{
    while(S.top != -1)
    {
        printf("%d ",S.data[S.top]);
        S.top--;
    }
    printf("\n");
}

5.循环队列的基本运算

函数:

1.初始化队列
2.判断队空
3.判断队满
4.取队头元素
5.入队
6.出队
7.元素个数
8.输出当前队列
9.输出当前队列(字符)

代码如下(.h文件):
#include <iostream>
#include <string.h>
typedef int elementtype;
#define MAXLEN 100
typedef struct Queue
{
    elementtype data[MAXLEN];
    int front;          //头
    int rear;           //尾
}queue;
void initialqueue(queue * Q)                //初始化队列
{
    Q -> front = 0;
    Q -> rear = 0;
}
bool queueEmpty(queue Q)                    //判断队空
{
    if(Q.rear == Q.front)
    return true;
    return false;
}
bool queueFull(queue Q)                     //判断队满
{
    if((Q.rear + 1) % MAXLEN == Q.front)
    return true;
    return false;
}
void gettop(queue Q, elementtype & n)       //取队头元素
{
    if(queueEmpty(Q))
    printf("The queue is Empty!\n");
    else
    n = Q.data[(Q.front+1)%MAXLEN];
}
bool inqueue(queue * Q, elementtype x)      //入队
{
    if(queueFull(*Q))  //队列满
    return false;
    else
    {
        Q -> rear = (Q -> rear + 1) % MAXLEN;
        Q -> data[Q -> rear] = x;
    }
    return true;
}
bool outqueue(queue * Q)                    //出队
{
    if(queueEmpty(*Q))
    {
        printf("The queue is empty!\n");
        return false;
    }
    Q -> front = (Q -> front + 1) % MAXLEN;
    return true;
}
int amount(queue Q)                         //元素个数
{
    return (Q.rear - Q.front + MAXLEN) % MAXLEN;
}
void output(queue Q)                        //输出当前队列
{
    while(Q.front != Q.rear)
    {
        printf("%d ",Q.data[Q.front+1]);
        Q.front++;
    }
    printf("\n");
}
void output_(queue Q)                        //输出当前队列
{
    while(Q.front != Q.rear)
    {
        printf("%c ",Q.data[Q.front+1]);
        Q.front++;
    }
    printf("\n");
}
void _1(queue * Q)                                   //第一题
{
    initialqueue(Q);
    printf("Initial succeed!\n");

    system("pause");
    fflush(stdin);
    system("cls");
}
void _2(queue * Q)                                   //第二题
{
    initialqueue(Q);

    int n;
    printf("Please input the element you want to type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        inqueue(Q,n);
        scanf("%d",&n);
    }
    output(*Q);
    if(queueEmpty(*Q))
    printf("The queue is empty!\n");
    else
    printf("The queue is not empty!\n");

    system("pause");
    fflush(stdin);
    system("cls");
}
void _3(queue * Q)                                   //第三题
{
    initialqueue(Q);

    int n,x;
    printf("Please input the amount of the elements you want to type in: ");
    scanf("%d",&n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&x);
        inqueue(Q,x);
    }
    if(queueFull(*Q))
    printf("The queue is full!\n");
    else
    printf("The queue is not full!\n");

    initialqueue(Q);
    for(int i = 1; i < 100; i++)
    {
        inqueue(Q,i);
    }
    if(queueFull(*Q))
    printf("The queue is full!\n");
    else
    printf("The queue is not full!\n");

    system("pause");
    fflush(stdin);
    system("cls");
}
void _4(queue * Q)                                   //第四题
{
    initialqueue(Q);

    int n;
    char m;
    printf("Please input the element you want to type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        inqueue(Q,n);
        scanf("%d",&n);
    }
    output(*Q);

    initialqueue(Q);
    printf("Please input the element you want to type in(end when you type '0'):\n");
    scanf("%c",&m);
    n = m;
    while(n != 48)
    {
        if(n != 32 && n != 10)
        inqueue(Q,n);
        scanf("%c",&m);
        n = m;
    }
    output_(*Q);

    system("pause");
    fflush(stdin);
    system("cls");
}
void _5(queue * Q)                                   //第五题
{
    initialqueue(Q);

    int n,x;
    printf("Please input the element you want to type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        inqueue(Q,n);
        scanf("%d",&n);
    }
    output(*Q);
    printf("type 1 -> outqueue\n");
    scanf("%d",&x);
    while(x == 1)
    {
        outqueue(Q);
        output(*Q);
        scanf("%d",&x);
    }

    system("pause");
    fflush(stdin);
    system("cls");
}
void _6(queue * Q)                                   //第六题
{
    initialqueue(Q);

    int n,x,&m = x;
    printf("Please input the element you want to type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        inqueue(Q,n);
        scanf("%d",&n);
    }
    output(*Q);
    gettop(*Q,m);
    printf("The top element is %d\n",x);

    system("pause");
    fflush(stdin);
    system("cls");
}
void _7(queue * Q)                                   //第七题
{
    initialqueue(Q);

    int n;
    printf("Please input the element you want to type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        inqueue(Q,n);
        scanf("%d",&n);
    }
    output(*Q);
    printf("The amount of the elements in the queue is %d\n",amount(*Q));

    system("pause");
    fflush(stdin);
    system("cls");
}
void _8(queue * Q)                                   //第八题
{
    initialqueue(Q);

    int n;
    while(true)
    {
        printf("Please input a number: ");
        scanf("%d",&n);
        if(n % 2 == 1)
        {
            inqueue(Q,n);
            output(*Q);
        }
        else if(n % 2 == 0 && n != 0)
        {
            outqueue(Q);
            output(*Q);
        }
        else
        {
            output(*Q);
            break;
        }
    }

    system("pause");
    fflush(stdin);
    system("cls");
}

6.链队的基本运算

函数:

1.初始化
2.判断是否队空
3.销毁链表
4.输出链表队列
5.输出链表队列(字符)
6.出队
7.入队

代码如下(.h文件):
#include <iostream>
#define MAXLEN 100
typedef int elementtype;
typedef struct slist
{
    elementtype data;
    struct slist * next;
}node;
void initiallist(node *&L)                      //初始化
{
    L = NULL;
    printf("Initial succeed!\n");
}
bool nodeempty(node *L)                         //判断是否队空
{
    if(L == NULL)
    {
        printf("The List is Empty!\n");
        return true;
    }
    else
    {
        return false;
    }   
}
void destory(node *&L)                          //销毁链表(头结点改变,需要用指针的引用)
{
    if(nodeempty(L));
    else
    {
        node * u = L -> next;
        node * p;
        while(u != NULL)
        {
            p = u -> next;
            delete u;
            u = p;
        }
        delete L;
        L = NULL;
    }
    printf("Destory Succeed!\n");
}
void output2(node * L)                          //输出链表队列(整型)
{
    if(nodeempty(L))
    printf("The List is Empty!\n");
    node * u = L;
    while(u != NULL)
    {
        printf("%d ",u -> data);
        u = u -> next;
    }
    printf("\n");
}
void output2_(node *&L)                         //输出链表队列(字符)
{
    if(nodeempty(L))
    printf("The List is Empty!\n");
    node * u = L;
    while(u != NULL)
    {
        printf("%c ",u -> data);
        u = u -> next;
    }
    printf("\n");
}
bool outlist(node *&L)                          //出队
{
    if(nodeempty(L))
    return false;
    else
    {
        node * u = L;
        L = L -> next;
        delete u;
    }
    return true;
}
void inlist(node *&L,elementtype x)             //入队
{
    if(L == NULL)
    {
        L = new node;
        L -> data = x;
        L -> next = NULL;
    }
    else
    {
        node * u = L, * p;
        while(u -> next != NULL)
        {
            u = u -> next;
        }
        p = new node;
        p -> data = x;
        p -> next = u -> next;
        u -> next = p;
    }
}
int amount2(node *&L)
{
    int amount = 0;
    node * u = L;
    while(u != NULL)
    {
        amount++;
        u = u -> next;
    }
    return amount;
}
void _9(node *&L)               //第一题
{
    destory(L);
    initiallist(L);
    printf("Initial Succeed!\n");

    system("pause");
    fflush(stdin);
    system("cls");
}
void _10(node *&L)              //第二题
{
    destory(L);
    initiallist(L);

    int n;
    node * u;
    printf("Please input the element you want type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        u = new node;
        u -> data = n;
        u -> next = L;
        L = u;
        scanf("%d",&n);
    }
    output2(L);

    nodeempty(L);

    system("pause");
    fflush(stdin);
    system("cls");
}
void _11(node *&L)              //第三题
{
    destory(L);
    initiallist(L);

    int n;
    node * u, * p;
    printf("Please input the element you want type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        u = new node;
        u -> data = n;
        if(L == NULL)
        {
            u -> next = L;
            L = u;
            p = L;
        }
        else
        {
            u -> next = p -> next;
            p -> next = u;
            p = u;
        }
        scanf("%d",&n);
    }
    output2(L);

    destory(L);
    initiallist(L);

    int m;
    char s;
    printf("Please input the element you want type in(end when you type '0'):\n");
    scanf("%c",&s);
    m = s;
    while(m != 48)
    {
        if(m != 32 && m != 10)
        {
            u = new node;
            u -> data = s;
            if(L == NULL)
            {
                u -> next = L;
                L = u;
                p = L;
            }
            else
            {
                u -> next = p -> next;
                p -> next = u;
                p = u;
            }
        }
        scanf("%c",&s);
        m = s;
    }
    output2_(L);

    system("pause");
    fflush(stdin);
    system("cls");
}
void _12(node *&L)              //第四题
{
    destory(L);
    initiallist(L);

    int n,x;
    node * u, * p;
    printf("Please input the element you want type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        u = new node;
        u -> data = n;
        if(L == NULL)
        {
            u -> next = L;
            L = u;
            p = L;
        }
        else
        {
            u -> next = p -> next;
            p -> next = u;
            p = u;
        }
        scanf("%d",&n);
    }
    output2(L);

    printf("type 1 -> outqueue\n");
    scanf("%d",&x);

    while(x == 1)
    {
        outlist(L);
        output2(L);
        scanf("%d",&x);
    }

    system("pause");
    fflush(stdin);
    system("cls");
}
void _13(node *&L)              //第五题
{
    destory(L);
    initiallist(L);

    int n;
    node * u, * p;
    printf("Please input the element you want type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        u = new node;
        u -> data = n;
        if(L == NULL)
        {
            u -> next = L;
            L = u;
            p = L;
        }
        else
        {
            u -> next = p -> next;
            p -> next = u;
            p = u;
        }
        scanf("%d",&n);
    }
    output2(L);

    printf("The first element in the list is %d\n",L -> data);

    system("pause");
    fflush(stdin);
    system("cls");
}
void _14(node *&L)              //第六题
{
    destory(L);
    initiallist(L);

    int n;
    node * u, * p;
    printf("Please input the element you want type in(end when you type 9999):\n");
    scanf("%d",&n);
    while(n != 9999)
    {
        u = new node;
        u -> data = n;
        if(L == NULL)
        {
            u -> next = L;
            L = u;
            p = L;
        }
        else
        {
            u -> next = p -> next;
            p -> next = u;
            p = u;
        }
        scanf("%d",&n);
    }
    output2(L);

    printf("The amount of the list is %d\n",amount2(L));
    system("pause");
    fflush(stdin);
    system("cls");
}
void _15(node * L)              //第七题
{
    destory(L);
    initiallist(L);

    int n;
    while(true)
    {
        printf("Please input a number: ");
        scanf("%d",&n);
        if(n % 2 == 1)
        {
            inlist(L,n);
            output2(L);
        }
        else if(n % 2 == 0 && n != 0)
        {
            outlist(L);
            output2(L);
        }
        else
        {
            output2(L);
            break;
        }
    }

    system("pause");
    fflush(stdin);
    system("cls");
}

7.树的基本运算

函数:

…待更

总结:希望能从数据结构里活着游出来,加油啊!!!

最后更新时间:2021/5/18 13:18

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值