顺序表
#include <stdio.h>
#include <malloc.h>

#define MaxSize 50

typedef char ElemType;
typedef struct
{
 ElemType elem[MaxSize];
 int length;
}SqlList;


//initial list
void InitList(SqlList* &L)
{
 L = (SqlList*)malloc(sizeof(SqlList));
 L->length = 0;
}

//destroy the list
void DestroyList(SqlList* L)
{
 free (L);
}

//estimate the list if it is empty
int ListEmpty(SqlList* L)
{
 return (L->length == 0);
}

int ListLength(SqlList* L)
{
 return (L->length);
}

void DisplayList( SqlList* L)
{
 int i ;
 if ( ListEmpty(L) == 1)
 {
  return;
 }

 for ( i = 0 ; i < L->length ; i++)
 {
  printf("%c" , L->elem[i]);
 }

 printf("\n");
}

int GetElem( SqlList* L , int i , ElemType& e)
{
 if (i < 1 || i > L->length)
 {
  return 0 ;
 }

 e = L->elem[i - 1];
 return 1;
}

int LocateElem ( SqlList* L , ElemType e)
{
 int i = 0;
 while ( (i < L->length) && (L->elem[i] != e))
 {
  i ++;
 }
 if ( i > L->length)
 {
  return 0;
 }
 else
 {
  return i +1;
 }
}

int ListInsert ( SqlList* &L , int i , ElemType e)
{
 int j;
 if ( i < 1 || i > (L->length + 1) )
 {
  return 0;
 }

 i--;

 for ( j = L->length ; j > i ; j--)
 {
  L->elem[j] = L->elem[j - 1];
 }

 L->elem[i] = e;
 L->length++;
 return 1;
}

int ListDelete(SqlList* &L , int i , ElemType &e)
{
 int j;
 if ( i < 1 || i > L->length)
 {
  return 0;
 }

 i--;
 e = L->elem[i];

 for ( j = i ; j < L->length - 1 ; j ++)
 {
  L->elem[j] = L->elem[j + 1];
 }

 L->length--;

 return 1;
}

int main (void)
{
 SqlList* L;
 ElemType e;
 printf("(1)initial the list\n");
 InitList(L);
 printf("(2)create the list by rail inserting \n");
 ListInsert(L , 1 ,'a');
 ListInsert(L , 2 , 'b');
 ListInsert(L , 3 , 'c');
 ListInsert(L , 4 , 'd');
 ListInsert(L , 5 , 'e');
 printf("(3)output the list\n");
 DisplayList(L);

 printf("(4)the length of the list : %d\n" , ListLength(L));
 printf("(5) the list is %s \n" , ListEmpty(L) ? "null" : "not null");
 GetElem(L , 3 ,e);
 printf("(6)the third element of the list is %c \n" , e);
 printf("(7)the position of the element a is %d \n" , LocateElem(L , 'a'));
 printf("(8)inset f into the list at the fourth place \n");
 ListInsert(L , 4 , 'f');
 printf("(9) output the list:");
 DisplayList(L);

 return 0;

}

链表
#include <stdio.h>
#include <malloc.h>

typedef char ElemType;
typedef struct LNode
{
 ElemType data;
 struct LNode* next;
}LinkList;

void InitList(LinkList* &L)
{
 L = (LinkList*) malloc(sizeof(LinkList));
 L->next = NULL;
}

void DestroyList(LinkList* L)
{
 LinkList* p = L;
 LinkList* q = p->next;
 while ( q != NULL)
 {
  free(p);
  p = q;
  q = q->next;
 }
 free(p);
}

int ListEmpty(LinkList* L)
{
 return (L->next == NULL);
}

int ListLength ( LinkList* L)
{
 LinkList* p = L;
 int i = 0;
 while (p->next != NULL)
 {
  i++;
  p = p->next;
 }

 return i;
}

void DisplayList (LinkList* L)
{
 LinkList* p = L->next;
 while ( p != NULL)
 {
  printf("%c" , p->data);
  p = p->next;
 }

 printf("\n");
}

int GetElem(LinkList* L , int i , ElemType& e)
{
 int j = 0;
 LinkList* p = L;
 while (j < i && p != NULL)
 {
  j ++;
  p = p->next;
 }

 if (p == NULL)
 {
  return 0;
 }
 else
 {
  e = p->data;
  return 1;
 }
}

 

int LocateElem (LinkList* L , ElemType e)
{
 LinkList* p  = L->next;
 int n = 1;
 while ( p != NULL && p->data != e)
 {
  p = p->next;
  n ++;
 }

 if ( p == NULL)
 {
  return 0;
 }
 else
 {
  return n;
 }
}

