数据结构的一些方法

 
链表:
#include < stdio .h>
#include <stdlib.h>
 
struct chain
{
         int value ;
         struct chain * next ;
};
 
struct chain * create ()
{
         struct chain * head , * tail , * p ;
         int x , i ;
         head = tail = NULL ;
         printf ( " 请输入四个整型数据,然后回车 :/n" );
         for ( i = 0; i < 4; i ++)
         {      
                  scanf ( "%d" ,& x );
                   p = ( struct chain *) malloc ( sizeof ( struct chain ));
                  p -> value = x ;
                   p -> next = NULL ;
                   if ( head == NULL )
                            head = tail = p ;
                   else
                            tail = tail -> next = p ;
         }
         return head ;
}
 
struct chain * inlink ( head , a , b )
struct chain * head ;
int a , b ;
{
         struct chain * p , * q , * s ;
         s = ( struct chain *) malloc ( sizeof ( struct chain ));
         s -> value = b ;
         /* 空表插入 */
         if ( head == NULL )
         {
                   head = s ;
                   s -> next = NULL ;
         }
         /* 插入 s 结点作为新表头 */
         if ( head -> value == a )
         {
                   s -> next = head ;
                   head = s ;
         }
         else
         {
                   p = head ;
                   /* 遍历单链表,寻找数据域值为 a 的结点 */
                   while (( p -> value != a )&&( p -> next != NULL ))
                   {
                            q = p ;
                            p = p -> next ;
                   }
                  if ( p -> value == a )                            // 找到数据域为 a 的结点
                   {
                            q -> next = s ;
                            s -> next = p ;
                   }
                   /* 插入结点 s 作为表尾 */
                   else
                   {
                            p -> next = s ;
                           s -> next = NULL ;
                   }
         }
         return ( head );
}
 
struct chain * dellink ( head , a )
struct chain * head ;
int a ;
{
         struct chain * p ,* q ;
         if ( head == NULL )
                   printf ( " 空链表 /n" );
         else if ( head -> value == a )
                   /* 链表的第一个结点即为 a 结点 */
         {
                   p = head ;
                   head = head -> next ;
         }
         else
         {
                   p = head ;
                   while (( p -> value != a )&&( p -> next != NULL ))
                            /* 在链表中搜索数据为 a 的结点 */
                   {
                            q = p ;
                            p = p -> next ;
                   }
                  if ( p -> value != a )
                            /* 在链表中无数据值为 a 的结点 */
                            printf ( " 没有要删除的数据 %d/n" , a );
                   else
                   {
                            q -> next = p -> next ;
                            free ( p );
                   }
         }
         return ( head );
}
 
void main ()
{
         struct chain * q ,* head ;
         int a , b ;
         q = create ();
         head = q ;
         while ( q )                       // 显示链表
         {
                  printf ( "%d/n" , q -> value );
                   q = q -> next ;
         }
         printf ( " 请输入新插入的表元数据位于那个数据之前: " );
         scanf ( "%d" ,& a );
         printf ( "/n 请输入要插入的表元数据: " );
         scanf ( "%d" ,& b );
        
         q = inlink ( head , a , b );
         head = q ;
         while ( q )                       // 显示链表
         {
                  printf ( "%d/n" , q -> value );
                   q = q -> next ;
         }
        
         printf ( " 请输入要删除表元的数据: " );
         scanf ( "%d" ,& a );
         q = dellink ( head , a );
         while ( q )                       // 显示链表
         {
                  printf ( "%d/n" , q -> value );
                   q = q -> next ;
         }
}
 
队列:

栈:
#include < stdio .h>
#include <stdlib.h>
#define Max 100
 
int * p ;
int * tos ;
int * bos ;
 
/* 添加一个数据放到堆栈对顶端 */
void push ( int i )
{
         if ( p > bos )
         {
                   printf ( " 堆栈以满 /n" );
                   return ;
         }
         * p = i ;
         p ++;
}
 
/* 丛堆栈顶端取出一个数据 */
int pop ( void )
{
         p --;
         if ( p < tos )
         {
                   printf ( " 堆栈下溢 /n" );
                   return 0;
         }
         return * p ;
}
 
