实验1
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct LNode* PtrToNode;
typedef PtrToNode Position;

typedef struct LNode
{
    ElemType data;
    struct LNode * next;
}LNode, * LinkList;
/*在带头结点的单链表中第i个位置(从1开始)插入元素*/
int ListInsert_L(LinkList head, int i, ElemType e)
{
    LinkList p = head;
    LinkList s;
    int j;
    for (j = 1; j < i; j++)
    {
        if (p)p = p->next;
        else break;
    }
    if (!p|| i < 1)
    {
        printf("error!!请输入正确的i值!!\n");
        return 0;
    }
    s = (LinkList)malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;                             /*在当前结点p之后插入结点s*/
    p->next = s;
    return 1;
}
/*建立链表输入元素,用头插法建立带头结点的单链表(逆序),输入0结束*/
LinkList CreateList_L(LinkList head)
{
    ElemType temp;
    LinkList p;
    printf("请输入结点值(输入0结束):");
    fflush(stdin);  /*清除键盘缓冲区*/
    scanf("%c", &temp);
    while (temp != '0') {
        if (('A' <= temp && temp <= 'Z') || ('a' <= temp && temp <= 'z'))
        {
            p = (LinkList)malloc(sizeof(LNode)); /*生成新的结点*/
            p->data = temp;
            p->next = head->next;
            head->next = p;/*在链表头部插入结点,即头插法*/
            printf("请输入结点值(输入0结束):");
            fflush(stdin);
        }
        scanf("%c", &temp);
    }
    return head;
}

/*在带头结点的单链表中删除第i个位置(从1开始)的元素*/
int ListDel_L(LinkList head, int i)
{
    LinkList p, tmp;
    int j;
    p = head->next;
    tmp = head;
    for (j = 1; j < i; j++)
    {
        if (p)
        {
            p = p->next;/*p指向当前结点*/
            tmp = tmp->next;/*tmp指向当前结点的前趋*/
        }
        else break;
    }
    if (!p || i < 1)
    {
        printf("error!!请输入正确的i值!!\n");
        return 0;
    }
    tmp->next = p->next;              /*删除结点p*/
    free(p);
    return 1;
}

/*顺序输出链表的内容*/
void ListPint_L(LinkList head)
{
    LinkList p;
    int i = 0;
    p = head->next;
    while (p != NULL)
    {
        i++;
        printf("第%d个元素是:", i);
        printf("%c\n", p->data);
        p = p->next;
    }
}

int get_length(LinkList head)
{
    int i = 0;
    Position P = head->next;
    while (P != NULL)
    {
        i++;
        P = P->next;
    }
    return i;
}

void BubblePoint(LinkList head)
{
    Position pre;
    Position cur;
    Position next;

    int i, j;
    i = get_length(head);
    printf("length = %d\n", i);

    while (i != 1)
    {
        pre = head;
        cur = head->next;
        next = cur->next;
        j = i;
        i--;
        while (j != 1)
        {
            j--;
            if (cur->data > next->data)
            {
                cur->next = next->next;
                pre->next = next;
                next->next = cur;

                pre = next;
                next = cur->next;
            }
            else
            {
                pre = cur;
                cur = next;
                next = next->next;
            }
        }
    }
}


void main()
{
    int i;
    char cmd, e;
    LinkList head;
    head = (LinkList)malloc(sizeof(LNode));  /*头结点*/

    head->next = NULL;
    CreateList_L(head);
    BubblePoint(head);
    ListPint_L(head);
    do {
        printf("i,I...插入\n");
        printf("d,D...删除\n");
        printf("q,Q...退出\n");
        do
        {    
            fflush(stdin);
            scanf("%c", &cmd);
        } while ((cmd != 'd') && (cmd != 'D') && (cmd != 'q') && (cmd != 'Q') && (cmd != 'i') && (cmd != 'I'));
        switch (cmd)
        {
        case'i':
        case'I':
            printf("请输入你要插入的数据:");
            fflush(stdin);
            scanf("%c", &e);
            printf("请输入你要插入的位置:");
            scanf("%d", &i);
            ListInsert_L(head, i, e);
            ListPint_L(head);
            break;
        case'd':
        case'D':
            printf("请输入你要删除元素的位置:");
            fflush(stdin);  /*清楚键盘缓冲区*/
            scanf("%d", &i);
            ListDel_L(head, i);
            ListPint_L(head);
            break;
        }
    } while ((cmd != 'q') && (cmd != 'Q'));
}

 


 

 

 VC++6.0就很好,空工程,C++源文件

posted on 2020-09-23 07:13  维特根斯坦  阅读(56)  评论(0)    收藏  举报