c0703 链表操作

  • 【问题描述】
    输入n(n>1)个整数,每次将输入的整数插入到链表头部。-1表示输入结束。再输入一个整数,在链表中查找该数据并删除对应的节点。要求输出惊醒删除操作后链表中所有节点的值。

  • 【输入形式】
    输入以空格分割的n个整数,以-1结束,在输入一个需要删除的数。

  • 【输出形式】
    从链表第一个元素开始,输出链表中所有节点的值。以空格分割。

  • 【样例输入】
    2 4 6 7 8 4 -1
    2

  • 【样例输出】
    4 8 7 6 4

  • 自做答案1

//2020/4/4
//使用带有头结点的链表
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
	int data;
	Node *next;
}Node;
void insert_head(Node *head,int x)
{
	Node *p,*s;
	p = head;
	s = (Node*)malloc(sizeof(Node));
	s->data = x;
	s->next = p->next;
	p->next = s;
}
void print(Node* head)
{
	Node* p = head->next;
	while(p)
	{
		if(p->next == NULL)
		{
			printf("%d\n",p->data);
		}
		else
			printf("%d ",p->data);
		p = p->next;
	}
}
void del_all(Node* head,int data)
{
	Node *p,*q;
	p = head->next;
	q = head;

	while(p)
	{
		if(p->data == data)
		{
			q->next = p->next;
			free(p);
			p = q->next;
		}
		else
		{
			q = p;
			p = p->next;
		}
	}
}

int main()
{
	Node *head = (Node*)malloc(sizeof(Node));
	head->next = NULL;

	int tmp_data;
	scanf("%d",&tmp_data);
	while(tmp_data != -1)
	{
		insert_head(head,tmp_data);
		scanf("%d",&tmp_data);
	}
	print(head);
	scanf("%d",&tmp_data);
	del_all(head,tmp_data);
	print(head);
	return 0;
}

-自做答案2:

#include<stdio.h>
#include<stdlib.h>
//使用不带有头结点的链表

struct Node
{
	int data;
	Node* next;
};

//在刚开始建立链表的时候与有头结点的写法有区别

Node* insert_head(Node *head,int x)//头指针的指向有可能发生改变,需要使用引用或者返回来修改head
{
	if(head == NULL)
	{
		head = (Node*)malloc(sizeof(Node));
		head->data = x;
		head->next = NULL;
		return head;
	}
	else
	{
		Node* s;
		s = (Node*)malloc(sizeof(Node));
		s->data = x;
		s->next = head;
		head = s;
		return head;
	}
}

Node* del_one(Node *head,int x)
{
	Node* p;
	Node* tmp;
	if(head->data == x)
	{
		tmp = head;
		head = head->next;
		free(tmp);
		return head;
	}
	else
	{
		p = head;
		while(p->next)
		{
			if(p->next->data == x)
			{
				tmp = p->next;
				p->next = p->next->next;
				free(tmp);
				return head;//
			}
			p = p->next;
		}
		return head;
	}
}




void print(Node* head)
{
	Node *p = head;
	while(p)
	{
		if(p->next == NULL)
		{
			printf("%d\n",p->data);
		}
		else
		{
			printf("%d ",p->data);
		}
		p = p->next;
	}
}

int main()
{
	int tmp_data;
	Node* head = NULL;

	scanf("%d",&tmp_data);
	while(tmp_data != -1)
	{	
		head = insert_head(head,tmp_data);
		scanf("%d",&tmp_data);
	}
	print(head);
	scanf("%d",&tmp_data);
	del_one(head,tmp_data);
	print(head);
	return 0;
}
  • 小结:带有头结点和不带有头结点的链表在插入和删除操作的时候有所不同。(不带头结点的head头指针有可能会发生改变,需要单独处理第一个元元素)
  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
链表是一种常见的数据结构,它可以用来实现各种算法和数据结构。C语言中,链表可以通过结构体和指针来实现。下面是链表的基本操作: 1. 定义链表节点结构体 ``` struct ListNode { int val; struct ListNode *next; }; ``` 2. 创建链表节点 ``` struct ListNode* createNode(int val) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; return node; } ``` 3. 插入节点到链表头部 ``` void insertAtHead(struct ListNode** head, int val) { struct ListNode* node = createNode(val); node->next = *head; *head = node; } ``` 4. 插入节点到链表尾部 ``` void insertAtTail(struct ListNode** head, int val) { struct ListNode* node = createNode(val); if (*head == NULL) { *head = node; return; } struct ListNode* tail = *head; while (tail->next != NULL) { tail = tail->next; } tail->next = node; } ``` 5. 删除节点 ``` void deleteNode(struct ListNode** head, int val) { struct ListNode* cur = *head; struct ListNode* prev = NULL; while (cur != NULL && cur->val != val) { prev = cur; cur = cur->next; } if (cur == NULL) { return; } if (prev == NULL) { *head = cur->next; } else { prev->next = cur->next; } free(cur); } ``` 6. 查找节点 ``` struct ListNode* searchNode(struct ListNode* head, int val) { struct ListNode* cur = head; while (cur != NULL && cur->val != val) { cur = cur->next; } return cur; } ``` 7. 遍历链表 ``` void printList(struct ListNode* head) { struct ListNode* cur = head; while (cur != NULL) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); } ``` 这些是链表的基本操作,可以根据需要进行扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值