数据结构线性表

来源:互联网 发布:erp系统属于数据库 编辑:程序博客网 时间:2024/06/14 12:20
#include <stdio.h>
#include <stdlib.h>
typedef char datatype;
typedef struct linknode
{
    datatype data;
    struct linknode *next;
}node;
node *head;         /*定义p为结构类型指针变量*/

void creat()    /*建表*/
{
    char x;
    printf("请输入一串字符以#结束\n");
    node  *s,  *r;   /*s, r分别为头尾指针*/
    head = NULL;    /*链表头尾为空*/
    r = NULL;
    int count = 0;   /*count 为链表长*/
    x = getchar();
    while (x != '#')
    {
        s = (node *)malloc (sizeof(node));
        s -> data = x;   /*将x的值插入数据域*/
        if (head == NULL)   //
            head = s;
        else
            r -> next = s;
        r = s;           /*r始终指向链表未结点*/
        count++;
        x = getchar();
    }
    if (r != NULL )
        r -> next = NULL;
    printf("链表长度为%d\n",count-1);
}

int  search( node *l, char x)   
/*l接受以存的链表的头指针,找与数据域相同的值*/
{
    node *p;
    int i = 1;
    if (l == NULL)
    {
        printf("链表下溢\n");
        return 0 ;
    }
    if (l -> next == NULL)
    {
        printf("线表为空\n");
        return 0;
    }
    p = l -> next;
    while (p != NULL && p -> data != x)
    {
        p = p -> next;   /*找x的前结点*/
        i++;
    }
    if (p != NULL)
    {
        printf("\n在第%d位置找到值为%c的值",i, x);
    }
    else
    {
        printf("\n未找到值为%c的结点\n",x);
        return 0;
    }
}

void insertchar(int temp, char x)   /*插入结点函数*/
{
    node *p;
    p = head; // 初始化  因为没初始化 错误找了一个小时
    int i;
    for (i = 0; i < temp-1; i++)
        p = p -> next;      /*找x的前结点*/
        node *s;
        s = (node *)malloc(sizeof(node)*1);
        s -> data = x;  
        s -> next = p -> next;
//        printf("22222\n");   /*用printf来找错误处重点*/
        p -> next = s;    /*插入值*/

}
void dellist(char x)   /*删除函数*/
{
    node *p, *q;
    if (head == NULL)  /* 判断是否溢出 */
    {
        printf("链表下溢\n");
        return ;
    }
    if (head -> next == NULL) /*判断是否为空表*/
    {
        printf("线性表为空\n");
        return ;
    }
    q = head;
    p = head -> next;
    while (p != NULL && p -> data != x)
    {
        q = p;
        p = p -> next;
    }
    if (p != NULL)
    {
        char temp = x;
        q -> next = p -> next;
        free(p);
        printf("%c已经被删除\n", temp);
    }
    else
        printf("未找到\n");
}
void show(node *l)  /*显示函数*/
{
    node *p;
    p = l -> next; /*指向头指针*/

    while (p)
    {
        printf("%c",p -> data);  
        p = p -> next;
    }
    printf("\n");

}
int listlong(node *l)  /*表长函数*/
{
    node *p;
    int n = 0;
    p = l ->next;
    while (p)
    {
        n++;
        p = p -> next;
/*    printf("%c",p -> data);  测表长时可以顺便输出表值*/
    }
    printf("表长为%d\n",n);

}
int main()
{
    int num, i;
    char t, d;
    printf("*************************\n");
    printf("*    1 ----- 建 表      *\n");
    printf("*    2 ----- 插 入      *\n");
    printf("*    3 ----- 删 除      *\n");
    printf("*    4 ----- 显 示      *\n");
    printf("*    5 ----- 查 找      *\n");
    printf("*    6 ----- 求表长     *\n");
    printf("*    0 ----- 返回       *\n");
    printf("*************************\n");
    while (1)
    {
        printf("please one num :");
        scanf("%d",&num);
        switch(num)
        {
        case 1:   /*建表*/
            {

                creat();
                break;
            }
        case 2:  /*插入结点*/
            {

                printf("请输入要插入的位置:");
                  scanf("%d",&i);
                getchar();
                printf("请输入你要插入的值:");
                scanf("%c",&t);
                insertchar(i, t);
                break;
            }

        case 3:  /*删除结点*/
            {
                getchar();
                printf("请输入要删除的结点值:");
                scanf("%c",&d);
                dellist(d);
                break;
            }
        case 4:  /*显示表值*/
            {
                show(head);
                   break;
            }

        case 5: /*查找*/
            {

               getchar();
               printf("请输入你要查找的值:");
               scanf("%c",&t);
               search(head,t);
                break;
            }

        case 6:  /*表长*/
            {
                listlong(head);
               break;
            }
        case 0:  return 0;  /*结束语句*/
        default:printf("you enter num is error !\n");
        }
    }
    return 0;

}
原创粉丝点击