#include<malloc.h>
#define error NULL
typedef int ElementType;
typedef struct LNode list;
struct LNode{
ElementType data;
ElementType last;
list *next;
};
list *Create();
int IntergetSearch(list *h,ElementType X);
list *PointSearch(list *h,ElementType X);
bool Delete(list *h,list *q);
bool insert(list *h,list *q,ElementType X);
int main()
{
// list h = (list)malloc(sizeof(struct LNode));
list *h = Create();
int NUM;
/* printf("please input the number you want to find: ");
scanf("%d",&NUM);
ElementType Position;
if(IntergetSearch(h,NUM)!=-1)
{
Position = IntergetSearch(h,NUM);
printf("the position is %d",Position);
}
else
{
printf("not find!");
}
*/
printf("please input the position you want to fine ");
scanf("%d",&NUM);
list *q1 =PointSearch(h,NUM);
while(q1)
{
printf("(%d):%d",q1->last+1,q1->data);
q1= q1->next;
}
putchar('\n');
list *t =PointSearch(h,NUM);
if(t==h)
{
h = h->next;
list *p1 = h;
while(p1)
{
printf("(%d):%d",p1->last+1,p1->data);
p1 = p1->next;
}
}
else if(Delete(h,t))
{
printf("succeed!\n");
list *p1 = h;
while(p1)
{
printf("(%d):%d",p1->last+1,p1->data);
p1 = p1->next;
}
}
//list p1 = h->next;
/* list p1 = h;
while(p1)
{
printf("(%d):%d",p1->last+1,p1->data);
p1 = p1->next;
}
*/
putchar('\n');
int N;
scanf("%d",&N);
t =PointSearch(h,N);
if(insert(h,t,60)){
list *p = h;
//list p = h->next;
while(p)
{
printf("(%d):%d",p->last,p->data);
p = p->next;
}
}
return 0;
}
list *Create()
{
int n,i;
list *p,*h,*t;
int num;
h = NULL;
// h = (list)malloc(sizeof(struct LNode));
// h->next = NULL;
printf("please input the n:");
scanf("%d",&n);
if(n<=0)
{
printf("input error");
return error;
}
for(i=0;i<n;i++)
{
p = (list *)malloc(sizeof(struct LNode));
scanf("%d",&num);
p->data = num;
p->last = i;
//if(h->next==NULL)
if(h==NULL)
{
h = p;
// h->next = p;
t = p;
}
else
{
t->next = p;
t = p;
}
}
t->next =NULL;
return h;
}
int IntergetSearch(list *h,ElementType X)
{
// list p = h->next;
list *p = h;
int i =0;
while(p&&p->data!=X)
{
p=p->next;
i++;
}
if(p==NULL)
{
return -1;
}
else
{
return i+1;
}
}
list *PointSearch(list *h,ElementType X)
{
list *p = h;
// list p = h->next;
int i =0;
while(p&&p->data!=X)
{
p = p->next;
}
if(p)
{
return p;
}
else
{
printf("error!");
}
}
bool Delete(list *h,list *q)
{
list *p = h;
// list p = h->next;
int i=0;
for(p;p&&p->next!=q;p=p->next)
{
i++;
}
if(i==0)
{
//第二个节点
p->next = q->next;
free(q);
return true;
}
if(p==NULL||q==NULL)
{
printf("error!\n");
return false;
}
else
{
p->next = q->next;
free(q);
return true;
}
}
bool insert(list *h,list *q,ElementType X)
{
list *tem,*p;
for(p=h;p&&p->next!=q;p=p->next)
{
;
}
if(p==NULL)
{
printf("the position is error!");
return false;
}
else
{
tem = (list *)malloc(sizeof(struct LNode));
tem->data = X;
tem->next = q;
p->next = tem;
return true;
}
}
能帮忙看下为什么如果是删除第一个节点的数据,不在主函数中进行就会无限循环