예제 #1
0
파일: imp.c 프로젝트: SunshineW/DSExercise
void AddTerm(SLList *L)
{
	Term newTerm = { 0 };
	printf("Please input the c: ");
	scanf("%d", &(newTerm.c));
	printf("Please input the e: ");
	scanf("%d", &(newTerm.e));
	if (newTerm.c != 0)
	{
		SLList *find = SLWalk(L, Find, &newTerm);
		if (find == NULL)  /* 该项不存在链表中, 把该项插入到链表中, 并保持链表有序  */
		{
			SLList *insertPos = SLWalk(L, FindInsertPosition, &newTerm); /* 寻找插入位置 */
			if (insertPos == NULL)
			{
				SLAppend(L, 0, &newTerm, sizeof(Term));
			}
			else
			{
				SLAdd(SLGetPri(L, insertPos), 0, &newTerm, sizeof(Term));
			}

		}
		else  /* 该项已存在于链表中, 则合并此项 */
		{
			Term *oldTerm = SLGetData(find, NULL, NULL);
			newTerm.c += oldTerm->c;
			if (newTerm.c == 0)  /* 如果合并后 系数为0, 则删除该项 */
			{
				SLDeleteNext(SLGetPri(L, find));
			}
			else
			{
				SLUpdate(find, 0, &newTerm, sizeof(Term));
			}
		}
	}
}
예제 #2
0
int main(void)
{
  BOOK Book[] =
  {
    {"Expert C Programming", "van der Linden"},
    {"C Programming FAQs", "Summit"},
    {"C++ Programming Language", "Stroustrup"},
    {"Algorithms in C", "Sedgewick"},
    {"Teach Yourself BCB", "Reisdorph"},
    {"The Standard C Library", "Plauger"},
    {"C++ Unleashed", "Liberty"},
    {"Data Structures & Algorithms", "Lafore"},
    {"C Programming Language", "Kernighan & Ritchie"},
    {"Linux Unleashed", "Husain and Parker"},
    {"C Unleashed", "Heathfield & Kirby"},
    {"C : A Reference Manual", "Harbison & Steele"},
    {"DOS Programmers Reference", "Dettmann & Johnson"},
    {"C: How to Program", "Deitel & Deitel"},
    {"Builder Unleashed", "Calvert"},
    {"UNIX Unleashed", "Burk and Horvath"}

  };

  SLLIST *List = NULL;
  SLLIST *Removed = NULL;

  BOOK *Data;

  FIELD_INFO FldInfo = { 30, 30};
  size_t NumBooks = sizeof Book / sizeof Book[0];

  size_t i;

  /* Populate the list */
  for(i = 0; i < NumBooks; i++)
  {
    if(SL_SUCCESS !=
          SLFront(&List, 0, Book + i, sizeof(BOOK)))
    {
      puts("Couldn't allocate enough memory.");
      SLDestroy(&List);
      exit(EXIT_FAILURE);
    }
  }

  /* Print the list */
  SLWalk(List, PrintBook, &FldInfo);

  /* Remove one item */
  Removed = List;

  for(i = 0; i < NumBooks / 2; i++)
  {
    Removed = Removed->Next;
  }

  Data = SLGetData(Removed->Next, NULL, NULL);
  printf("\nRemoving title %s\n\n", Data->Title);
  SLDeleteNext(Removed);

  /* Print the list again to confirm deletion */
  SLWalk(List, PrintBook, &FldInfo);

  /* Destroy the list */
  SLDestroy(&List);

  return 0;
}