#include <stdio.h>
#include <stdlib.h>
/*typedef struct student{
int num;
char name[20];
} ElemType; */
typedef int ElemType;
struct Node{
ElemType data;
struct Node * next;
};
void insert(struct Node *p ,int site,ElemType value);
void dele(struct Node *p,int site);
void output(struct Node *p);
void create(struct Node *p,int t);
int find(struct Node *p,ElemType value);
void update(struct Node *p,int site,ElemType value);
int main()
{
struct Node *head,*q;
int i,t,site,value;
printf("请输入元素的总个数:");
scanf("%d",&t);
head=(struct Node *)malloc(sizeof(struct Node));
printf("请输入%d个元素",t);
create(head,t);
printf("数组中的内容为:") ;
output(head);
printf("请输入你要插入元素的位置和你要插入的数为:");
scanf("%d %d",&site,&value);
insert(head,site,value);
printf("数组中的内容为:") ;
output(head);
printf("请输入你要删除元素的位置为:");
scanf("%d",&site);
dele(head,site);
printf("数组中的内容为:") ;
output(head);
printf("请输入你要更改元素的位置和更改后的数为:");
scanf("%d %d",&site,&value);
update(head,site,value);
printf("数组中的内容为:") ;
output(head);
printf("请输入你要查找的元素为:");
scanf("%d",&value);
if(value!=NULL)
{
printf("你要查找元素的下标为:%d\n",find(head,value));
}
else{
printf("你输入的元素不在查找的范围之内");
}
}
void create(struct Node *p,int t)//创建链表
{
struct Node *n;
int i;
for(i=0;i<t;i++)
{
n=(struct Node *)malloc(sizeof(struct Node));
scanf("%d",&n->data);
n->next=NULL;
p->next=n;
p=n;
}
}
void insert(struct Node *p ,int site,ElemType value)//增
{
int i;
struct Node *n=(struct Node *)malloc(sizeof(struct Node));
n->data=value;
for(i=1;i<site;i++)//将p定位到插入位置site前面的一个结点
{
p=p->next;
}
n->next=p->next;
p->next=n;
}
void dele(struct Node *p,int site)//删
{
int i;
for(i=1;i<site;i++)
{
p=p->next;
}
p->next=p->next->next;
}
void update(struct Node *p,int site,ElemType value)//改
{
int i;
for(i=1;i<=site;i++)
p=p->next;
p->data=value;
}
int find(struct Node *p,ElemType value)//查
{
int k=1;
for(p=p->next;p!=NULL;p=p->next,k++)
{
if(p->data==value)
return k-1;
}
return NULL;
}
void output(struct Node *p)//输出
{
for(p=p->next;p!=NULL;p=p->next)
{
printf("%d ",p->data);
}
printf("\n");
}
输出显示: