#include
typedef int elemtype;
typedef struct node{
elemtype val;
struct node *next;
}link;
typedef link *Link;
Link creatlink(void);
void overlink(Link head);
elemtype isemptylink(Link head);
elemtype linklength(Link head);
elemtype insertnode(Link head,elemtype pos,elemtype data);
elemtype deletenode(Link head,elemtype pos);
void freenode(Link head);
int main(void)
{
Link linkdata;
linkdata = creatlink();
overlink(linkdata);
linklength(linkdata);
insertnode(linkdata,2,10);
deletenode(linkdata,10);
freenode(linkdata);
return 0;
}
void freenode(Link head){
free(head);
}
elemtype isemptylink(Link head)
{
if(head == NULL)
{
printf("link is empty!\n");
return -1;
}else{
return 0;
}
}
Link creatlink(void)
{
elemtype table[4]={1,2,3,4};
Link Lnew = NULL;
Link temp = NULL;
Link head = (Link)malloc(sizeof(Link));
if( head == NULL){
printf("Creat link failt!\n");
return -1;
}
head->val = 0;
head->next = NULL;
temp = head;
int i;
for(i=0;i<4;i++)
{
Lnew = (Link)malloc(sizeof(Link));
if(Lnew == NULL){
printf("Creat nee node failt!\n");
return -1;
}
Lnew->val = table[i];
Lnew->next = NULL;
temp->next = Lnew;
temp = Lnew;
}
return head;
}
void overlink(Link head)
{
isemptylink(head);
Link temp = head->next;
printf("Now begin print link\n");
while(temp != NULL)
{
printf("%d ,",temp->val);
temp = temp->next;
}
printf("\n");
}
#if 1
elemtype linklength(Link head)
{
isemptylink(head);
Link temp = head->next;
int i;
printf("link length is ");
while(temp != NULL)
{
i++;
temp = temp->next;
}
printf("%d",i);
printf("\n");
}
elemtype insertnode(Link head,elemtype pos,elemtype data)
{
isemptylink(head);
Link find = head->next;
Link temp = (Link)malloc(sizeof(Link));
while(find->val != pos)
{
find = find->next;
}
temp->val = data;
temp->next= find->next;
find->next = temp;
overlink(head);
return 0;
}
elemtype deletenode(Link head,elemtype pos){
isemptylink(head);
Link find = head->next;
Link temp;
while(find->val != pos)
{
find = find->next;
}
temp = find;
find = find->next;
temp->next = find->next;
free(find);
overlink(head);
return 0;
}
#endif
阅读(1381) | 评论(0) | 转发(0) |