#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
int data;
struct node *next;
}LNode,*Linklist;
bool initlist(Linklist *l)
{
(*l) = (LNode*)malloc(sizeof(LNode));
if((*l)==NULL)
return false;
(*l)->next=NULL;
return true;
}
bool empty(Linklist l)
{
if(l->next==NULL)
return true;
else
return false;
}
bool listinsert(Linklist l, int i, int e)
{
if(i<1)
return false;
LNode *p;
int j=0;
p = l;
while (p != NULL && j < i - 1) {
p=p->next;
j++;
}
if(p==NULL)
return false;
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data=e;
s->next = p->next;
p->next = s;
return true;
}
bool insertnextnode(LNode* p, int e)
{
if(p==NULL)
return false;
LNode *s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
return false;
s->data=e;
s->next =p->next;
p->next = s;
return true;
}
bool insertpriornode(LNode* p, int e)
{
if(p==NULL)
return false;
LNode *s = (LNode*)malloc(sizeof(LNode));
if(s==NULL)
return false;
s->next=p->next;
p->next =s;
s->data=p->data;
p->data=e;
return true;
}
bool listdelete(Linklist l, int i, int* e)
{
if(i<1)
return false;
LNode*p;
int j=0;
p=l;
while (p != NULL && j < i - 1) {
p=p->next;
j++;
}
if(p==NULL)
return false;
if(p->next==NULL)
return false;
LNode* q =p->next;
*e = q->data;
p->next = q->next;
free(q);
return true;
}
bool deletenode(LNode* p)
{
if(p==NULL)
return false;
if(p->next==NULL)
return false;
LNode *q= p->next;
p->data=q->data;
p->next=q->next;
free(q);
return true;
}
LNode* getelem(Linklist l, int i)
{
if(i<0)
return NULL;
LNode *p;
int j=0;
p=l;
while (p != NULL && j < i) {
p=p->next;
j++;
}
return p;
}
LNode* locateelem(Linklist l, int e)
{
LNode *p = l->next;
while (p != NULL && p->data != e) {
p=p->next;
}
return p;
}
int length(Linklist l)
{
int len =0;
LNode *p = l;
while (p->next != NULL) {
p=p->next;
len++;
}
return len;
}
Linklist creat_withhead_by_tailinsert(Linklist* l)
{
int x;
scanf("%d",&x);
(*l)=(LNode*)malloc(sizeof(LNode));
LNode *s,*r =(*l);
while (x != 9999) {
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return (*l);
}
Linklist creat_withhead_by_headinsert(Linklist* l)
{
int x;
scanf("%d",&x);
(*l)=(LNode*)malloc(sizeof(LNode));
(*l)->next=NULL;
LNode *s;
while (x != 9999) {
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=(*l)->next;
(*l)->next=s;
scanf("%d",&x);
}
return (*l);
}
bool print(Linklist l)
{
LNode *p;
p=l;
if(p->next==NULL)
return false;
while (p->next != NULL) {
printf("%d\n",p->next->data);
p=p->next;
}
return true;
}
int main()
{
Linklist list=NULL;
creat_withhead_by_tailinsert(&list);
printf("%d##\n",length(list));
print(list);
listinsert(list,1,99);
print(list);
printf("%d##\n",length(list));
return 0;
}