链表基本功能

#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;

typedef struct Node {
    ElemType data;
    struct Node *next;
}Node,*LinkedList;

LinkedList LinkedListInit() {
    Node *L;
    L = (Node *)malloc(sizeof(Node));
    if(L == NULL) {
        printf("申请内存空间失败\n");
    }
    L->next = NULL;
 	return L;
}

LinkedList LinkedListCreatH() { //头插
    Node *L;
    L = (Node *)malloc(sizeof(Node));
    L->next = NULL;

    ElemType x;
    while(scanf("%d",&x) != EOF) {
        Node *p;
        p = (Node *)malloc(sizeof(Node));
        p->data = x;
        p->next = L->next;
        L->next = p;
    }
    return L;
}

LinkedList LinkedListCreatT() {//尾插
    Node *L;
    L = (Node *)malloc(sizeof(Node));
    L->next = NULL;
    Node *r=L;
    ElemType x;
    while(scanf("%d",&x) != EOF) {
        Node *p;
        p = (Node *)malloc(sizeof(Node));
        p->data = x;
        r->next = p;
        r=p;
    }
    r->next=NULL;
    return L;
}

LinkedList LinkedListInsert(LinkedList L,int i,ElemType x) {
    Node *pre;
    pre = L;
    int tempi = 0;
    for (tempi = 1; tempi < i; tempi++) {
    	pre = pre->next;
	}
    Node *p;
    p = (Node *)malloc(sizeof(Node));
    p->data = x;
    p->next = pre->next;
    pre->next = p;

    return L;
}

//LinkedList LinkedListDelete(LinkedList L,ElemType x)
//{
//    Node *p,*pre;
//    p = L->next;
//    while(p->data != x) {
//        pre = p;
//        p = p->next;
//    }
//    pre->next = p->next;
//    free(p);
//    return L;
//}

LinkedList LinkedListDelete(LinkedList L,ElemType x)
{
    Node *p,*pre=L;
    p = L->next;
    while(p->data != x) {
        pre = p;
        p = p->next;
    }
    pre->next = p->next;
    free(p);
    return L;
}

int main() {
    LinkedList list,start;
 	printf("请输入单链表的数据:");
    list = LinkedListCreatT();
    for(start = list->next; start != NULL; start = start->next) {
    	printf("%d ",start->data);
	}
    printf("\n");
    int i;
    ElemType x;
    printf("请输入插入数据的位置:");
    scanf("%d",&i);
    printf("请输入插入数据的值:");
    scanf("%d",&x);
    LinkedListInsert(list,i,x);
    for(start = list->next; start != NULL; start = start->next) {
    	printf("%d ",start->data);
	}
    printf("\n");
    printf("请输入要删除的元素的值:");
    scanf("%d",&x);
    LinkedListDelete(list,x);
    for(start = list->next; start != NULL; start = start->next) {
    	printf("%d ",start->data);
	}
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41333844/article/details/82745797