Big Bang

사이트 주소:http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=1325
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct node
{
    char data[100];
    node *next;
}LinkList;
void InitList(LinkList *&L)
{
    L = (LinkList *)malloc(sizeof(LinkList));
    L->next = NULL;
}
void InsertList(LinkList *&L,char a[],int term)
{
    LinkList *p = L, *r;
    int i = 1;
    while(i <term && p->next != NULL)
    {
        p = p->next;
        i++;
    }
    r = (LinkList *)malloc(sizeof(LinkList));
    strcpy(r->data,a);
    r->next = p->next;
    p->next = r;
}
void DeleteList(LinkList *&L,char a[])
{
    LinkList *p = L;
    while(p->next != NULL)
    {
        if(strcmp(p->next->data,a) == 0)
            break;
        p = p->next;
    }
    p->next = p->next->next;
}
int SearchList(LinkList *L,char a[])
{
    LinkList *p = L->next;
    int i = 0, num = 0;
    while(p != NULL)
    {
        i++;
        if(strcmp(p->data,a) == 0)
        {
            num = i;
            break;
        }
        p = p->next;
    }
    return num;
}
void DispList(LinkList *L)
{
    LinkList *p = L->next;
    while(p->next != NULL)
    {
        printf("%s ",p->data);
        p = p->next;
    }
    printf("%s
",p->data); } int main() { LinkList *L; InitList(L); char a[100]; while(scanf("%s",a) != EOF) { if(!strcmp(a,"insert")) { int term; scanf("%d%s",&term,a); InsertList(L,a,term); } if(!strcmp(a,"show")) { if(L->next != NULL) DispList(L); else printf("
"); } if(!strcmp(a,"delete")) { scanf("%s",a); DeleteList(L,a); } if(!strcmp(a,"search")) { scanf("%s",a); printf("%d
",SearchList(L,a)); } } return 0; }

좋은 웹페이지 즐겨찾기