单链表

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

typedef char DataType;

//单链表
typedef struct node {
	DataType data;
	struct node* next;
}ListNode;
typedef ListNode* LinkList;
//
//typedef struct node {
//	DataType data;
//	struct node* next;
//}*LinkList;
LinkList CreateListF() {
	LinkList head;
	ListNode* p;
	char ch;
	head = NULL;

	ch = getchar();
	while (ch != '\n')
	{
		p = (ListNode*)malloc(sizeof(ListNode));
		p->data = ch;
		p->next = head;
		head = p;
		ch = getchar();
	}
	return head;
}

LinkList CreateListR() {
	LinkList head, rear;
	ListNode* p;
	char ch;
	head = NULL; rear = NULL;
	ch = getchar();
	while (ch != '\n')
	{
		p = (ListNode*)malloc(sizeof(ListNode));
		p->data = ch;
		if (head == NULL)
			head = p;
		else
			rear->next = p;

		rear = p;//新的尾结点
		ch = getchar();
	}
	if (rear != NULL) rear->next = NULL;//指针域置空
	return head;
}
//插入带头结点的单链表尾
LinkList CreateListR1() {
	LinkList head= (ListNode*) malloc(sizeof(ListNode));
	ListNode* p,*r;
	DataType ch;
	r = head;	
	printf("输入abc");
	while ((ch= getchar() )!='\n')
	{
		p = (ListNode*)malloc(sizeof(ListNode));
		p->data = ch;
		r->next = p;
		r= p;//新尾 
	}
	r->next = NULL;//更新最后空
	return head;
} 
//找某位置数据
ListNode* GetNode(LinkList head, int i) {
	ListNode* p; int j;
	p = head->next; j = 1;
	while (p != NULL && j < i)
	{
		p = p->next; ++j;
	}

	if (j == i)
		return p;
	else
	{
		return NULL;
	}
}
//找某值
ListNode* LocateNode(LinkList head, DataType k) {
	ListNode* p;
	p = head->next;
	while (p && p->data != k)
	{
		p = p->next;
	}
	return p;
}

void InsertList(LinkList head, int i, DataType x) {
	ListNode* p, * s;
	int j;
	p = head;//
	j = 0;

	while (p != NULL && j < i - 1)
	{
		p = p->next;
	}
	if (p == NULL) {
		return;
	}
	else
	{
		s = (ListNode*)malloc(sizeof(ListNode));
		s->data = x; s->next = p->next;
		p->next = s;
	}
}
//删除i
DataType DeleteList(LinkList head, int i) {
	ListNode* p, * s;
	DataType x; int j;
	p = head; j = 0;
	while (p != NULL && j < i - 1)
	{
		p = p->next; ++j;
	}
	if (p) {
		s = p->next;
		p->next = s->next;
		x = s->data;
		free(s);
		return x;
	}
	exit(0);
} 

//删除链表 某x
LinkList DeleteX(LinkList head, DataType x) {
	ListNode* p, * q, * s;
	p = head;
	if (!p) {
		printf("空链表");
		exit(0);
	}
	q = p->next;
	while (q != NULL)
	{
		if (q->data == x) {
			s = q;
			q = q->next;
			free(s);
			p->next = q;
		}
		else
		{
			p = q;
			q = q->next;
		}
	}
	return head;
}
//分离奇偶
void split(LinkList a, LinkList b) {
	ListNode* p, * r, * s;
	p = a->next;

	r = a; s = b;
	while (p != NULL)
	{
		r->next = p;
		r = p;

		p = p->next;
		if (p != NULL) {
			s->next = p;
			s = p;
			p = p->next;
		}
	}
	r->next = s->next = NULL;
}
//合并两个
LinkList MergeList(LinkList a, LinkList b) {
	LinkList c;		ListNode* pa, * pb, * pc;
	pa = a->next; pb = b->next;
	c = pc = a;//a的头结点作为c 的头结点

	while (pa != NULL && pb != NULL)
	{
		if (pa->data < pb->data) {
			pc->next = pa; pc = pa;
			pa = pa->next;
		}
		else
		{
			pc->next = pb; pc = pb;
			pb = pb->next;
		}
	}

	pc->next = pa != NULL ? pa : pb;
	free(b);//释放b头结点
	return c;
}

void PrintList(LinkList head,bool h) {
	ListNode* p ;
	if (h) {//包含头
		p = head->next;
	}
	else {
		p = head;
	}
	while (p != NULL)
	{
		printf("%c", p->data);
		p = p->next;
	}
	printf("\n");
}
void PrintList(LinkList head) {
	ListNode* p = head; 
	while (p!=NULL)
	{
		printf("%c", p->data);
		p = p->next;
	}
	printf("\n");
}

int main() {
	LinkList l = CreateListR1();// CreateListF();//创建链表
	PrintList(l,true);
	l=DeleteX(l, 'b');
	PrintList(l,true);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值