【數據結構】—— 雙向鏈表ADT

抽象數據類型(Abstract Data Type,ADT)是計算機科學中具有類似行爲的特定類別的數據結構的數學模型;或者具有類似語義的一種或多種程序設計語言的數據類型。抽象數據類型是間接定義的,通過其上的可執行的操作以及這些操作的效果的數學約束(與可能的代價)。——維基百科


接口頭文件DuLinkedList.h

 /**************************************************************
*	Multi-Include-Prevent Section
**************************************************************/

#ifndef DULINKEDLIST_H_INCLUDED
#define DULINKEDLIST_H_INCLUDED

/**************************************************************
*	Macro Define Section
**************************************************************/

#define OVERFLOW -1

/**************************************************************
*	Struct Define Section
**************************************************************/

// define element type
typedef int ElemType;

// define struct of linked list
typedef struct DuLNode {
	ElemType data;
  	struct DuLNode *prior, *next;
} DuLNode, *DuLinkedList;

// define status
typedef enum Status {
	ERROR,
	SUCCESS,
} Status;


/**************************************************************
*	Prototype Declare Section
**************************************************************/

/**
 *  @data        : void *InitList_DuL(DuLinkedList L)
 *	@description : initialize an empty linked list with only the head node
 *	@param		 : L(the head pointer)
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *InitList_DuL(DuLinkedList L);

/**
 *  @data        : void DestroyList_DuL(DuLinkedList L)
 *	@description : destroy a linked list
 *	@param		 : L(the head pointer)
 *	@return		 : status
 *  @notice      : None
 */
void DestroyList_DuL(DuLinkedList L);

/**
 *  @data        : void *Insert_Node(DuLinkedList head)
 *	@description : insert node q before node p
 *	@param		 : L(the head pointer)
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *Insert_Node(DuLinkedList head);

/**
 *  @data        : void *DeleteList_DuL(DuLinkedList L, ElemType e)
 *	@description : delete the first node after the node p and assign its value to e
 *	@param		 : L(the head pointer), ElemType e
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *DeleteList_DuL(DuLinkedList L, ElemType e);

/**
 *  @data        : void TraverseList_DuL(DuLinkedList L, void (*visit)(ElemType e))
 *	@description : traverse the linked list and call the funtion visit
 *	@param		 : L(the head node), visit
 *	@return		 : Status
 *  @notice      : None
 */
void TraverseList_DuL(DuLinkedList L, void (*visit)(ElemType e));
void visit(ElemType e);

/**
 *  @name        : void *Create(int n)
 *	@description : Create a linked list
 *	@param		 : int n
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *Create(int n);

/**
 *  @name        : void *SearchList(LinkedList L, int i)
 *	@description : find the first node in the linked list according to e
 *	@param		 : L(the head pointer), e
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *SearchList(DuLinkedList L, int i);

int InputNumber(); // Check input number.

 /**************************************************************
*	End-Multi-Include-Prevent Section
**************************************************************/
#endif

使用接口main.c

#include <stdio.h>
#include <stdlib.h>
#include "duLinkedList.h"

int main()
{
    DuLinkedList head; // Head of DuLinkedList.
    int flag = 0; // Whether exist DuLinkedList.
    while(SUCCESS)
    {
        system("cls");
        printf("\n\n\n---------------------------\n");
        printf(">>> 1.Creat.               \n");
        printf(">>> 2.Print.               \n");
        printf(">>> 3.Initialize.          \n");
        printf(">>> 4.Destroy.             \n");
        printf(">>> 5.Insert.              \n");
        printf(">>> 6.Delete.              \n");
        printf(">>> 7.Search.              \n");
        printf(">>> 8.Exit.                \n");
        printf("---------------------------\n");
        if(flag)
        {
            TraverseList_DuL(head, visit); // Print LinkedList.
        }
        else
        {
            printf("No LinkedList.\n");
        }
        printf("---------------------------\n");
        printf("----->>> Please input your choice:");
        switch(InputNumber())
        {
        case 1:
            {
                int n;
                printf("Length of the DuLinkedList:");
                scanf("%d", &n);
                head = Create(n);
                flag = 1;
                break;
            }
        case 2:
            if(flag)
            {
                TraverseList_DuL(head, visit);
            }
            else
            {
                printf("No LinkedList.\n");
            }
            break;
        case 3:
            head = InitList_DuL(head);
            flag = 1;
            break;
        case 4:
            if(flag)
            {
                DestroyList_DuL(head);
                flag = 0;
            }
            else
            {
                printf("No LinkedList.\n");
            }
            break;
        case 5:
            if(flag)
            {
                head = Insert_Node(head);
            }
            else
            {
                printf("No LinkedList.\n");
            }
            break;
        case 6:
            if(flag)
            {
                int x;
                printf("What to delete? ");
                scanf("%d", &x);
                head = DeleteList_DuL(head, x);
            }
            else
            {
                printf("No LinkedList.\n");
            }
            break;
        case 7:
            if(flag)
            {
                int i;
                DuLinkedList temp;
                printf("Where to search? ");
                scanf("%d", &i);
                temp = SearchList(head, i);
                printf("What you find is %d.\n", temp->data);
            }
            else
            {
                printf("No LinkedList.\n");
            }
            break;
        case 8:
            exit(0);
        default:
            break;
        }
        system("pause");
    }
    return 0;
}

實現接口DuLinkedList.c

#include <stdio.h>
#include <stdlib.h>
#include "DuLinkedList.h"

