您的位置:首页 > 理论基础 > 数据结构算法

数据结构:链表的基本操作

2013-02-23 20:01 471 查看
//抽象数据类型(ADT)之链表结构
#include<stdio.h>
#include<stdlib.h>
#define ELEMENT int
typedef struct node{
ELEMENT value;
struct node *next;
}list;
list* Initial();
void Greate_List(list*,ELEMENT *a,int n);

int IsEmpty(list *);
int IsLast(list *);
list * Find(ELEMENT,list *);
list * FindPrevious(ELEMENT,list *);//找出 元素的前驱
void Delete(ELEMENT,list *);
void DeleteList(list *);
void Print_list(list *);
list* Initial(){
list *head = (list *)malloc(sizeof(list));
head->value=0;
head->next=NULL;
return head;
}
void Greate_List(list*head,ELEMENT *a,int n){
list *p1,*p2;
int i=0;
p1=head;
for(;i < n;i++){
p2=(list *)malloc(sizeof(list));
p2->next=NULL;
p2->value=*(a+i);
p1->next=p2;
p1=p2;

}
}
int IsEmpty(list *head){
if(head->next==NULL){
return 1;
}
return 0;
}
int IsLast(list *p){
if(p->next == NULL){
return 1;
}
return 0;

}

list *Find(ELEMENT e,list *head){
list *p=head;
p=p->next;
while(p->value!=e && p!=NULL){
p=p->next;
}
return p;
}
list *FindPrevious(ELEMENT e, list *head){
list *p=head;
while(p->next!=NULL && p->next->value != e){
p=p->next;
}

return p;

}
void Delete(ELEMENT e,list *head){
list *p1;
list *p=FindPrevious(e,head);
if(!IsLast(p)){
p1=p->next;
p->next=p1->next;
free(p1);
}

}
void DeleteList(list *head){
list *p=head->next;
list *p1;
while(p!=NULL){
p1=p->next;
free(p);
p=p1;
}
head->next=NULL;

}
void Print_list(list *head){
list *p=head;
if(IsEmpty(head)){
printf("%s","这个是空表");
}
while(p->next!=NULL){
p=p->next;
printf(" :%d",p->value);
}
}
void main(){
ELEMENT a[]={1,2,3,4,5,6,7,8,9,10};
list *head=Initial();
Greate_List(head,a,10);
Print_list(head);

Delete(5,head);
Print_list(head);

DeleteList(head);
Print_list(head);
}

2.双向链表

应用:we imagine that N people have decided to commit mass suicide by arranging themseleves in a circle and killing the Mth person around the circle,closing ranks as each person drops out of the circle. print the killing order.

//-----------------------------------------------------------------------

//双向链表 传递的参数一般不要选head 要不双向就失去了意义了,应该是要从哪里开始

//数数值  就传哪里

//--------------------------------------------------------------------------

#define N 150
#define M 6
typedef struct node{
int number;
struct node *next;
}node;
node *Initial_Node(){
node *p1,*p2,*head;
int i;

head = (node *) malloc (sizeof(node));
p1=head;
head->number = 0;

for(i=0;i < N;i++){
p2 =(node *) malloc (sizeof(node));
p2->number=i+1;
p1->next=p2;
p1=p2;
}
p2->next=head->next;
return head;
}
void delete_node(node *p_previous,node *current){

if(p_previous->next == current->next)
{
p_previous->next = NULL;
return;
}

p_previous->next = current->next;
free(current);
}
void search_node(node *p){

node *p_next = NULL;
node *p_previous = NULL;
int i;
if(p == NULL)
{
return;
}
for(i=1;i<M;i++)
{
p_previous = p;
p=p->next;
}
printf("%d ",p->number);

delete_node(p_previous,p);

search_node(p_previous->next);
}

void main(){
node *head = Initial_Node();
search_node(head->next);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表