Chinaunix首页 | 论坛 | 博客
  • 博客访问: 11591
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-18 19:37
文章分类
文章存档

2016年(2)

我的朋友
最近访客

分类: C/C++

2016-12-21 14:07:16

#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) |
0

上一篇:关于c语言栈操作的小结

下一篇:没有了

给主人留下些什么吧!~~