顺序表的
#include <malloc.h>
#include <stdio.h>
#define List_Max_Size 100
#define Add_Size 10
struct Human_Data {
char Name [20];
int Age;
};//定义顺序表数据结构体
typedef Human_Data Thread_Save_Struct;
struct Thread_1 {
int Length;
int List_Size;
Thread_Save_Struct *Point;
};//定义顺序表指针结构体
typedef Thread_1 Thread;
int Create_List( Thread &List )
{
List.Point = (Thread_Save_Struct * )malloc ( sizeof(Thread_Save_Struct) * List_Max_Size);
if ( !List.Point ) return (0);
List.List_Size = List_Max_Size;
List.Length = 0;
return (1);
}//建立一个顺序表
int main()
{
int Create_List( Thread &List );
int OnOff = 1;
int Add = 0;
Thread_Save_Struct *Point_1;
Thread User;
if (Create_List(User)==1)
{
printf("\n","存储空间开辟成功");
for (Point_1 = User.Point;OnOff==1 && User.Length<User.List_Size;Point_1++)
{ printf("当前指针域:%d\n",Point_1);
printf("在此输入名字:");
scanf("%s", Point_1->Name);
printf("在此输入年龄:");
scanf("%d", &(Point_1->Age));
printf("是否继续输入?是(1),否(0):");
scanf("%d",&OnOff);
User.Length++;
}//for
printf("当前指针域:%d\n",Point_1);
for (Point_1 = User.Point;Add != User.Length ;Point_1++)
{
printf("当前指针域%d\n",Point_1);
printf("姓名:%s\n",Point_1->Name);
printf("年龄:%d",Point_1->Age);
scanf("%d", &OnOff);
Add++;
}//for
}//if
else printf("%s\n","存储空间开辟失败");
return 0;}//main
开辟了段空间,然后在里面存数,然后打印
双向链式表的
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define List_Max_Size 100
#define Add_Size 10
#define Null 0
struct Member_Data {
char Name [20];
int Age,Length;
Member_Data *Next;
Member_Data *Pre;
};//定义链式表数据结构体
typedef Member_Data Chain_Save_Struct;
typedef Member_Data Head_Chain;
int Create_Chain( Chain_Save_Struct &List )
{
List.Length = 0;
List.Next = Null;
List.Pre = Null;
return (1);
}//建立一个链式表
int Add_Chain ( Head_Chain &List,Chain_Save_Struct **Position)
{
Chain_Save_Struct *Position_1,*PreP;
if (List.Next==NULL)
{
List.Next = (Chain_Save_Struct * )malloc ( sizeof(Chain_Save_Struct));
if (!List.Next) return(0);
List.Length=1;
(List.Next -> Next) = Null;
(List.Next ->Pre) = &List;
*Position = List.Next;
printf("List.Next=%d\n",List.Next);
return(1);
}
else
{
Position_1 = *Position;
for (*Position = (List.Next);
*Position !=NULL; *Position=(*Position)->Next)PreP = *Position;
PreP->Next = (Chain_Save_Struct * )malloc ( sizeof(Chain_Save_Struct));
if (!List.Next) return(0);
List.Length++;
(PreP->Next->Next) = Null;
(PreP->Next->Pre)= PreP;
*Position = Position_1;
return(1);
}
}//为链表添加一个元素;
int Del_Chain (Chain_Save_Struct &List,Chain_Save_Struct **Position)
{ int a,b,c;
Chain_Save_Struct *PreP,*NPosition;
PreP = Null;
if (List.Next==Null||(*Position)->Pre==Null)
{
return 0;
}//if 链表为空表时
for (NPosition=&List;
NPosition != *Position; NPosition = NPosition->Next)PreP = NPosition;//找到当前删除节点的前驱
NPosition = (*Position)->Next;//当前节点的后继
free(*Position);
--List.Length;
//分为当前节点无后继,当前节点有后继两种情况
if (NPosition == Null)
{ *Position =PreP;
printf("无后继");
scanf("%d",&a);
(*Position)->Next = Null ;
return 1;// 当前节点无后继节点
}
else if (NPosition != Null)
{ printf("有后继");
scanf("%d",&a);
*Position = PreP;
PreP->Next = NPosition;
NPosition->Pre = PreP;
return 1;
}//当前节点有后继节点
}
int Chain_Next (Chain_Save_Struct **Position)
{ if (*Position==Null)return 0;
if ((*Position)->Next == Null) return 0;
*Position=((*Position)->Next);
return 1;
}//下移一个位置
int Chain_Pre (Chain_Save_Struct **Position)
{
if (*Position==Null)return 0;
if ((*Position)->Pre == Null) return 0 ;
*Position=((*Position)->Pre);
return 1;
}//上移一个位置
int main()
{
int Create_Chain( Chain_Save_Struct &List );
int Add_Chain ( Chain_Save_Struct &List,Chain_Save_Struct *Position);
int Del_Chain (Chain_Save_Struct &List,Chain_Save_Struct *Position);
int Chain_Next (Chain_Save_Struct *Position) ;
int Chain_Pre (Chain_Save_Struct *Position);
int OnOff = 1;
int Long = 0,Now =0,First=1;
Chain_Save_Struct *Point_1,*Point_2;
Chain_Save_Struct User,User_1;
char Comm[20];
if (Create_Chain(User)==1)
{
printf("\n","存储空间开辟成功");
Point_1 = User.Next;
while (OnOff==1)
{
printf(">:");
scanf("%s", Comm);
if (strcmp("add",strlwr(Comm))== 0)Add_Chain(User,&Point_1);
if (strcmp("del",strlwr(Comm))== 0) if (Del_Chain(User,&Point_1)==0)printf("表已空或当前节点为头节点\n");
if (strcmp("next",strlwr(Comm))== 0)if (Chain_Next(&Point_1)==0)printf ("已到表尾\n");
if (strcmp("back",strlwr(Comm))== 0) if (Chain_Pre(&Point_1)==0)printf ("已到表头\n");
if (strcmp("exit",strlwr(Comm))==0) OnOff =0;
for (Point_2 = User.Next;Point_2!=Null;Point_2 = Point_2->Next)
{
Long++;
if (Point_2==Point_1) Now = Long;
}//for
printf("表长=%d\n",Long);
printf("当前位置=%d\n",Now);
printf("@");
while(Long>=First)
{
printf("#");
First++;
}//while
printf(">\n");
First = 1;
while(Now>=(First))
{
printf(" ");
First++;
}//while
printf("|\n");
printf("Point_1=%d\n",Point_1);
if (Point_1!=Null)printf("Point_1->Pre=%d\n",Point_1->Pre);
if (Point_1!=Null)printf("Point_1->Next=%d\n",Point_1->Next);
printf("当前位置%d\n",Point_1);
printf("User.Length=%d\n",User.Length);
Long=0,Now=0,First=1;
}//while
}//if
else printf("%s\n","存储空间开辟失败");
return 0;
}//main
就在里面做了4个函数,运行后输入add可以添加,del可以删除,next指针下移,back,上移
顺序栈的
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define List_Max_Size 100
#define Add_Size 10
struct Human_Data {
char Name[20] ;
int Age;
};//定义顺序栈数据结构体
typedef Human_Data Stack_Save_Struct;
struct Stack {
int Length;
int List_Size;
Stack_Save_Struct *Base,*Top;
};//定义顺序栈指针结构体
int Create_Stack( Stack &Stack_List )
{
Stack_List.Base = (Stack_Save_Struct * )malloc ( sizeof(Stack_Save_Struct) * List_Max_Size);
if ( !Stack_List.Base ) return (0);
Stack_List.List_Size = List_Max_Size;
Stack_List.Top = Stack_List.Base;
Stack_List.Length = 0;
return (1);
}//建立一个栈表
int Push_Stack ( Stack &List,Stack_Save_Struct &Data)
{ if (List.Length == List.List_Size)return 0;
*List.Top = Data;
// *Top++ = &Data;
List.Top++;
List.Length++;
return 1;
}//入栈
int Get_Stack ( Stack &List,Stack_Save_Struct &Data)
{ int a;
if ( List.Length == 0)return 0;
Data = *(--List.Top);
List.Length--;
return 1;
}//出栈
int main()
{ char Comm[20];
int a,b,c;
int OnOff = 1;
struct Stack User,User_1;
Stack_Save_Struct Save,Save_1;
int Create_Stack( Stack &Stack_List );
int Push_Stack ( Stack &List,Stack_Save_Struct &Data);
int Get_Stack ( Stack &List,Stack_Save_Struct &Data);
Create_Stack(User);
printf("存储空间开辟成功");
while (OnOff==1)
{ printf(">:");
scanf("%s", Comm);
if (strcmp("push",strlwr(Comm))== 0)
{
printf("在此输入姓名:");
scanf("%s", Save.Name);
printf("在此输入年龄:");
scanf("%d", &Save.Age);
if (Push_Stack(User,Save)== 0)printf("栈满");
}
else if (strcmp("get",strlwr(Comm))== 0)
{
if (Get_Stack(User,Save)== 0)printf("栈空");
else {
printf("%s\n", Save.Name);
printf("%d\n", Save.Age);}
}
}//while
// }//if
return 1 ;
}
运行后输入push和get可以分别入栈和出栈
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define List_Max_Size 4
#define Add_Size 10
struct Human_Data {
char Name[20] ;
int Age;
};//定义顺序栈数据结构体
typedef Human_Data Stack_Save_Struct;
struct Queue {
int Tail_Position,Head_Position;
int Length;
int List_Size;
Stack_Save_Struct *Base;
};//定义顺序栈指针结构体
int Create_Stack( Queue &Queue_List )
{
Queue_List.Base = (Stack_Save_Struct * )malloc ( sizeof(Stack_Save_Struct) * List_Max_Size);
if ( !Queue_List.Base ) return (0);
Queue_List.List_Size = List_Max_Size;
Queue_List.Head_Position=0;
Queue_List.Tail_Position=0;
Queue_List.Length = 0;
return (1);
}//建立一个队列
int In_Stack ( Queue &List,Stack_Save_Struct &Data)
{ //printf(" tail=%d,Head_Position=%d,base=%d\n", List.Base+((List.Tail_Position%List_Max_Size)+1),List.Head_Position,List.Base);
if (List.Base+((List.Tail_Position+1)%List_Max_Size) == (List.Base+(List.Head_Position%List_Max_Size)))return 0;
List.Tail_Position = List.Tail_Position%List_Max_Size;
*(List.Base+List.Tail_Position) = Data;
List.Tail_Position++;
List.Length++;
return 1;
}//入队
int Out_Stack ( Queue &List,Stack_Save_Struct &Data)
{ if ( List.Base+(List.Head_Position%List_Max_Size) == List.Base+(List.Tail_Position%List_Max_Size))return 0;
Data = *(List.Base+(List.Head_Position%List_Max_Size));
List.Head_Position++;
List.Length--;
return 1;
}//出队
int main()
{ char Comm[20];
int a,b,c;
int OnOff = 1;
struct Queue User,User_1;
Stack_Save_Struct Save,Save_1;
int Create_Stack( Queue &Queue_List );
int In_Stack ( Queue &List,Stack_Save_Struct &Data);
int Out_Stack ( Queue &List,Stack_Save_Struct &Data);
if (Create_Stack(User)==1)
{
printf("存储空间开辟成功\n");
while (OnOff==1)
{ printf(">:");
scanf("%s", Comm);
if (strcmp("in",strlwr(Comm))== 0)
{
printf("在此输入姓名:");
scanf("%s", Save.Name);
printf("在此输入年龄:");
scanf("%d", &Save.Age);
if (In_Stack(User,Save)== 0)printf("队列满,数据没有添加\n");
}
else if (strcmp("out",strlwr(Comm))== 0)
{
if (Out_Stack(User,Save)== 0)printf("队列空\n");
else {
printf("%s\n", Save.Name);
printf("%d\n", Save.Age);
}
}
/* printf("Position=%d,",User.Tail_Position);
printf("Base=%d,",User.Base);
printf("Base+P=%d,",User.Tail_Position%List_Max_Size);
printf("NowAdd=%d,",User.Base+(User.Tail_Position%List_Max_Size));
printf("Head_Position==%d,",User.Head_Position);
printf("Size=%d\n",sizeof(Stack_Save_Struct));*/
}//while
}//if
return 1 ;
}
顺序队列的,输入in和out可以入队和出队,