算法上机

1.程序:线性表基本操作的实现

这个程序中演示了线性表的创建、插入、删除和查找操作的实现。
程序如下:

#include<stdio.h>
#include <string.h>
#include <malloc.h>
#include <conio.h>
#define MaxLength 10
typedef struct LIST
{
    char name[MaxLength];
    int length;
    struct LIST* next;
}LIST;
LIST a, l, b, c, * p, * head;
void print_list()  // Print all the node of the list. 
{
    do {
        printf("%s\n", p->name);
        p = p->next;
    } while (p != NULL);
}
  insert_element(LIST l, int i, char x)  //将X插入链表,使之成为线性表的第i 个元素
{
    int k;

    if (i<1 || i>l.length + 1)

        return(0);

    if (l.length >= MaxLength)

    {
        printf("The sequenlist is overflow!");

        return(-1);

    }

    for (k = l.length; k >= i - 1; k--)
        l.name[k + 1] = l.name[k];
    l .name[i - 1] = x;
    l.length++;

    return(1);

}//请完成此处代码实现函数操作


 delete_element(LIST l, int i)   //删除顺序表中的第i个元素
{
    int k;
    if (i > l.length || i < 1) {
        printf("The element you want to delete is not exist!");
        return(0);
    }
    for (k = i; k < l.length; k++)
        l.name[k - 1] = l.name[k];
    return(1);
}

LIST* insert_node(LIST* head, char x[MaxLength], int k) //在单链表的第k个结点前插入新结点
{
    LIST* p, * pre, * s;
    int j = 1;
    p = head;
    pre = NULL;
    while ((p != NULL) && (j < k)) /*Find the pre-node of k. */
    {
        pre = p;
        p = p->next;
        j++;
    }
    if (j != k) {
        printf("The number of the node in the linklist is not reached k!");
        return(0);
    }
    s = (LIST*)malloc(sizeof(LIST));
    if (s == NULL)   printf("The memory is not enough!");
    strcmp(s->name, x);
    if (pre == NULL) /* When k=1. */
    {
        s->next = head;
        head = s;
    }
    else {
        s->next = pre->next;
        pre->next = s;
    }
    return(head);
}

delete_node(LIST* head, int i)  //在带头结点的单链表head中删除第i个元素
{
    LIST* p, * q;
    q = NULL;
    p = head;
    while (i-- != 0 && p != NULL) {
        q = p;
        p = p->next;
    }
    if (p == NULL) {
        printf("The node not exist!");
        return(0);
    }
    else {
        if (q == NULL)     head = head->next;
        else q->next = p->next;
        free(p);
    }
}
main()
{
    int i, choice;
    char x[MaxLength], element, listname;
    LIST l;
  
    strcpy(a.name, "Zhou"); a.length = 4;
    strcpy(b.name, "Xing"); b.length = 4;
    strcpy(c.name, "Chi"); c.length = 3;
    head = &a;
    a.next = &b;
    b.next = &c;
    c.next = NULL;
    p = head;
    printf("Welcome to use this programe!\nNow the exist list is:\n");
    print_list();   //输出当前列表
    printf("Please chose the function:\n1.Insert a element in a  equenlist.\n2.Delete a element in a sequenlist.\n3.Insert a node in the linklist.\n4.Delete a node in the link list.\n5.Quit the programe.\n");
    scanf("%d", &choice); //菜单选择
    switch (choice) {
    case 5: {  printf("Thanks to use!Good bye!");    break; }
    case 1: {  printf("Input the list name:");     //在列表中插入元素
        scanf("%c", &listname);
        printf("Input where the element you Insert:");
        scanf("%i", &i);
        printf("Input the element you want to Insert:");
        scanf("%c", &element);
        if (listname == 'a') l = a;
        if (listname == 'b') l = b;
        if (listname == 'c') l = c;
        insert_element(l, i, element);
        print_list();
    }
    case 2: {  printf("Input the list name:"); //删除列表中的元素。
        scanf("%c", &listname);
        printf("Input where the element you Delete:");
        scanf("%i", &i);
        if (listname == 'a') l = a;
        if (listname == 'b') l = b;
        if (listname == 'c') l = c;
        delete_element(l, i);
        print_list();
    }
    case 3: {  printf("Input where the node you want to Insert:"); //在列表中插入结点
        scanf("%d", &i);
        printf("Input the node you want to Insert:");
        scanf("%c", &x);
        insert_node(head, x, i);
        print_list();
    }
    case 4: {  printf("Input where the node you want to Delete:"); //删除列表中的结点
        scanf("%d", &i);
        delete_node(head, i);
        print_list();
    }
    }
}

TAG:none

发表新评论