int ListInsert ( LinkList*L , int i , ElemType e)
{
 int j = 0;
 LinkList* p = L;
 LinkList* s;
 while (j < i - 1 && p != NULL)
 {
  j ++;
  p = p->next;
 }

 if (p==NULL)
 {
  return 0;
 }
 else
 {
  s = (LinkList*)malloc(sizeof(LinkList));
  s->data = e;
  s->next = p->next;
  p->next = s;
  return 1;
 }
}

int ListDelete ( LinkList* &L , int i , ElemType &e)
{
 int j = 0;
 LinkList* p = L;
 LinkList* q;
 while ( j < i && p != NULL)
 {
  j++;
  p = p->next;
 }

 if (p == NULL)
 {
  return 0;
 }
 else
 {
  q = p->next;
  p->next = q->next;
  free(q);
  return 1;
 }
}

int main (void)
{
 LinkList* L;
 ElemType e;
 printf("(1)initial the list\n");
 InitList(L);
 printf("(2)create the list by rail inserting \n");
 ListInsert(L , 1 ,'a');
 ListInsert(L , 2 , 'b');
 ListInsert(L , 3 , 'c');
 ListInsert(L , 4 , 'd');
 ListInsert(L , 5 , 'e');
 printf("(3)output the list\n");
 DisplayList(L);

 printf("(4)the length of the list : %d\n" , ListLength(L));
 printf("(5) the list is %s \n" , ListEmpty(L) ? "null" : "not null");
 GetElem(L , 3 ,e);
 printf("(6)the third element of the list is %c \n" , e);
 printf("(7)the position of the element a is %d \n" , LocateElem(L , 'a'));
 printf("(8)inset f into the list at the fourth place \n");
 ListInsert(L , 4 , 'f');
 printf("(9) output the list:");
 DisplayList(L);

 return 0;

}
双向链表
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct DNode
{
 ElemType data;
 struct DNode* prior;
 struct DNode* next;
}DLinkList;

void InitList ( DLinkList* &L)
{
 L = (DLinkList*)malloc(sizeof(DLinkList));
 L->prior = L->next = NULL;
}

void DetroyList ( DLinkList* &L)
{
 DLinkList* p = L;
 DLinkList* q = p->next;
 while (q != NULL)
 {
  free(p);
  p = q;
  q = p->next;
 }
 free(p);
}

int ListEmpty(DLinkList* L)
{
 return L->next == NULL;
}

int ListLength (DLinkList* L)
{
 DLinkList* p = L;
 int i = 0;
 while (p->next != NULL)
 {
  i ++;
  p = p->next;
 }
 return i;
}

void DisplayList ( DLinkList * L)
{
 DLinkList* p = L->next;
 while ( p != NULL)
 {
  printf("%c" , p->data);
  p = p->next;
 }

 printf("\n");
}

int GetElem (DLinkList* L , int i , ElemType &e)
{
 int j = 0;
 DLinkList* p = L;
 while ( j < i && p != NULL)
 {
  j ++;
  p = p->next;
 }

 if ( p != NULL)
 {
  return 0;
 }
 else
 {
  e = p->data;
  return 1;
 }
}

int LocateElem (DLinkList* L , ElemType e)
{
 int n = 0;
 DLinkList* p = L->next;
 while ( p != NULL && p->data != e)
 {
  n ++;
  p = p->next;
 }
 if (p == NULL)
 {
  return 0;
 }
 else
 {
  return n;
 }
}

int ListInsert (DLinkList* &L , int i , ElemType e)
{
 int j = 0;
 DLinkList* p = L;
 DLinkList* s;
 while (j < i - 1 && p != NULL)
 {
  j ++;
  p = p->next;
 }

 if (p == NULL)
 {
  return 0;
 }
 else
 {
  s =(DLinkList*)malloc(sizeof(DLinkList));
  s->data = e;
  s->next = p->next;
  if ( p->next != NULL)
  {
   p->next->prior = s;
  }

  s->prior = p;
  p->next = s;
  return 1;
 }
}

int ListDelete (DLinkList* &L , int i , ElemType &e)
{
 int j = 0;
 DLinkList* p = L;
 DLinkList* q;
 while ( j < i -1 && p != NULL)
 {
  j ++;
  p = p->next;
 }
 if (p == NULL)
 {
  return 0;
 }
 else
 {
  q = p->next;
  if ( q == NULL)
  {
   return 0;
  }

  p->next = q->next;
  if (p->next != NULL)
  {
   p->next->prior = p;
  }
  free(q);
  return 1;
 }
}