/**
 *  @data        : void *InitList_DuL(DuLinkedList L)
 *	@description : initialize an empty linked list with only the head node
 *	@param		 : L(the head pointer)
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *InitList_DuL(DuLinkedList L)
{
    L = (DuLinkedList)malloc(sizeof(DuLNode));
	if(!L)
    {
        return OVERFLOW;
    }
	L->prior = L->next = NULL;
	printf("Successfully initialize a DuLinkedList!\n");
	return L;
}

/**
 *  @data        : void DestroyList_DuL(DuLinkedList L)
 *	@description : destroy a linked list
 *	@param		 : L(the head pointer)
 *	@return		 : status
 *  @notice      : None
 */
void DestroyList_DuL(DuLinkedList L)
{
    DuLinkedList p;
    p = L;
	while(p)
    {
		p = p->next;
		free(L);
		L = p;
	}
}

/**
 *  @data        : void *Insert_Node(DuLinkedList head)
 *	@description : insert node q before node p
 *	@param		 : L(the head pointer)
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *Insert_Node(DuLinkedList head)
{
   DuLinkedList q;
   DuLinkedList pnext = head;
   int i = 1;
   int n;

   printf("Please input the position of the inserted node:");
   scanf("%d", &n);

   while((i<n) && (pnext != NULL))
   {
      i++;
      pnext = pnext->next;

   }

   if(pnext == NULL)
   {
     return ERROR;
   }
   else
   {
        q = (DuLinkedList)malloc(sizeof(DuLNode));
        printf("Please input the data of the inserted node:");
        scanf("%d", &q->data);

        q->next = pnext->next;
        if(n = 0)
        {
          pnext->next = q;
          q->next = NULL;
        }
        else
        {
          pnext->next = q;
          pnext->next->prior = q;
          q->prior = pnext;
        }
   }
   return head;
}

/**
 *  @data        : void *DeleteList_DuL(DuLinkedList L, ElemType e)
 *	@description : delete the first node after the node p and assign its value to e
 *	@param		 : L(the head pointer), ElemType e
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *DeleteList_DuL(DuLinkedList L, ElemType e)
{
    if(L == NULL || L->next == NULL)
    {
        return ERROR;
    }
    DuLinkedList q, pre;
    q = L->next;
    while(q->data != e)
    {
        pre = q;
        q = q->next;
    }
    pre->next = q->next;
    free(q);
    printf("Successfully delete node!\n");
    return L;
}

/**
 *  @data        : void TraverseList_DuL(DuLinkedList L, void (*visit)(ElemType e))
 *	@description : traverse the linked list and call the funtion visit
 *	@param		 : L(the head node), visit
 *	@return		 : Status
 *  @notice      : None
 */
void TraverseList_DuL(DuLinkedList L, void (*visit)(ElemType e))
{
    DuLinkedList p = L->next;
	while(p)
    {
		visit(p->data);
		p = p->next;
	}
	printf("\n");
}
void visit(ElemType e)
{
	printf("%d->",e);
}

/**
 *  @name        : void *Create(int n)
 *	@description : Create a linked list
 *	@param		 : int n
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *Create(int n)
{
    DuLinkedList h, p, s;
    h = (DuLinkedList)malloc(sizeof(DuLNode));

    if(h == NULL)
    {
        return OVERFLOW;
    }

    h->data = 0;
    h->prior = NULL;
    h->next = NULL;
    p = h;

    for(int i=0; i<n; i++)
    {
        s = (DuLinkedList)malloc(sizeof(DuLNode));
        if(s == NULL)
        {
            return OVERFLOW;
        }
        p->next = s;
        printf("Please enter the data for No.%d node:", i+1);
        scanf("%d", &s->data);
        s->prior = p;
        s->next = NULL;
        p = s;
    }
    printf("Successfully create a DuLinkedList!\n");
    return h;
}

/**
 *  @name        : void *SearchList(LinkedList L, int i)
 *	@description : find the first node in the linked list according to e
 *	@param		 : L(the head pointer), e
 *	@return		 : L(the head pointer)
 *  @notice      : None
 */
void *SearchList(DuLinkedList L, int i) // Maybe can create a function to realize find by value.
{
    if(L == NULL || L->next == NULL)
    {
        printf("Something wrong.\nThe DuLinkedList may not exist or there is only one node.");
        return NULL;
    }
    int j = 1;
    DuLinkedList p = L->next;
    if(i == 1)
    {
        return L;
    }
    if(i < 1)
    {
        return NULL;
    }
    while(p && i>j)
    {
        p = p->next;
        j++;
    }
    return p;
}

/**
 *  @name        : int InputNumber()
 *	@description : Input number
 *	@param		 : None
 *	@return		 : int
 *  @notice      : None
 */
int InputNumber()
{
	int num = 0; // Store converted numbers.
	int status = 0; // Flag status.
	char str[100]; // Receive string.
	do
	{
		scanf("%s", str);
		status = SUCCESS;
		for (int i = 0; str[i] != '\0'; i++)
		{
			// Check for illegal characters.
			if (i == 0)
            {
				if (str[i] == '-' || str[i] == '+')
					continue;
			}
			else
			{
				if (str[i] < '0' || str[i] > '9')
				{
					status = ERROR;
				}
			}
		}
		if (status == ERROR)
        {
			printf("No such choice, input it again:");
		}
		else
		{
			int i = 0;
			// Convert string to number.
			for (i = 0, num = 0; str[i] != '\0'; i++)
			{
				if (i == 0)
                {
					if (str[i] == '-' || str[i] == '+')
					{
						continue;
					}
					else
					{
						num *= 10;
						num += (str[i] - 48);
					}
				}
				else
				{
					num *= 10;
					num += (str[i] - 48);
				}
			}
			if (str[0] == '-')
            {
				num = -num;
			}
			// Check if the number entered is out of bounds.
			if (i >= 10)
			{
				printf("Overflow, please input again:");
				status = ERROR;
			}
		}
	} while (status == ERROR);
	return num;
}

DuLinkedList.exe

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章