MOOC浙江大學 資料結構第二講 線性結構 2.1-2.2
2.1 線性表及其實現
例題:
在順序結構表示的線性表中,刪除第i個元素(陣列下標為i-1),需要把後面的所有元素都往前挪一位,相應的語句是:
for (___________ )
PtrL->Data[j-1]=PtrL->Data[j];
其中空缺部分的內容應該是 j = i; j< = PtrL->Last; j++
線性表的定義和操作--1,順序表:
typedef int Position; typedef struct LNode *List; struct LNode{ ElementType Data[MAXSIZE]; Position Last; };
//初始化 List MakeEmpty() { List L; L=(List)malloc(sizeof(struct LNode)); L->last=-1; return L; }
//查詢 #define ERROR -1 Position Find(List L,ElementType X){ Position i=0; while(i<=L->last&&L->Data[i]!=X) i++; if(i>L->Last) return ERROR;//如果沒找到,返回錯誤資訊 else return i;//找到後返回的是儲存位置 }
//插入 bool Insert(List L,ElementType X,Position P){ //在L的指定位置P之前插入一個新元素X Position i; if(L->last==MAXSIZE-1){// printf("表滿"); return false; } if(P<0||P>L->Last+1){//檢查插入位置的合法性 printf("位置不合法"); return false; } for(i=L->Last;i>=P;i--) L->Data[i+1]=L->Data[i];//將位置P及以後的元素順序向後移動 L->Data[P]=X;//新元素插入 L->Last++;//Last仍指向最後元素 return false; }
//刪除 bool Delete(List L,Position P){ Position i; if(P<0||P>L->Last){ printf("位置%d不存在元素",P); return false; } for(i=P+1;i<L->Last;i++) L->Data[i-1]=L->Data[i];//將位置P+1及以後的元素順序向前移動 L->Last--;//Last仍指向最後元素 return true; }
2,鏈式表:
typedef struct LNode *PtrToLNode; struct LNode{ ElementType Data; PtrToLNode Next; }; typedef PtrToLNode Position; typedef PtrToLNode List;
//查詢 #define ERROR NULL
Position Find(List L,ElementType X){ Position p=L;//p指向L的第1個結點 while(p&&p->Data!=X) p=p->Next; return p; }
//帶頭結點的插入 bool Insert(List L,ElementType X,Position P){ //這裡預設L有頭節點 Position tmp,pre; //查詢p的前一個結點 for(pre=L;pre&&pre->Next!=p;pre=pre->next); if(pre==NULL){ printf("插入位置引數錯誤"); return false; } else { tmp=(Position)malloc(sizeof(struct LNode));//申請,裝填結點 tmp->Data=X; tmp->Next=P; pre->Next=tmp; return true; } }
//帶頭節點的刪除 bool Delete(List L,Position P){ //這裡預設L有頭結點 Position tmp,pre; //查詢p的前一個結點 for(pre=L;pre&&pre->next!=P;pre=pre->next); if(pre==NULL||P==NULL){ printf("刪除位置引數錯誤\n"); return false; } else{//找到P的前一個結點pre //將P位置的結點刪除 pre-Next=P->Next; free(P); return true; } }
2.2 堆疊
1,堆疊的定義與操作--順序棧:
typedef int Position; struct SNode{ ElementType *Data; //儲存元素的陣列 Position Top; //棧頂指標 int MaxSize; //堆疊最大容量 } typedef struct SNode *Stack;
Stack CreateStack(int MaxSize){ Stack S=(Stack)malloc(sizeof(struct SNode)); S->Data=(ElementType *)malloc(MaxSize*sizeof(ElementType)); S->Top=-1; S->MaxSize=MaxSize; return S; }
bool IsFull(Stack S){ return (s->Top==S->MaxSize-1); }
bool Push(Stack S,ElementType X){ if(IsFull(S)){ printf("堆疊滿); return false; } else{ S->Data[++(S->Top)] = X; return true; } }
bool IsEmpty(Stack S){ return (S->Top==-1); }
ElementType Pop(Stack S){ if(IsEmpty(S)){ printf("堆疊空"); return ERROR; } else return (S->Data[(S->Top)--]); }
鏈式棧:
typedef struct SNode *PtrToLNode; struct SNode{ ElementType Data; PtrToLNode Next; }; typedef PtrToLNode Stack;
Stack CreateStack(){ //構建一個堆疊的頭節點,返回該節點指標 Stack S; S=(Stack)malloc(sizeof(struct SNode)); S->Next=NULL; return S; }
bool IsEmpty(Stack S){ //判斷堆疊S是否為空, return (S->Next==NULL); }
bool Push(Stack S,ElementType X) {//將元素X壓入堆疊S PtrToLNode TmpCell; TmpCell=(PtrToNode)malloc(sizeof(struct SNode)); TmpCell->Data=X; TmpCell->Next=S->Next; S->Next=TmpCell; return true; }
ElementType Pop(Stack S){ //刪除並返回堆疊S的棧頂元素 PtrToLNode FirstCell; ElementType TopElem; if(IsEmpty(S)){ printf("堆疊空"); return ERROR; } else{ FirstCell=S->Next; TopElem=FirstCell->Data; S->Next=FirstCell->Next; free(FirstCell); return TopElem; } }