int main (void)
{
 DLinkList* L;
 ElemType e;
 printf("(1)initial the list\n");
 InitList(L);
 printf("(2)create the list by rail inserting \n");
 ListInsert(L , 1 ,'a');
 ListInsert(L , 2 , 'b');
 ListInsert(L , 3 , 'c');
 ListInsert(L , 4 , 'd');
 ListInsert(L , 5 , 'e');
 printf("(3)output the list\n");
 DisplayList(L);

 printf("(4)the length of the list : %d\n" , ListLength(L));
 printf("(5) the list is %s \n" , ListEmpty(L) ? "null" : "not null");
 GetElem(L , 3 ,e);
 printf("(6)the third element of the list is %c \n" , e);
 printf("(7)the position of the element a is %d \n" , LocateElem(L , 'a'));
 printf("(8)inset f into the list at the fourth place \n");
 ListInsert(L , 4 , 'f');
 printf("(9) output the list:");
 DisplayList(L);

 return 0;

}
循环链表
#include <stdio.h>
#include <malloc.h>

typedef char ElemType;
typedef struct LNode
{
 ElemType data;
 struct LNode* next;
}LinkList;

void InitList ( LinkList* &L)
{
 L = (LinkList*)malloc(sizeof(LinkList));
 L->next = L;
}

void DestroyList(LinkList* &L)
{
 LinkList* p = L;
 LinkList* q = p->next;

 while ( q != L)
 {
  free(p);
  p = q;
  q = p->next;
 }

 free(p);
}

int ListEmpty(LinkList* L)
{
 return L->next == L;
}

int ListLength (LinkList* L)
{
 LinkList* p = L;
 int i = 0;
 while (p->next != L)
 {
  i ++;
  p = p->next;
 }

 return i;
}

void DisplayList( LinkList* L)
{
 LinkList* p = L->next;
 while (p != L)
 {
  printf("%c" , p->data);
  p = p->next;
 }

 printf("\n");
}

int GetElem (LinkList* L , int i , ElemType &e)
{
 int j = 0;
 LinkList* p;
 if ( L->next != L)
 {
  if ( i == 1)
  {
   e = L->next->data;
   return 1;
  }
  else
  {
   p = L->next;
   while (j < i - 1 && p != L)
   {
    j ++;
    p = p->next;
   }
   if ( p == L)
   {
    return 0;
   }
   else
   {
    e = p->data;
    return 1;
   }
  }
 }
 else
 {
  return 0;
 }
}

int LocateElem(LinkList* L , ElemType e)
{
 LinkList* p = L->next;
 int n = 1;
 while (p != L && p->data != e)
 {
  p = p->next;
  n++;
 }

 if (p == L)
 {
  return 0;
 }
 else
 {
  return n;
 }
}

int ListInsert ( LinkList* &L , int i , ElemType e)
{
 int j = 0;
 LinkList*p = L;
 LinkList* s;
 if (p->next == L || i == 1)
 {
  s = (LinkList*)malloc(sizeof(LinkList));
  s->data = e;
  s->next = p->next;
  p->next = s;
  return 1;
 }
 else
 {
  p = L->next;
  while (j < i - 2 && p != L)
  {
   j ++;
   p = p->next;
  }

  if ( p == L)
  {
   return 0;
  }
  else
  {
   s = (LinkList*)malloc(sizeof(LinkList));
   s->data = e;
   s->next = p->next;
   p->next = s;
   return 1;
  }
 }
}

int ListDelete (LinkList* &L , int i  , ElemType &e )
{
 int j = 0;
 LinkList* p = L;
 LinkList* q;
 if (p->next != L)
 {
  if ( i == 1)
  {
   q = L->next;
   L->next = q->next;
   free(q);
   return 1;
  }
  else
  {
   p = L->next;
   while ( j < i - 1 && p != L)
   {
    j ++;
    p = p->next;
   }

   if ( i == 1)
   {
    return 0;
   }
   else
   {
    q = p->next;
    p->next = q->next;
    free(q);
    return 1;
   }
  }
 }
 else
 {
  return 0;
 }
}

