链表创建插入删除

存储结构

typedef struct Node{
		E element;
		struct Node *next;
}*node;

创建(初始化)

void initList(node nd){
  nd->next=NULL;
}

插入

void insertNode(node head,E e){
	//有这个循环就是尾插,否则就是头插 
	while(head->next!=NULL)
		 	head=head->next;
	// new一个node存我们要插入结点的数据
		
	node nd=(node)malloc(sizeof(struct Node));
	nd->element =e;
	//令我们的nd指向head指向的地方 ,其实就是NULL 
	nd->next=head->next;
	//再把head的指向改为nd 
	head->next=nd;
	}

删除

void delteNode(node n){
//要删除第几个,就让n后面跟几个->next,赋值就多指向一个
	n->next->next=n->next->next->next;
}

删除重复元素


void deleteDup(node nd) {
    if (nd == NULL) return;
    node current = nd;
    while (current != NULL) {
        node runner = current;
        while (runner->next != NULL) {
            if (current->element == runner->next->element) {
                node tmp = runner->next;
                runner->next = runner->next->next;
                free(tmp); 
            } else {
                runner = runner->next;
            }
        }
        current = current->next;
    }
}
posted @ 2023-12-21 18:00  Happy_Eric  阅读(7)  评论(0编辑  收藏  举报