void main ( void )
{
         int a , b ;
         char s [80];
         p = ( int *) malloc ( Max * sizeof ( int ));
         if (! p )
         {
                   printf ( " 分配内存失败 " );
                   exit (1);
         }
         tos = p ;
         bos = p + Max -1;
         printf ( " 请输入第一个数据 :/n" );
         scanf ( "%d" ,& a );
         push ( a );
         printf ( " 请输入第二个数据 :/n" );
         scanf ( "%d" ,& b );
         push ( b );
         printf ( " 请输入操作符 :/n" );
         scanf ( "%s" , s );
         switch (* s )
         {
         case '+' :
                   a = pop ();
                   b = pop ();
                   printf ( " 结果是 a+b = %d/n" ,( a + b ));
                  push ( a + b );
                   break ;
         case '-' :
                   a = pop ();
                   b = pop ();
                   printf ( " 结果是 a-b = %d/n" ,( a - b ));
                  push ( a - b );
                   break ;
         case '*' :
                   a = pop ();
                   b = pop ();
                   printf ( " 结果是 a*b = %d/n" ,( a * b ));
                  push ( a * b );
                   break ;        
         case '/' :
                   a = pop ();
                   b = pop ();
                   printf ( " 结果是 a/b = %d/n" ,( a / b ));
                  push ( a / b );
                   break ;
         default :
                   printf ( " 请输入正确操作符 /n" );
         }
}


 
字符串:
#include < stdio .h>
#include <stdlib.h>
#define MAX 100
 
typedef struct node
{
         char Data [80];
         struct node * Next ;
} nodetype ;
 
typedef struct head
{
         int Num ;                      /* 行号 */
         int Len ;                        /* 改行字符的个数 */
         nodetype * Next ;
} headtype ;
 
headtype Head [ MAX ];
 
void Initial ();
int MenuSelect ();
void EnterData ();
void DeleteLine ();
void List ();
void ClearBuffer ();
 
main ()
{
         char choice ;
         Initial ();
         while (1)
         {
                   choice = MenuSelect ();
                   switch ( choice )
                   {
                            case 1: EnterData ();
                                     break ;
                            case 2: DeleteLine ();
                                     break ;
                            case 3: List ();
                                     break ;
                            case 4: exit (0);
                   }
         }
}
 
void ClearBuffer ()
{
         while ( getchar ()!= '/n' );
}
 
void Initial ()
{
         int i ;
         for ( i =0; i < MAX ; i ++)
         {
                  Head [ i ]. Len =0;
         }
}
 
int MenuSelect ()
{
         int i ;
         i =0;
         printf ( " 1. Enter/n" );
         printf ( " 2. Delete/n" );
         printf ( " 3. List/n" );
         printf ( " 4. Exit/n" );
         while ( i <=0|| i >4)
         {
                   printf ( " 请输入菜单选择号 /n" );
                  scanf ( "%d" ,& i );
                  ClearBuffer ();
         }
         return ( i );
}
 
void EnterData ()
{
         nodetype * p ,* find ();
         int i , j , m , LineNumber , k ;
         char StrBuffer [100];
         while (1)
         {
                   printf ( " 输入数据要插入的行号 (0~100):/n" );
                            scanf ( "%d" ,& LineNumber );
                  ClearBuffer ();
                  if ( LineNumber <0|| LineNumber >= MAX )
                            return ;
                   printf ( " 请输入要插入的数据,以 @ 作为结束符号 /n" );
                  i = LineNumber ;
                  Head [ i ]. Num = LineNumber ;
                  Head [ i ]. Next =( nodetype *) malloc ( sizeof ( nodetype ));
                  p = Head [ i ]. Next ;
                   m =1;
                   j =-1;
                  StrBuffer [0]=0;
                   k =0;
                   do
                   {
                            j ++;
                            if (! StrBuffer [ k ])
                            {
                                    
                                     scanf ( "%s" , StrBuffer );
                                     k =0;
                            }
                            if ( j >=80* m )
                            {
                                     m ++;
                                     p -> Next =( nodetype *) malloc ( sizeof ( nodetype ));
                                     p = p -> Next ;
                            }
                            p -> Data [ j %80] = StrBuffer [ k ++];
                  } while ( p -> Data [ j %80]!= '@' );
                  Head [ i ]. Len = j ;
         }
}
 
void DeleteLine ()
{
         nodetype * p ,* q ;
         int i , j , m , LineNumber ;
         while (1)
         {
                   printf ( " 输入要删除的行号 (0~100) /n" );
                  scanf ( "%d" ,& LineNumber );
                  if ( LineNumber <0|| LineNumber >= MAX )
                            return ;
                   i = LineNumber ;
                  p = Head [ i ]. Next ;
                   m =0;
                  if ( Head [ i ]. Len >0)
                   {
                            m =( Head [ i ]. Len -1)/80+1;               /* 查找改行用到几个链表结点 */
                   }
                  for ( j =0; j < m ; j ++)
                   {
                            q = p -> Next ;
                            free ( p );
                            p = q ;
                   }
                  Head [ i ]. Len =0;
                  Head [ i ]. Num =0;
         }
}
 
