废话不多说,直接上代码!
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
int data;
struct LNode*next;
}LNode;
LNode *CL(int len){
LNode*L=(LNode*)malloc(sizeof(LNode));
LNode*temp=L;
temp->next=NULL;
for(int i=1;i<len;i++){
LNode*p=(LNode*)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=temp->next;
temp->next=p;
}
return(LNode*)L;
}
LNode*TTCL(int len){
LNode*L=(LNode*)malloc(sizeof(LNode));
LNode*temp=L;
temp->next=NULL;
for(int i=1;i<=len;i++){
LNode*p=(LNode*)malloc(sizeof(LNode));
scanf("%d",&p->data);
temp->next=p;
temp=p;
}
temp->next=NULL;
return (LNode*)L;
}
int Search(LNode*L,int key){
LNode*temp=L;
int pos=0;
int i=1;
while(temp->next){
temp=temp->next;
if(key==temp->data){
pos=i;
printf("%s%d","The position is \n",i);
return pos;
}
i++;
}
printf("No such data!\n");
return -1;
}
LNode*replace(LNode*L,int pos,int key){
LNode*temp=L;
temp=temp->next;
for(int i=1;i<pos;i++){
temp=temp->next;
}
if(temp!=NULL)
temp->data=key;
return(LNode*)L;
}
LNode*Insert(LNode*L,int pos,int key){
LNode*temp=L;
int i=0;
while(temp!=NULL&&(i<pos-1)){
temp=temp->next;
++i;
}
if((temp==NULL)||(i>pos-1)){
printf("insert fail!\n");
}
LNode*New=(LNode*)malloc(sizeof(LNode));
New->data=key;
New->next=temp->next;
temp->next=New;
return (LNode*)L;
}
LNode*Del(LNode*L,int pos){
LNode*temp=L;
int i=0;
while((i<pos-1)&&temp!=NULL){
temp=temp->next;
++i;
}
if(temp==NULL||i>pos-1){
printf("Wrong with the del!\n");
return (LNode*)L;
}
LNode*del=temp->next;
temp->next=temp->next->next;
free(del);
}
void printlist(LNode*L){
printf("das");
if(L->next==NULL){
printf("empty list!\n");
exit(0);
}
LNode*temp=L;
int count=0;
printf("List:\n");
while(temp->next){
temp=temp->next;
printf("%d\t",temp->data);
count++;
if(count%5==0)
printf("\n");
}
printf("\n");
}
int main(){
int len;
LNode*L;
scanf("%d",len);
printlist(L);
return 0;
}