#include <stdio.h>
#include <malloc.h>
typedef struct Link_node
{
int data;
struct Link_node *next;
}Lnode;
int InitList(Lnode *head)
{
head = (struct Link_node *)malloc(sizeof(struct Link_node));
head->next = NULL;
return 0;
}
int Link_end(Lnode *head)
{
int i,a=0;
Lnode *p = head,*q = head;
if(head = NULL)
{
printf("Not Init Link_list Head!\n");
return -1;
}
printf("Please Input 5 number : ");
for(i=0;i<5;i++)
{
scanf("%d",&a);
p = (struct Link_node *)malloc(sizeof(struct Link_node));
p->data = a;
p->next = NULL;
q->next = p;
q = p;
}
}
int Link_head(Lnode *head)
{
int i,a=0;
Lnode *p=head;
if(head->next = NULL)
{
printf("Not Init Link_list Head!\n");
return -1;
}
printf("Please Input 5 number : ");
for(i=0;i<5;i++)
{
scanf("%d",&a);
p=(struct Link_node *)malloc(sizeof(struct Link_node));
p->next = head->next;
p->data = a;
head->next = p;
}
return 0;
}
int clear_link(Lnode *head)
{
Lnode *p = head,*q = head;
p->next = head->next;
while( p->next != NULL )
{
q = p->next;
p->next = q->next;
free(q);
}
free(p);
return 0;
}
int Display(Lnode *head)
{
Lnode *p = head;
p = p->next;
while(p->next != NULL)
{
printf("%d\t",p->data);
p = p->next;
}
printf("%d\n",p->data);
return 0;
}
int insert(Lnode *head,int n)
{
Lnode *p = head;
Lnode *in = (struct Link_node*)malloc(sizeof(struct Link_node));
in->data = 100;
n--;
while(n)
{
p = p->next;
n--;
}
in->next = p->next;
p->next = in;
return 0;
}
int delete(Lnode *head,int n)
{
Lnode *p = head;
Lnode *q = head;
n--;
while(n)
{
p = p->next;
n--;
}
q = p->next;
p->next = q->next;
free(q);
return 0;
}
int add(Lnode *head)
{
Lnode *p = head;
Lnode *add = (struct Link_node*)malloc(sizeof(struct Link_node));
add->data = 200;
add->next = NULL;
while(p->next != NULL)
{
p = p->next;
}
p->next = add;
return 0;
}
int main()
{
Lnode *head = (struct Link_node *)malloc(sizeof(struct Link_node));
InitList(head);
Link_head(head);
Display(head);
insert(head,1);
Display(head);
delete(head,1);
Display(head);
add(head);
Display(head);
clear_link(head);
return 0;
}
Linux C 单链表实现
最新推荐文章于 2024-03-11 17:09:42 发布