void List ()
{
         nodetype * p ;
         int i , j , m , n ;
         for ( i =0; i < MAX ; i ++)
         {
                  if ( Head [ i ]. Len >0)
                   {
                            printf ( " %d 行有数据,它们是: /n" , Head [ i ]. Num );
                            n = Head [ i ]. Len ;
                            m =1;
                            p = Head [ i ]. Next ;
                            for ( j =0; j < n ; j ++)
                                     if ( j >=80* m )
                                     {
                                              p = p -> Next ;
                                               m ++;
                                     }
                                     else
                                     printf ( "%c" , p -> Data [ j %80]);
                                     printf ( "/n" );
                   }
         }
         printf ( "/n" );
}


树:
#include < stdio .h>
#include <stdlib.h>
 
struct tree
{
         char info ;
         struct tree * left ;
         struct tree * right ;
};
 
struct tree * root ;                   /* 树的第一个结点 */
struct tree * construct ( struct tree * root , struct tree * r , char info );
void print ( struct tree * r , int l );
 
int main ( void )
{
         char s [80];
         root = NULL ;
         do
         {
                   printf ( " 请输入一个字符 :" );
                  gets ( s );
                   root = construct ( root , root ,* s );
         } while (* s );
         print ( root ,0);
         return 0;
}
 
struct tree * construct (
         struct tree * root ,
         struct tree * r ,
         char info )
{
         if (! r )
         {
                   r = ( struct tree *) malloc ( sizeof ( struct tree ));
                   if (! r )
                   {
                            printf ( " 内存分配失败! " );
                            exit (0);
                   }
                   r -> left = NULL ;
                   r -> right = NULL ;
                   r -> info = info ;
                  if (! root )
                            return r ;
                   if ( info < root -> info )
                            root -> left = r ;
                   else
                            root -> right = r ;
                   return r ;
         }
         if ( info < r -> info )
                  construct ( r , r -> left , info );
         else
                  construct ( r , r -> right , info );
 
         return root ;        
}
 
void print ( struct tree * r , int l )
{
         int i ;
         if (! r )
                   return ;
         print ( r -> left , l +1);
         for ( i = 0; i < l ;++ i )
                   printf ( " " );
         printf ( "%c/n" , r -> info );
         print ( r -> right , l +1);
}
#include < stdio .h>
#define Max 100
 
void SetNull ( front , rear )
int * front , * rear ;
{
         * front = 0;
         * rear = 0;
}
 
int Empty ( front , rear )
int * front , * rear ;
{
         if (* front == * rear )
                  return (1);
         else
                  return (0);
}
 
int EnQueue ( q , x , front , rear )
int q [];
int x ;
int * front ,* rear ;
{
         * rear = (* rear +1) % Max ;
         if (* front == * rear )
 
         {
                   printf ( " 队列发生上溢 /n" );
                  return (-1);
         }
         else
         {
                  q [* rear ] = x ;
                  return (0);
         }
}
 
int DelQueue ( q , y , front , rear )
int q [];
int * y ;
int * front ,* rear ;
{
         * front = (* front +1)% Max ;
         if (* front == * rear )
         {
                   printf ( " 队列发生下溢 /n" );
                  return (-1);
         }
         else
         {
                   * y = q [* front ];
                  return (0);
         }
}
 
void main ()
{
         int q [ Max ];
         int f = 0, r = 0;      /*f r 分别对应队列的头和尾在整个队列存储区域的位置 */
         int i , x , m , n ;
         int a ;
         SetNull (& f ,& r );                     /* 清空队列 */
         printf ( " 要输入队列的字符个数: /n" );
         scanf ( "%d" ,& m );
         printf ( " 输入队列的整型数据: /n" );
         for ( i =0; i < m ; i ++)
         {
                   i = i ;
                  scanf ( "%d" ,& x );
                   a = EnQueue ( q , x ,& f ,& r );
                   if ( a == -1)
                            break ;
         }
         printf ( " 要提出队列的字符个数: " );
         scanf ( "%d" ,& n );
         printf ( " 输出从队列中提取的数据: /n" );
        for ( i = 0; i < n ; i ++)
         {
                  if ( DelQueue ( q ,& x ,& f ,& r ) == -1)
                            break ;
                  printf ( "%d/n" , x );
         }
         if ( Empty (& f ,& r ) == 1)
                   printf ( " 队列为空 " );
         else
                   printf ( " 队列中还有 %d 个数据 " ,( m - n ));
}

你可能感兴趣的:(数据结构)