int main (void)
{
 LinkList* L;
 ElemType e;
 printf("(1)initial the list\n");
 InitList(L);
 printf("(2)create the list by rail inserting \n");
 ListInsert(L , 1 ,'a');
 ListInsert(L , 2 , 'b');
 ListInsert(L , 3 , 'c');
 ListInsert(L , 4 , 'd');
 ListInsert(L , 5 , 'e');
 printf("(3)output the list\n");
 DisplayList(L);

 printf("(4)the length of the list : %d\n" , ListLength(L));
 printf("(5) the list is %s \n" , ListEmpty(L) ? "null" : "not null");
 GetElem(L , 3 ,e);
 printf("(6)the third element of the list is %c \n" , e);
 printf("(7)the position of the element a is %d \n" , LocateElem(L , 'a'));
 printf("(8)inset f into the list at the fourth place \n");
 ListInsert(L , 4 , 'f');
 printf("(9) output the list:");
 DisplayList(L);

 return 0;

}
应用1,集合运算
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct LNode
{
 ElemType data;
 struct LNode* next;
}LinkList;

void DisplayList( LinkList* L)
{
 LinkList* p = L->next;
 while (p != NULL)
 {
  printf("%c" , p->data);
  p = p->next;
 }
 printf("\n");
 
}

void CreateListR (LinkList* &L , ElemType a[] , int n)
{
 LinkList *s , *r ;
 int i;
 L= (LinkList*)malloc(sizeof(LinkList));
 L->next = NULL;
 r = L;
 for ( i = 0 ; i < n ; i ++)
 {
  s = (LinkList*)malloc(sizeof(LinkList));
  s->data = a[i];
  r->next = s;
  r = s;
 }
 r->next = NULL;
}

void Sort(LinkList* &head)
{
 LinkList* p = head->next;
 LinkList *q , *r;
 if (p != NULL)
 {
  r = p->next;
  p->next = NULL;
  p = r;
  while (p != NULL)
  {
   r = p->next;
   q = head;
   while (q->next != NULL && q->next->data < p->data)
   {
    q = q->next;
   }
   p->next = q->next;
   q->next = p;
   p = r;
  }
 }
}

void UnionLinkList(LinkList* ha , LinkList* hb , LinkList* &hc)
{
 LinkList* pa = ha->next;
 LinkList* pb = hb->next;
 LinkList *s , *tc;
 hc = (LinkList*)malloc(sizeof(LinkList));
 tc = hc;
 while (pa != NULL && pb != NULL)
 {
  if (pa->data < pb->data)
  {
   s = (LinkList*)malloc(sizeof(LinkList));
   s->data = pa->data;
   tc->next = s;
   tc = s;
   pa = pa->next;
  }
  else if (pa->data > pb->data)
  {
   s = (LinkList*)malloc(sizeof(LinkList));
   s->data = pb->data;
   tc->next = s;
   tc = s;
   pb = pb->next;
  }
  else
  {
   s = (LinkList*)malloc(sizeof(LinkList));
   s->data = pa->data;
   tc->next = s;
   tc = s;
   pa = pa->next;
   pb = pb->next;
  }
 }

 if ( pb != NULL)
 {
  pa = pb;
 }
 while (pa != NULL)
 {
  s = (LinkList*)malloc(sizeof(LinkList));
  s->data = pa->data;
  tc->next = s;
  tc = s;
  pa = pa->next;
 }

 tc->next = NULL;
}

void InterSect(LinkList* ha , LinkList* hb , LinkList* &hc)
{
 LinkList* pa = ha->next;
 LinkList* pb ;
 LinkList *s , *tc;
 hc = (LinkList*)malloc(sizeof(LinkList));
 tc = hc;
 while (pa != NULL)
 {
  pb = hb->next;
  while (pb != NULL && pb->data < pa->data)
  {
   pb = pb->next;
  }
  if (pb != NULL && pb->data == pa->data)
  {
   s = (LinkList*)malloc(sizeof(LinkList));
   s->data = pa->data;
   tc->next = s;
   tc = s ;
  }
  pa = pa->next;
 }

 tc->next = NULL;
}

void Subs(LinkList* ha , LinkList* hb , LinkList* &hc)
{
 LinkList* pa = ha->next;
 LinkList *pb , *s , *tc;
 hc = (LinkList*)malloc(sizeof(LinkList));
 tc = hc;
 while (pa != NULL)
 {
  pb = hb->next;
  while (pb != NULL && pb->data < pa->data)
  {
   pb = pb->next;
  }
  if (!(pb != NULL && pa->data == pb->data))
  {
   s = (LinkList*)malloc(sizeof(LinkList));
   s->data = pa->data ;
   tc->next = s;
   tc = s;
  }
  pa = pa->next;
 }

 tc->next = NULL;
}


