#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node_t;
void insert_list(node_t *ph,int x,int pos)
{
int i;
node_t *ptmp = ph,*pnew=NULL;
for(i=0;i<pos-1;i++)
{
ptmp = ptmp->next;
}
pnew = (node_t*)malloc(sizeof(node_t));
if(NULL==pnew)
{
puts("malloc error");
exit(-1);
}
pnew->data = x;
pnew->next = NULL;
pnew->next = ptmp->next;
ptmp->next = pnew;
}
node_t *creat_empty_list()
{
node_t *q=(node_t*)malloc(sizeof(node_t));
if(NULL==q)
{
puts("malloc error");
exit(-1);
}
q->next = NULL;
return q;
}
void print_list(node_t *ph)
{
while(ph->next!=NULL)
{
ph = ph->next;
printf("%d ",ph->data);
}
}
int main()
{
node_t *ph=NULL;
ph = creat_empty_list();
insert_list(ph,100,1);
insert_list(ph,10,1);
insert_list(ph,60,1);
insert_list(ph,88,2);
print_list(ph);
return 0;
}
void delete_list(node_t *ph,int pos)
{
int i;
node_t *ptmp=ph,*pdel=NULL;
for(i=0;i<pos-1;i++)
{
ptmp = ptmp->next;
}
pdel = ptmp->next;
ptmp->next = pdel->next;
pdel->next = NULL;
if(pdel!=NULL)
{
free(pdel);
pdel = NULL;
}
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node_t;
int length_list(node_t *ph)
{
int count=0;
while(ph->next!=NULL)
{
ph = ph->next;
count++;
}
return count;
}
void insert_list(node_t *ph,int x,int pos)
{
if(pos<1||pos>length_list(ph)+1)
{
puts("insert error");
return;
}
int i;
node_t *ptmp = ph,*pnew=NULL;
for(i=0;i<pos-1;i++)
{
ptmp = ptmp->next;
}
pnew = (node_t*)malloc(sizeof(node_t));
if(NULL==pnew)
{
puts("malloc error");
exit(-1);
}
pnew->data = x;
pnew->next = NULL;
pnew->next = ptmp->next;
ptmp->next = pnew;
}
node_t *creat_empty_list()
{
node_t *q=(node_t*)malloc(sizeof(node_t));
if(NULL==q)
{
puts("malloc error");
exit(-1);
}
q->next = NULL;
return q;
}
void print_list(node_t *ph)
{
while(ph->next!=NULL)
{
ph = ph->next;
printf("%d ",ph->data);
}
putchar('\n');
}
void delete_list(node_t *ph,int pos)
{
int i;
if(pos<1||pos>length_list(ph))
{
puts("del error");
return;
}
node_t *ptmp=ph,*pdel=NULL;
for(i=0;i<pos-1;i++)
{
ptmp = ptmp->next;
}
pdel = ptmp->next;
ptmp->next = pdel->next;
pdel->next = NULL;
if(pdel!=NULL)
{
free(pdel);
pdel = NULL;
}
}
void search_list(node_t *ph,int data)
{
while(ph->next!=NULL)
{
ph = ph->next;
if(ph->data==data)
{
puts("has found");
return;
}
}
puts("not found!");
}
int main()
{
node_t *ph=NULL;
ph = creat_empty_list();
insert_list(ph,100,1);
insert_list(ph,10,1);
insert_list(ph,60,1);
insert_list(ph,88,2);
print_list(ph);
delete_list(ph,3);
print_list(ph);
search_list(ph,88);
return 0;
}