头文件:
#ifndef __LOOP_LIST_H__
#define __LOOP_LIST_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*node_p;
node_p create_loop();
node_p create_node(int data);
int empty_loop(node_p H);
void insert_head(node_p H,int data);
void show_loop(node_p H);
void insert_tail(node_p H,int data);
void delete_head(node_p H);
void delete_tail(node_p H);
void insert_location(node_p H,int data,int location);
void delete_location(node_p H,int location);
node_p delete_H(node_p H);
void show_newloop(node_p HH);
#endif
主函数:
#include "loop.h"
int main(int argc, const char *argv[])
{
node_p H=create_loop();
insert_head(H,1);
insert_head(H,5);
insert_head(H,8);
insert_head(H,4);
insert_head(H,22);
insert_head(H,16);
show_loop(H);
putchar(10);
printf("add 6 at the head\n");
insert_head(H,6);
show_loop(H);
putchar(10);
printf("add 66 at the end\n");
insert_tail(H,66);
show_loop(H);
putchar(10);
printf("delete the head\n");
delete_head(H);
show_loop(H);
putchar(10);
printf("delete the end\n");
delete_tail(H);
show_loop(H);
putchar(10);
printf("add 33 at the 3rd location\n");
insert_location(H,33,3);
show_loop(H);
putchar(10);
printf("delete the 4th location\n");
delete_location(H,4);
show_loop(H);
putchar(10);
printf("delete the H_node\n");
node_p HH=delete_H(H);
show_newloop(HH);
return 0;
}
ub
自定义函数:
#include "loop.h"
node_p create_loop()
{
node_p H=(node_p)malloc(sizeof(node));
if(H==NULL)
{
printf("apply fail\n");
return NULL;
}
H->data=0;
H->next=H;
return H;
}
node_p create_node(int data)
{
node_p new=(node_p)malloc(sizeof(node));
if(new==NULL)
{
printf("apply fail\n");
return NULL;
}
new->data=data;
return new;
}
int empty_loop(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return -1;
}
return H->next==H?1:0;
}
void insert_head(node_p H,int data)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
node_p new=create_node(data);
new->next=H->next;
H->next=new;
H->data++;
}
void show_loop(node_p H)
{
if(H==NULL)
{
printf("apply fail");
return;
}
if(empty_loop(H))
{
printf("loop is empty\n");
return;
}
node_p p=H->next;
printf("H->");
while(p!=H)
{
printf("%d->",p->data);
p=p->next;
}
printf("H");
putchar(10);
}
void insert_tail(node_p H,int data)
{
if(H==NULL)
{
printf("apply fail");
return;
}
node_p new=create_node(data);
node_p p=H->next;
while(p->next!=H)
{
p=p->next;
}
new->next=p->next;
p->next=new;
H->data++;
}
void delete_head(node_p H)
{
if(H==NULL)
{
printf("apply fail");
return;
}
if(empty_loop(H))
{
printf("loop is empty\n");
return;
}
node_p p=H->next;
H->next=p->next;
free(p);
H->data--;
}
void delete_tail(node_p H)
{
if(H==NULL)
{
printf("apply fail");
return;
}
if(empty_loop(H))
{
printf("loop is empty\n");
return;
}
node_p p=H->next;
while(p->next->next!=H)
{
p=p->next;
}
node_p q=p->next;
p->next=q->next;
free(q);
H->data--;
}
void insert_location(node_p H,int data,int location)
{
if(H==NULL)
{
printf("apply fail");
return;
}
if(location<=0||location>H->data+1)
{
printf("the location is wrong\n");
return;
}
node_p new=create_node(data);
node_p p=H;
for(int i=0;i<location-1;i++)
{
p=p->next;
}
new->next=p->next;
p->next=new;
H->data++;
}
void delete_location(node_p H,int location)
{
if(H==NULL)
{
printf("apply fail");
return;
}
if(empty_loop(H))
{
printf("loop is empty\n");
return;
}
if(location<0||location>H->data)
{
printf("location is wrong\n");
return;
}
node_p p=H;
for(int i=0;i<location-1;i++)
{
p=p->next;
}
node_p q=p->next;
p->next=q->next;
free(q);
H->data--;
}
node_p delete_H(node_p H)
{
if(H==NULL)
{
printf("apply fail");
return NULL;
}
node_p p=H->next;
while(p->next!=H)
{
p=p->next;
}
p->next=H->next;
free(H);
return p->next;
}
void show_newloop(node_p HH)
{
if(HH==NULL)
{
printf("ally is wrong\n");
return;
}
node_p p=HH;
while(p->next!=HH)
{
printf("%d->",p->data);
p=p->next;
}
printf("%d",p->data);
putchar(10);
}
实现: