链表的合并和查找
代码中对两个链表进行了合并操作,并可以根据不同的关键字进行查找。head_all是总表,head_boy和head_girl代表分表,函数Link_link对两个链表进行合并,其基本思想是建立一个head_all头结点,将其插入head_boy的头结点之后,将head_girl与之合并后,删除head_girl和head_boy结点。查找是利用ctrcmp函数进行全关键字查找。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct Student
{
int age;
char name[20];
struct Student *piror;
struct Student *next;
};
typedef struct Student Node;
typedef struct Student * Link;
void Creat_link(Link *head)
{
do
{
*head = (Link)malloc(sizeof(Node));
}while(NULL == *head);
(*head) -> next = (*head);
(*head) -> piror = (*head) ;
}
void Creat_node(Link *new_node , int age,char name0[20])
{
do
{
*new_node = (Link)malloc(sizeof(Node));
}while((*new_node) == NULL);
(*new_node) -> age = age;
strcpy((*new_node) -> name , name0);
}
void Insert_node(Link head ,Link new_node)
{
Link p = head -> next;
head -> next = new_node;
p -> piror = new_node;
new_node -> next = p;
new_node -> piror = head;
}
void display(Link head)
{
Link p = head -> next;
if(head == p)
{
printf("Nobody in list\n");
return;
}
while(head != p)
{
printf("Name is ");
puts(p -> name);
printf("Age is %d \n", p -> age);
p = p -> next;
}
return;
}
void switch0(Link p , Link q)
{
p -> piror -> next = q;
q -> next -> piror = p;
q -> piror = p -> piror;
p -> next = q -> next;
q -> next = p;
p -> piror = q;
return;
}
void Link_link(Link head_all ,Link *head_src0 ,Link *head_src1)
{
Link p = (*head_src0) -> next ;
head_all -> next = p ;
(*head_src0) -> piror -> next = head_all;
head_all -> piror = (*head_src0) -> piror;
p -> piror = head_all;
(*head_src0) -> next = NULL;
(*head_src0) -> piror = NULL;
Link q = (*head_src1) -> next;
head_all -> piror -> next = q;
q -> piror = head_all -> piror;
(*head_src1) -> piror -> next = head_all;
head_all -> piror = (*head_src1) ->piror;
(*head_src1) -> next = NULL;
(*head_src1) -> piror =NULL;
free(*head_src0);
free(*head_src1);
return ;
}
void Find_Name(Link head , char name_putin[20])
{
Link p = head -> next;
int i = 0;
printf("**************\n");
printf("Search Results\n");
while(head != p )
{
if(1 == strcmp(p -> name , name_putin))
{
i = i + 1;
printf("Name is : ");
puts(p -> name);
printf("Age is : %d\n" , p -> age);
}
p = p -> next;
}
if(0 == i)
{
printf("Empty\n");
}
printf("***************\n");
}
void Find_Age(Link head ,int age_putin)
{
Link p = head -> next;
int i = 0;
printf("**************\n");
printf("Search Results\n");
while(head != p)
{
if(age_putin == p -> age)
{
i = i + 1;
printf("Name is : ");
puts(p -> name);
printf("Age is : %d\n" , p -> age);
}
p = p -> next;
}
if(0 == i)
{
printf("Empty\n");
}
printf("***************\n");
}
int main()
{
srand(time(NULL));
Link head_boy = NULL;
Link head_girl = NULL;
Link new_node = NULL;
int i;
char name[20] = "name_defort";
Creat_link(&head_boy);
Creat_link(&head_girl);
for(i = 0 ;i < 5 ;i++)
{
//printf("Plz input a char \n");
//gets(name);
Creat_node(&new_node , 16+(rand()%10) , name);
Insert_node(head_boy ,new_node);
}
printf("boy : \n\n");
display(head_boy);
printf("**************************\n\n");
for(i = 0 ;i < 5 ;i++)
{
//printf("Plz input a char \n");
//gets(name);
Creat_node(&new_node , 15+(rand()%8) , name);
Insert_node(head_girl ,new_node);
}
printf("girl : \n\n");
display(head_girl);
printf("**************************\n\n");
Link head_all = NULL;
printf("Put them together\n");
Creat_link(&head_all);
Link_link(head_all ,&head_boy , &head_girl);
printf("boy : \n");
display(head_all);
printf("************************\n\n");
//switch0(head_all -> next , head_all -> next -> next);
// Order_link(head_all);
// display(head_all);
char name_putin[20];
int age_putin;
printf("Plz input the mane you want to find:\n");
gets(name);
Find_Name(head_all , name_putin);
printf("Plz input the age you want to find:\n");
scanf("%d" , &age_putin);
Find_Age(head_all , age_putin);
return 0;
}
运用链表进行堆栈操作
代码中使用链表进行堆栈操作,链表是单链表,以头结点为底部,每一个节点内包括一个数据区,一个指针区和一个top指示区,当这个值达到设定的Max值时,栈满。当然这个方法的问题在于每一个节点都有这样一个值导致空间的利用率不高,可以使用遍历的办法计算链表长度,对比Max值得到是否满栈。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Max 10
#define Max0 11
struct Seq_stack
{
int top;
int Ele;
struct Seq_stack* next;
};
typedef struct Seq_stack Stack;
typedef struct Seq_stack* Link;
void Creat_stack(Link *head)
{
do
{
(*head) = (Link)malloc(sizeof(Stack));
}while(NULL == (*head));
(*head) -> next = NULL;
}
void Init_stack(Link head)
{
head -> top = -1;
}
void Creat_node(Link * new_node ,int Ele_num ,int Top_num)
{
do
{
*new_node = (Link)malloc(sizeof(Stack));
}while(NULL == *new_node);
(*new_node) -> Ele = Ele_num;
(*new_node) -> top = Top_num;
}
int is_full(Link head)
{
Link p = NULL;
p = head->next;
if(p == NULL)
{
return 0;
}
while(NULL != p->next)
{
p = p->next;
}
if(p->top == Max)
{
return 1;
}
return 0;
}
int is_empty(Link head)
{
if(NULL == head -> next)
{
return 1;
}
return 0;
}
int Push_stack(Link head ,Link new_node )
{
Link p = NULL;
if(1 == is_full(head))
{
printf("Stack Full\n");
return 0;
}
p = head;
while(NULL != p -> next)
{
p = p -> next;
}
p -> next = new_node;
new_node -> next = NULL;
return 1;
}
int Pop_stack(Link head , int * RE_Ele)
{
if(1 == is_empty(head))
{
printf("Stack Empty\n");
return 0;
}
Link p = NULL;
p = head -> next;
Link q = NULL;
q = head -> next;
while(NULL != (p -> next) -> next)
{
p = p -> next;
q = q -> next;
}
(*RE_Ele) = p -> Ele;
p -> next = NULL;
q -> next = NULL;
free(p);
return 1;
}
int main()
{
Link new_node = NULL;
Link head = NULL;
srand(time(NULL));
Creat_stack(&head);
Init_stack(head);
int i;
int Ele;
int stat = 0;
int RE_Ele = 0;
for(i = 0 ; i < Max ; i++)
{
Ele = rand()%100;
printf("Rand Ele = %d\n", Ele);
Creat_node(&new_node , Ele, i);
printf("New_node -> Ele = %d\n", (new_node) -> Ele);
printf("New_node -> top = %d\n", (new_node) -> top);
stat = Push_stack(head , new_node);
if(0 == stat)
{
printf("Stack Full _Err1 _Beyond maxmun\n");
goto next0;
}
}
next0:
stat = Pop_stack(head ,&RE_Ele);
if(0 == stat)
{
printf("Stack Empty _Err2 _Top = -1\n");
}
else
{
printf("Pop Number = %d\n",RE_Ele);
}
return 0;
}