#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef struct Lnode
{
ElemType data;
struct Lnode *next;
}LNode, *LinkList;
LinkList INIT_link(LinkList H);
int Create_link(LinkList H);
int OUT_link(LinkList H);
int Search_link(LinkList H, int pos);
int Delete_link(LinkList H, int pos, ElemType *e);
int Insert_link(LinkList H, int pos, ElemType e);
int main()
{
LinkList head=NULL;
int pos;
ElemType e;
int n;
head = INIT_link(head);
printf("输入数字,以-1结束:\n");
Create_link(head);
printf("\n输出为:\n");
printf("\n");
OUT_link(head);
printf("\n输入要查找的位置:\n");
scanf("%d", &pos);
printf("这个位置的数是:\n");
Search_link(head, pos);
printf("\n输入要删除的位置:\n");
scanf("%d", &pos);
Delete_link(head, pos, &e);
printf("\n输出为:\n");
OUT_link(head);
return OK;
}
LinkList INIT_link(LinkList H)
{
H = (LNode*)malloc(sizeof(LNode));
if(!H) return NULL;
H->next = NULL;
return H;
}
int Create_link(LinkList H)
{
LinkList p, tail=H;
ElemType m;
int pos;
int i;
while(scanf("%d", &m)&&m!=-1)
{
p = (LNode*)malloc(sizeof(LNode));
if(!p) return ERROR;
p->data = m;
p->next = NULL;
tail->next = p;
tail = p;
}
return OK;
}
int OUT_link(LinkList H)
{
LinkList p=H->next;
if(H==NULL) return ERROR;
while(p)
{
printf("%d ", p->data);
p = p->next;
}
return OK;
}
int Search_link(LinkList H, int pos)
{
LinkList p=H->next;
int i=1;
if(H==NULL||pos<1) return ERROR;
while(p&&i<pos)
{
i++;
p = p->next;
}
if(p==NULL)
return ERROR;
else
printf("%d", p->data);
return OK;
}
int Delete_link(LinkList H, int pos, ElemType *e)
{
LinkList p=H->next,s;
int i=1;
if(H==NULL||pos<1) return ERROR;
while(p&&i<pos-1)
{
i++;
p = p->next;
}
if(p==NULL)
return ERROR;
s = p->next;
p->next = s->next;
*e = s->data;
free(s);
return OK;
}
|