去重
void same_delete(node_p H)
{
if(H==NULL)
{
printf("*H==NULL");
return ;
}
if(empty_link(H))
{
printf("H is empty\n");
return;
}
node_p n = H->next;
node_p m = n->next;
int count1=2,count2;
while(n->next->next!=NULL)
{
count2=count1;
m=n->next;
while(m!=NULL)
{
if(n->data==m->data)
{
node_p k=m->next;
pos_delete(H,count2);
m=k;
}
else
{
count2++;
m=m->next;
}
}
count1++;
n=n->next;
}
}
按位置删除
void pos_delete(node_p H,int pos)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
if(pos<1||pos>H->data)
{
printf("position is wrong\n");
return ;
}
if(empty_link(H))
{
printf("H is empty\n");
return ;
}
node_p m = H;
if(pos == H->data)
{
tail_delete(H);
return ;
}
while(pos>1)
{
m=m->next;
pos--;
}
node_p n = m->next;
m->next = m->next->next;
free(n);
H->data--;
return ;
}
按位置查找值
datatype search_position(node_p H,int pos)
{
if(H==NULL)
{
printf("H==NULL");
return 0;
}
if(empty_link(H))
{
printf("H is empty\n");
return 0;
}
if(pos>H->data||pos<1)
return 0;
node_p n=H;
while(pos>0)
{
pos--;
n=n->next;
}
return n->data;
}
按值查找位置
int search_value(node_p H,datatype key)
{
if(H==NULL)
{
printf("H==NULL");
return 0;
}
if(empty_link(H))
{
printf("H is empty\n");
return 0;
}
int pos=1;
node_p n=H->next;
while(n!=NULL)
{
if(n->data==key)
return pos;
pos++;
n=n->next;
}
return 0;
}
求以下结构体的大小
#pragma pack(2) //指定两字节对齐
typedef struct
{
char x;
struct A
{
short a;
int *b;
char c;
}p;
long b;
}T;
#pragma pack()
结构体大小为22
main.c
#include "linked_list.h"
int main(int argc, const char *argv[])
{
int key,pos,data;
node_p H=create_link_list();
head_insert(H,6);
head_insert(H,2);
head_insert(H,2);
head_insert(H,3);
head_insert(H,2);
head_insert(H,8);
head_insert(H,1);
tail_insert(H,7);
tail_insert(H,8);
tail_insert(H,8);
tail_insert(H,10);
tail_insert(H,8);
tail_insert(H,8);
tail_insert(H,11);
tail_insert(H,8);
show(H);
same_delete(H);
show(H);
node_p n=H->next;
printf("%d\n",n->data);
head_delete(H);
printf("%d\n",n->data);
show(H);
printf("After head_delete tail_delete:\n");
head_delete(H);
tail_delete(H);
show(H);
printf("please input insert data,pos:");
scanf("%d %d",&data,&pos);
pos_insert(H,data,pos);
show(H);
printf("please input delete pos:");
scanf("%d",&pos);
pos_delete(H,pos);
show(H);
printf("please input search_value key:");
scanf("%d",&key);
if(search_value(H,key)==0)
printf("no key%d in linked_list\n",key);
else
printf("key%d position is %d \n",key,search_value(H,key));
printf("please input search_position pos:");
scanf("%d",&pos);
if(search_position(H,pos)==0)
printf("position is wrong\n");
else
printf("position%d value = %d\n",pos,search_position(H,pos));
same_delete(H);
show(H);
all_delete(H);
printf("After all_delete:\n");
show(H);
free_link(&H);
printf("After free_link\n");
show(H);
return 0;
}
linked_list.c
#include "linked_list.h"
node_p create_link_list()
{
node_p H= (node_p)malloc(sizeof(node));
if(H==NULL)
{
printf("Space request failed");
return NULL;
}
H->data =0;
H->next = NULL;
return H;
}
node_p create_node(datatype data)
{
node_p new = (node_p)malloc(sizeof(node));
if(new==NULL)
{
printf("Space request failed");
return NULL;
}
new->data = data;
new->next = NULL;
return new;
}
void head_insert(node_p H,datatype data)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
node_p new = create_node(data);
new->next = H->next;
H->next = new;
H->data++;
return ;
}
void tail_insert(node_p H,datatype data)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
node_p new = create_node(data);
node_p m = H;
while(m->next!=NULL)
m = m->next;
m->next = new;
H->data++;
return ;
}
void head_delete(node_p H)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
if(empty_link(H))
{
printf("H is empty\n");
return ;
}
node_p m = H->next;
H->next=H->next->next;
free(m);
m=NULL;
H->data--;
return ;
}
void tail_delete(node_p H)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
if(empty_link(H))
{
printf("H is empty\n");
return ;
}
node_p m = H;
while(m->next->next!=NULL)
m = m->next;
free(m->next);
m->next = NULL;
H->data--;
return ;
}
void pos_insert(node_p H,datatype data,int pos)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
if(pos<1||pos>H->data)
{
printf("position is wrong\n");
return ;
}
node_p new = create_node(data);
node_p m = H;
while(pos>1)
{
pos--;
m = m->next;
}
new->next = m->next;
m->next = new;
H->data++;
return ;
}
void pos_delete(node_p H,int pos)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
if(pos<1||pos>H->data)
{
printf("position is wrong\n");
return ;
}
if(empty_link(H))
{
printf("H is empty\n");
return ;
}
node_p m = H;
if(pos == H->data)
{
tail_delete(H);
return ;
}
while(pos>1)
{
m=m->next;
pos--;
}
node_p n = m->next;
m->next = m->next->next;
free(n);
H->data--;
return ;
}
void all_delete(node_p H)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
if(empty_link(H))
{
printf("H is empty\n");
return;
}
node_p m = H->next;
node_p n;
while(m!=NULL)
{
n = m;
m=m->next;
free(n);
}
H->data=0;
H->next=NULL;
return ;
}
int search_value(node_p H,datatype key)
{
if(H==NULL)
{
printf("H==NULL");
return 0;
}
if(empty_link(H))
{
printf("H is empty\n");
return 0;
}
int pos=1;
node_p n=H->next;
while(n!=NULL)
{
if(n->data==key)
return pos;
pos++;
n=n->next;
}
return 0;
}
datatype search_position(node_p H,int pos)
{
if(H==NULL)
{
printf("H==NULL");
return 0;
}
if(empty_link(H))
{
printf("H is empty\n");
return 0;
}
if(pos>H->data||pos<1)
return 0;
node_p n=H;
while(pos>0)
{
pos--;
n=n->next;
}
return n->data;
}
int empty_link(node_p H)
{
if(H==NULL)
{
printf("H==NULL");
return -1 ;
}
return H->next==NULL?1:0;
}
void show(node_p H)
{
int len=H->data;
empty_link(H);
node_p n = H;
printf("len = %d :",H->data);
while(n->next!=NULL)
{
n = n->next;
printf("%d",n->data);
if(len>1)
{
printf("->");
len--;
}
}
putchar(10);
}
void free_link(node_p *H)
{
if(H==NULL)
{
printf("H==NULL");
return ;
}
free(*H);
}
void same_delete(node_p H)
{
if(H==NULL)
{
printf("*H==NULL");
return ;
}
if(empty_link(H))
{
printf("H is empty\n");
return;
}
node_p n = H->next;
node_p m = n->next;
int count1=2,count2;
while(n->next->next!=NULL)
{
count2=count1;
m=n->next;
while(m!=NULL)
{
if(n->data==m->data)
{
node_p k=m->next;
pos_delete(H,count2);
m=k;
}
else
{
count2++;
m=m->next;
}
}
count1++;
n=n->next;
}
}
linked_list.h
#ifndef __linked_list_H__
#define __linked_list_H__
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}node,*node_p;
node_p create_link_list();
node_p create_node(int data);
void head_insert(node_p H,datatype data);
void tail_insert(node_p H,datatype data);
void pos_insert(node_p H,datatype data,int pos);
void head_delete(node_p H);
void tail_delete(node_p H);
void pos_delete(node_p H,int pos);
void all_delete(node_p H);
int empty_link(node_p H);
int search_value(node_p H,datatype key);
datatype search_position(node_p H,int pos);
void same_delete(node_p H);
void show(node_p H);
void free_link(node_p *H);
#endif