#include <stdio.h>
#include <stdlib.h>
typedef struct singlist
{
int data;
struct singlist *next;
}list,*lplist;
//creat list//
lplist creatlist()
{
lplist head=(lplist)malloc(sizeof(list));
head->next=NULL;
return head;
}
//creat newnode
lplist creatnewnode(int data)
{
lplist newnode=(lplist)malloc(sizeof(list));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
// head insert node
void insert_node_head(lplist list,int data)
{
lplist newnode=creatnewnode(data);
newnode->next=list->next;
list->next=newnode;
}
// print node
void printnode(lplist list)
{
lplist p=list->next;
while(p)
{ printf("%d",p->data);
p=p->next;
printf("\n");
}
}
// (tail)==end insert
void insert_node_end(lplist list,int data)
{
lplist newnode=creatnewnode(data);
lplist p=list;
while(p->next)
{
p=p->next;
}
p->next=newnode;
}
// appoint position delete
void delete_node_position(lplist list,int posdata)
{
lplist q=list->next;
lplist p=list;
if (q==NULL)
{
printf("null_list\n");
return ;
}
else
{
while(q->data !=posdata)
{
p=q;
q=p->next;
if(q==NULL)
{
printf("no find\n");
return ;
}
}
}
p->next=q->next;
}
int main()
{
lplist list=creatlist();
insert_node_head(list,3);
insert_node_head(list,2);
insert_node_head(list,1);
insert_node_head(list,5);
insert_node_end(list,7);
insert_node_end(list,8);
insert_node_end(list,9);
printnode(list);
printf("练习链表\n");
delete_node_position(list,9);
printnode(list);
printf("练习链表\n");
return 0;
}