C链表的Demo

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


typedef struct list {
	int data;
	struct list* next;
} mylist;

int printflist(mylist* list)
{
	mylist* tmp;
	tmp = list;

	printf("printf-head:     %p\n",tmp);
	printf("printf-listnode: %p\n",list);
	
	while(list)
	{
		printf("the value of list is %d.\n", list->data);
		list = list->next;
	}
}

//头插法反转链表
// http://c.biancheng.net/view/8105.html
mylist* list_reverse(mylist* head)
{
    if(head == NULL || head->next== NULL)
    {
        return head;
    }

    mylist* new_head = NULL;
    mylist* temp = NULL;

    while (head)
    {
        temp = head;

        //将 temp 从 head 中摘除
        head = head->next;
        //将 temp 插入到 new_head 的头部
        temp->next = new_head;

        new_head = temp;
    }
    
    printflist(new_head);
    return new_head;
}


int GetListLenght(mylist* head)
{
	int length = 0;
	mylist* current = head;

	
	if(head == NULL)
		return 0;

	while(current != NULL)
	{
		length++;
		current = current->next;
	}

	return length;
}

int mymalloc()
{
	int i = 5;

	mylist* listnode = (mylist*)malloc(sizeof(mylist));

	mylist* head = listnode;

	
	
 	while(i>0)
 	{
		mylist* tmp = (mylist*)malloc(sizeof(mylist));
		tmp->data = i;
		listnode->next = tmp;
		listnode = listnode->next;
		i--;
	}

	printf("head:     %p\n",head);
	printf("listnode: %p\n",listnode);
	printflist(head->next);
	printf("pritnode: %p\n",head->next);

	list_reverse(head->next);

}

mylist* NthfromtEnd(mylist* head, int k)
{
	mylist* pre = head, *beh = head;

	while(k > 1)
	{
		pre = pre->next;
		k--;
	}

	while(pre->next != NULL)
	{
		pre = pre->next;
		beh = beh->next;
	}

	return beh;
}

int HasCycle(mylist* head)
{
	mylist* pre = head, *beh = head;
	while(pre->next != NULL && beh->next != NULL)
	{
		pre = pre->next->next;
		beh = beh->next;

		if(pre == beh)
			return 1;
	}
		
	return 0;
	
}

mylist* deleteDuplicates(mylist* head)
{
	mylist* node_a = head;
	mylist* node_b = head->next;

	while(node_a != NULL && node_b != NULL)
	{

		if(node_a->data == node_b->data)
		{
			node_a->next = node_b->next;
			node_b = node_b->next;
		} else {
			node_a = node_b;
			node_b = node_b->next;

		}
	}

	return head;
	
}

mylist* middle_node(mylist* head)
{
	mylist* node_a = head;
	mylist* node_b = head;

	while(node_b != NULL && node_b->next != NULL)
	{
		node_a = node_a->next;
		node_b = node_b->next->next;
	}

	return node_a;
}


int main(int argc, char *argv[])
{
	mymalloc();	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C语言链表demo,可以实现链表的创建、插入、删除和遍历操作。 ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct node { int data; // 数据域 struct node* next; // 指针域 } Node; // 创建链表 Node* createList() { Node* head = (Node*)malloc(sizeof(Node)); // 创建头节点 head->next = NULL; return head; } // 在链表尾部插入节点 void insertNode(Node* head, int data) { Node* p = head; // 指向头节点 while (p->next != NULL) { p = p->next; // 找到链表尾部 } Node* newNode = (Node*)malloc(sizeof(Node)); // 创建新节点 newNode->data = data; newNode->next = NULL; p->next = newNode; // 在链表尾部插入新节点 } // 在链表指定位置插入节点 void insertNodeAtPos(Node* head, int data, int pos) { Node* p = head; // 指向头节点 int i = 0; while (p != NULL && i < pos - 1) { p = p->next; // 找到指定位置的前一个节点 i++; } if (p == NULL || i > pos - 1) { printf("Invalid position\n"); // 位置无效 return; } Node* newNode = (Node*)malloc(sizeof(Node)); // 创建新节点 newNode->data = data; newNode->next = p->next; // 在指定位置插入新节点 p->next = newNode; } // 删除链表指定位置的节点 void deleteNodeAtPos(Node* head, int pos) { Node* p = head; // 指向头节点 int i = 0; while (p->next != NULL && i < pos - 1) { p = p->next; // 找到指定位置的前一个节点 i++; } if (p->next == NULL || i > pos - 1) { printf("Invalid position\n"); // 位置无效 return; } Node* q = p->next; // 指向要删除的节点 p->next = q->next; // 删除节点 free(q); } // 遍历链表 void traverseList(Node* head) { Node* p = head->next; // 指向第一个节点 while (p != NULL) { printf("%d ", p->data); // 输出节点的数据 p = p->next; } printf("\n"); } // 主函数 int main() { Node* head = createList(); // 创建链表 insertNode(head, 1); // 在链表尾部插入节点 insertNode(head, 2); insertNode(head, 3); insertNodeAtPos(head, 4, 2); // 在链表指定位置插入节点 deleteNodeAtPos(head, 3); // 删除链表指定位置的节点 traverseList(head); // 遍历链表 return 0; } ``` 运行结果: ``` 1 2 4 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值