[C语言]单链表各种基本操作

#include<stdio.h>
typedef struct student
{
	int data;
	struct student *next;
}node;

//链表建立
node* create()
{
	node *head,*p,*s;
	int x;
	head = (node*)malloc(sizeof(node));
	p = head;
	
	while(1)
	{
		printf("input the data: ");
		scanf("%d",&x);
		if(x!=0)
		{
			s = (node*)malloc(sizeof(node));
			s ->data = x;
			p ->next = s;
			p = s;
		}
		else
			break;
	}
	
	p ->next = NULL;
	head = head ->next;
	return head;
}

//链表打印
void print(node *head)
{
	node *p;
	p = head;
	while(p != NULL)
	{
		printf("  %d",p->data);
		p = p->next;
	}
	printf("\n \n");
}

//链表测长
int getLength(node *head)
{
	node *p;
	int length = 0;
	p = head;
	while(p != NULL)
	{
		length++;
		p = p->next;
	}
	return length;
}

//删除一个结点
node* deleteNode(node *head,int x)
{
	node *p,*s;
	//如果要删除的是头结点	
	if(x == head->data)
	{
		p = head->next;
		free(head);
		return p;
	}
	//如果要删除的是一般节点
	p = head;
	s = p->next;
	while(s!=NULL && s->data != x)
	{
		p = p->next;
		s = p->next;
	}
	if(s != NULL)
	{
		p ->next = s->next;
		free(s);
		return head;
	}
	//找不到要删除数据
	else
	{
		printf(" %d  not found \n " , x);
		return head;
	}
}

//链表插入一个结点,设这个链表递增,插入到它的正确位置
node* insertNode(node *head,int x)
{
	node *p,*s,*temp;
	
	temp = (node*)malloc(sizeof(node));
	temp ->data = x;
	
	//如果比头结点还小,则插入到头结点
	if(x < head->data)
	{
		temp -> next = head;
		head = temp;
		return head;
	}
	
	p = head;
	s = p->next;
	while(s != NULL && x > s->data)
	{
		p = p->next;
		s = p->next;
	}
	//s不为空,则插入的为中间结点
	if(s != NULL)
	{
		p->next = temp;
		temp->next = s;
		return head;
	}
	//s为空,则插入的是尾结点
	else
	{
		p->next = temp;
		temp->next = NULL;
		return head;
	}
}

//链表排序
void sort(node *head)
{
	node *p,*s;
	int temp;
	p = head;
	s = p->next;
	while(p != NULL)
	{
		s = p->next;
		while(s != NULL)
		{
			if(s->data < p->data)
			{
				temp = s->data;
				s->data = p->data;
				p->data = temp;
			}
			s = s->next;
		}
		p = p->next;
	}
}

//链表反转
node* reverse(node *head)
{
	node *p1,*p2,*p3;
	
	p1 = head;
	if(p1 == NULL)
		return p1;
	
	p2 = p1 ->next;
	if(p2 == NULL)
		return p1;
	
	p1 -> next = NULL;
	while(p2)
	{
		p3 = p2 ->next;
		p2 ->next = p1;
		p1 = p2;
		p2 = p3;
	}
	
	head = p1;
	return head;
}



//寻找中间结点
node* findMiddleNode(node *head)
{
	node *slow,*fast;
	slow = fast = head;
	while(fast ->next != NULL && fast ->next ->next != NULL)
	{
		slow = slow ->next;
		fast = fast ->next ->next;
	}
	printf("the middle data is: %d  \n \n" , slow->data);
	return slow;
}



void main()
{
	node *head;
	int length,in,out;
	head = create();
	print(head);
	
	length = getLength(head);
	printf("the length is  %d \n",length);
	
	printf("please enter the data to insert: \n");
	scanf("%d",&in);
	head = insertNode(head,in);
	print(head);
	
	printf("please enter the data to delete: \n");
	scanf("%d",&out);
	head = deleteNode(head,out);
	print(head);
	
	printf("sorted: \n");
	sort(head);
	print(head);
	
	printf("reversed: \n");
	head = reverse(head);
	print(head);
	
	findMiddleNode(head);
	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值