int main (void)
{
 LinkList *ha , *hb , *hc;
 ElemType a[] = {'c' , 'a' , 'e' , 'h'};
 ElemType b[] = {'f' , 'h' ,'b' , 'g' ,'d' , 'a'};
 CreateListR(ha , a , 4);
 CreateListR(hb , b , 6);
 printf("the orign set a  :");
 DisplayList(ha);
 printf("the orign set b :");
 DisplayList(hb);
 Sort(ha);
 Sort(hb);
 UnionLinkList(ha , hb , hc);
 printf("union c :");
 DisplayList(hc);
 InterSect(ha , hb , hc);
 printf("intersect c:");
 DisplayList(hc);
 Subs(ha , hb, hc);
 printf("sub c:");
 DisplayList(hc);

 return 0;
}

应用2,多项式运算
#include <stdio.h>
#include <malloc.h>
#define MAX 20
typedef struct
{
 float coef;
 int exp;
}PolyArray[MAX];
typedef struct pnode
{
 float coef;
 int exp;
 struct pnode* next;
}PolyNode;


void DisplayList (PolyNode* L)
{
 PolyNode* p = L->next;
 while (p != NULL)
 {
  printf("%gX^%d" , p->coef , p->exp);
  p = p->next;
 }
 printf("\n");
}


void CreateListR(pnode* &L , PolyArray a , int n)
{
 PolyNode *s , *r ;
 int i;
 L = (PolyNode*)malloc(sizeof(PolyNode));
 L->next = NULL;
 r = L;
 for ( i = 0 ; i < n ; i++)
 {
  s = (PolyNode*)malloc(sizeof(PolyNode));
  s->coef = a[i].coef;
  s->exp = a[i].exp;
  r->next = s;
  r = s;
 }

 r->next = NULL;
}

void Sort (PolyNode* &head)
{
 PolyNode* p = head->next;
 PolyNode *q , *r;
 if (p != NULL)
 {
  r = p->next;
  p->next = NULL;//构造 只有 一个节点的表
  p = r;
  while (p != NULL)
  {
   r = p->next;
   q = head;
   while (q->next != NULL && q->next->exp > p->exp)
   {
    q = q->next ;
   }

   p->next = q->next;
   q->next = p;
   p = r;
  }
 }
}


void Add(PolyNode* ha , PolyNode* hb , PolyNode* &hc)
{
 PolyNode* pa = ha->next;
 PolyNode* pb = hb->next;
 PolyNode *s , *tc;
 float c;
 hc = (PolyNode*)malloc(sizeof(PolyNode));
 tc = hc;
 while (pa != NULL && pb != NULL)
 {
  if (pa->exp > pa->exp)
  {
   s = (PolyNode*)malloc(sizeof(PolyNode));
   s->exp = pa->exp;
   s->coef = pa->coef;
   tc->next = s;
   tc = s;
   pa = pa->next;
  }
  else if (pa->exp < pb->exp)
  {
   s = (PolyNode*)malloc(sizeof(PolyNode));
   s->exp = pb->exp;
   s->coef = pb->coef;
   tc->next = s;
   tc = s;
   pb = pb->next;
  }
  else
  {
   c = pa->coef + pb->coef;
   if ( c != 0)
   {
    s = (PolyNode*)malloc(sizeof(PolyNode));
    s->exp = pa->exp;
    s->coef = c;
    tc->next = s;
    tc = s;
   }

   pa = pa->next;
   pb = pb->next;
  }
 }

 if (pb != NULL)
 {
  pa = pb;
 }
 while (pa != NULL)
 {
  s = (PolyNode*)malloc(sizeof(PolyNode));
  s->exp = pa->exp;
  s->coef = pa->coef;
  tc->next = s;
  tc = s;
  pa = pa->next;
 }

 tc->next = NULL;

}


int main (void)
{
 PolyNode *ha , *hb , *hc;
 PolyArray a = {{1.2,0},{2.5,1},{3.2,3},{-2.5,5}};
 PolyArray b = {{-1.2,0},{2.5,1},{3.2,3},{2.5,5},{5.4,10}};
 CreateListR(ha , a, 4);
 CreateListR(hb , b , 5);
 printf("orign poly  :");
 DisplayList(ha);
 printf("orign poly  :");
 DisplayList(hb);
 Sort(ha);
 Sort(hb);
 printf("seq poly :");
 DisplayList(ha);
 printf("seq poly  :");
 DisplayList(hb);
 Add(ha , hb , hc);
 printf("Adding   :");
 DisplayList(hc);
 return 0;
}


 

posted on 2007-04-17 12:41  tivoli_chen  阅读(341)  评论(0编辑  收藏  举报