对单链表的操作(c语言)

**如果想实操把该注释的给注释掉就可以了**
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<windows.h>
typedef struct LNode
{
	int data;
	struct LNode *next;	
}LNode;	
void createlisthead(LNode *&L, int a[], int n)///头插
{
	LNode *s;
	L = (LNode*)malloc(sizeof(LNode));
	L ->next = NULL;
	for(int i=0;i<n;i++)
	{
		s = (LNode*)malloc(sizeof(LNode));
		s ->data = a[i];
		s ->next = L ->next;
		L ->next = s;
	}
}
void creatListlast(LNode *&L,int a[],int n)///尾插
{
	LNode *s;
	L=(LNode*)malloc(sizeof(LNode));
	L->next=NULL;
	LNode *r=L;
	for(int i=0;i<n;i++){
		s=(LNode*)malloc(sizeof(LNode));
		s->data=a[i];
		r->next=s;
		r=r->next;
	}
	r->next=NULL;
 } 
void printfList(LNode *L)//输出链表
 {
 	LNode *temp = L->next;	
	while(temp)
	{
        printf("%d ",temp->data);
		temp = temp->next;
	}
	printf("\n");
 }
LNode *SearchElement(LNode *L,int n)///查找元素
{
	 if(n==0)return L;
	 if(n<1)return NULL;
	 LNode* s;
	 int flog=0;
	 s=L->next;
	 flog++;
	 while(flog!=n){
		 s=s->next;
		 flog++;
	 }
	 return s;
 }
void DeleteLink(LNode *L,int s)///删除一个节点
{
	 LNode *x=L;
	 LNode *q=x;
	 int flog=0;
	 while(flog!=s){
		 q=x;
		 x=x->next;
		 flog++;
	 }
	 q->next=x->next;
 }
int main()
{
 	LNode *L;
 	int n,x;
 	scanf("%d",&n);
 	int a[n];
 	for(int i=0;i<n;++i)
 		scanf("%d",&a[i]);
	creatListhead(L,a,n);
	creatListlast(L,a,n);
	printfList(L);
	x=SearchElement(L,3)->data;
	DeleteLink(L,2);
	printfList(L);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值