**如果想实操把该注释的给注释掉就可以了**
#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;
}
对单链表的操作(c语言)
最新推荐文章于 2023-05-28 02:31:28 发布