线性表——链式存储(动态)

代码如下:

#include <stdio.h>

typedef int DataType;
typedef struct LNode{
    DataType data;
    struct LNode *next;
}LNode,*LinkList;

initList(LinkList L);//init List
LNode *createListToHead();//create list to head
LNode *createListToRear();//create list to rear
LNode *Locate(LinkList L,int index);//locate node by index
int LocateKey(LinkList L,DataType y);//locate node by DataType
void foreachList(LinkList L);//show each every element
void insertRearList(LinkList L,int index,DataType key);//insert new element before the List element by index
void insertHeadList(LinkList L,int index,DataType key);//insert new element after the List element by index
void DelList(LinkList L,int index);//delete element by index

void main(){
    LinkList L;
    int i;
    initList(L);
    L = createListToRear();
    foreachList(L);
}

initList(LinkList L){
    L = (DataType *)malloc(sizeof(LNode));
    L->next =NULL;
}

LNode *createListToHead(){
    LNode *head;//head point
    LNode *s;//new node point
    LNode *p;//function point
    int c;
    head = (LNode*)malloc(sizeof(LNode));
    p=head;
    p->next = NULL;
    printf("Please type until the data less or equal than zero\n");
    scanf("%d",&c);
    while(c>0){
        s = (LNode*)malloc(sizeof(LNode));
        s->data = c;
        s->next = p->next;
        p->next =s;
        scanf("%d",&c);
    }
    return head;
}

LNode *createListToRear(){
    LNode *head;//head point
    LNode *s;//new node point
    LNode *p;//work point
    int c;
    head = (DataType*)malloc(sizeof(LNode));
    p = head;
    printf("Please type until the data less or equal than zero\n");
    scanf("%d",&c);
    while(c>0){
        s = (DataType*)malloc(sizeof(LNode));
        s->data = c;
        s->next = NULL;
        p->next = s;
        p = s;
        scanf("%d",&c);
    }
    return head;
}

LNode *Locate(LinkList L,int index){
    int count = 0;//set 0 because the fist node is empty
    LNode *p = L;
    if(index<=0){
        return NULL;
    }
    while(p->next!=NULL){
        if(count<index){
            count++;
            p=p->next;
        }
        else{
            break;
        }
    }
    if(index==count){
        return p;
    }
    return NULL;
}

int LocateKey(LinkList L,DataType y){
    LNode *head = L;
    int count = 0;
    while(head->next!=NULL){
        if(head->data==y){
            return count;
        }
        head=head->next;
        count++;
    }
    return -1;
}

void foreachList(LinkList L){
    LNode *p = L->next;
    while(p!=NULL){
        printf("%d ",p->data);
        p = p->next;
    }
}

void insertRearList(LinkList L,int index,DataType key){
    LNode *head = L;
    LNode *s;
    int count=0;
    if(index<=0){
        return NULL;
    }
    while(head->next!=NULL&&count<index){
        if(count < index){
            head=head->next;
            count++;
        }
        else{
            break;
        }
    }
    if(count == index){
        s = (LNode*)malloc(sizeof(LNode));
        s->data = key;
        s->next = head->next;
        head->next = s;
    }
    else{
        return NULL;
    }
}

void insertHeadList(LinkList L,int index,DataType key){
    LNode *head = L;
    LNode *s;
    int count=0;
    index = index-1;
    if(index<=0){
        return NULL;
    }
    while(head->next!=NULL&&count<index){
        if(count < index){
            head=head->next;
            count++;
        }
        else{
            break;
        }
    }
    if(count == index){
        s = (LNode*)malloc(sizeof(LNode));
        s->data = key;
        s->next = head->next;
        head->next = s;
    }
    else{
        return NULL;
    }
}

void DelList(LinkList L,int index){
    LNode *head = L;
    LNode *p;
    int count =1;
    if(index < 1){
        return NULL;
    }
    while(L->next != NULL && count<index){
        count++;
        head=head->next;
    }
    if(count != index){
        return NULL;
    }
    else{
        p=head->next;
        head->next=p->next;
        free(p);
    }
}

猜你喜欢

转载自blog.csdn.net/dumiaoxin/article/details/80877128