C语言---链表的基本应用


1.链表的插入//插入一个确定得数
#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node *next;
};
int main(void)
{
	int n,i,j,k;
	scanf("%d",&n);
	struct node *head,*p,*q,*t;
	head=NULL;
	while(n--)
	{
		scanf("%d",&k);
		p=(struct node *)malloc(sizeof(struct node));
		p->data=k;
		p->next=NULL;
		if(head==NULL)
		{
			head=p;
		}
		else
		{
			q->next=p;
		}
		q=p;
	}
	scanf("%d",&k);/////相比于链表的创建只是多了这一部分代码
	p=(struct node *)malloc(sizeof(struct node));
	p->data=k;
	t=head;
	while(t!=NULL)
	{
		if(t->next->data>k)
		{
			p->next=t->next;/////这两行代码顺序顺序相当重要
			t->next=p;//////新结点的下个指向指向大于插入数的地址,t的下个指向被p取代
			break;/////相当重要,不能忘记,没有它无法输出
		}
	}
	t=head;
	while(t!=NULL)
	{
		printf("%d\t",t->data);
		t=t->next;
	}
}
2,在第几个数后插入
 
 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct node
{
	int data;
	struct node *next;
}NODE;
NODE *creat(int n)
{
	int a;
	NODE *head,*p,*q;
	head=NULL;
	while(n--)
	{
		scanf("%d",&a);
		p=(NODE *)malloc(sizeof(NODE));
		p->data=a;
		p->next=NULL;
		if(head==NULL)
		{
			head=p;
		}
		else
		{
			q->next=p;
		}
		q=p;
	}
	return head;
}
NODE *charu(NODE *head,int a)
{
	NODE *p,*q;
	int i,j,b;
	scanf("%d",&b);
	p=(NODE *)malloc(sizeof(NODE));
	p->data=b;
	q=head;//相当重要,保存头结点,
	if(a==1)
	{
		p->next=head;
		return p;
	} 
		for(i=1;i<a-1;i++)
		{
			head=head->next;//head=第a个结点的上一个结点
		}
		p->next=head->next;
		head->next=p;
		return q;
}
int main(void)
{
	int i,a,n;
	scanf("%d",&n);
	NODE *head;
	head=creat(n);
	scanf("%d",&a);
	head=charu(head,a);
	while(head!=NULL)
	{
		printf("%d ",head->data);
		head=head->next;
	}
}


3.链表的删除//删除第i个数
NODE *shanchu(NODE *head,int a)//代入上面的charu函数即可
{
	NODE *p,*q;
	int i;
	p=head;
	q=head;
	if(a==1)
	{
		head=head->next;
		return head;
	} 
		for(i = 1; i < a - 1; i++)
    {
        p = p->next;
    }
    q = p->next;
    p->next = q->next;
    free(q);
    return head;
}

4,链表的排序///冒泡排序//交换值

/*首先将两个结点都设置为空,当内循环走到begin->next=NULL时, 令end=begin,则外循环减少一次,最后一个结点不比较,令begin=head,
 每次都从表头开始比较。*/ 
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}NODE;
NODE *create(int n)
{
	int i,j;
	NODE *head,*p;
	head=(NODE *)malloc(sizeof(NODE));
	head->next=NULL;
	for(i=0;i<n;i++)
	{
		scanf("%d",&j);
		p=(NODE *)malloc(sizeof(NODE));
		p->data=j;
		p->next=head->next;
		head->next=p;
	}
	return head;
}
NODE *paixu(NODE *head)
{
	int x;
	NODE *begin,*end,*p;
	begin=head;end=NULL;
	while(begin!=end)
	{
		while(begin->next!=end)//遍历到end前一个
		{
			if(begin->data>begin->next->data)
			{
				x=begin->data;
				begin->data=begin->next->data;
				begin->next->data=x;
			}
			begin=begin->next;//begin指向下一个结点
		} 
		end=begin;//很重要,已经比好的最后一个结点不比较
		begin=head;外循环仍从头开始遍历
	}
	return begin;
}
int main(void)
{
	int n;
	NODE *head;
	scanf("%d",&n);
	head=create(n);
	head=paixu(head);
	while(head->next!=NULL)//因为是用头插法做的,头结点未赋值用->next
	{
		printf("%d ",head->data);
		head=head->next;
	}
}
 
 
5,链表的添加//不难理解,不解释
#include<stdio.h>
#include<stdlib.h>
int s;
typedef struct node
{
	int data;
	struct node *next;
}NODE;
NODE *create(int n)
{
	int i,j;
	NODE *head,*p;
	head=(NODE *)malloc(sizeof(NODE));
	head->next=NULL;
	for(i=0;i<n;i++)
	{
		scanf("%d",&j);
		p=(NODE *)malloc(sizeof(NODE));
		p->data=j;
		p->next=head->next;
		head->next=p;
	}
	return head;
}
NODE *tianjia(NODE *head)
{
	int i,a;
	scanf("%d",&s);
	scanf("%d",&a);
	NODE *p,*q;
	q=head;
	p=(NODE *)malloc(sizeof(NODE));
	p->data=a;
	if(s==1)
	{
	p->next=head->next;
	return p;	
	}
	else
	{
		for(i=1;i<s;i++)
		{
			head=head->next;
		}
		p->next=head->next;
		head->next=p;
	}
	return q;
}
int main(void)
{
	int n;
	NODE *head;
	scanf("%d",&n);
	head=create(n);
	head=tianjia(head);
	if(s!=1)
	head=head->next;
	while(head!=NULL)
	{
		printf("%d ",head->data);
		head=head->next;
	}
}
链表还有很多东西没有涉及到,后面还得好好学~~~




猜你喜欢

转载自blog.csdn.net/printfxgd/article/details/79873945