链表操作:
#include <stdio.h> #include <iostream.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <algorithm> #include <vector> using namespace std; typedef struct student { int data; struct student* next; }node; node* Create() { printf("Enter key value:\n"); int x; node* L = (node*)malloc(sizeof(node)); L->next = NULL; node* pos = L; while(scanf("%d",&x)){ pos->next = (node*)malloc(sizeof(node)); pos->next->data = x; pos->next->next = NULL; pos = pos->next; } printf("Queue established!\n"); return L; } void PrintQueue(node* L) { node* pos = L; while(pos->next != NULL){ printf("%d ",pos->next->data); pos = pos->next; } printf("\n"); } node* FindPrevious(node* L,int key) { if(L->next==NULL){ printf("Empty List!\n"); exit(0); } node* pos = L; while(pos->next!=NULL && pos->next->data != key){ pos = pos->next; } return pos;//²éÕҳɹ¦£¬·µ»ØÏàÓ¦½ÚµãµØÖ·£»·ñÔò·µ»Ø×îºóÒ»¸ö½ÚµãµØÖ· } int IsLast(node* p) { if(p->next == NULL) return 1; else return 0; } void Insert(node* p,int key) { node* temp = p; temp = (node*)malloc(sizeof(node)); temp->data = key; temp->next = p->next; p->next = temp; } void DeleteEle(node* L,int key) { if(L==NULL){ printf("Empty List!\n"); exit(0); } node* p = FindPrevious(L,key); if(!IsLast(p)){ node* temp = p->next; p->next = temp->next; free(temp); } } void DeleteList(node* L) { node* p = L->next; L->next = NULL; while(p!=NULL) { node* temp = p->next; free(p); p=temp; } } void main(void) { node* p = Create(); PrintQueue(p); //DeleteEle(p,2); //printf("After Deletion:\n"); //Insert(p,4); //printf("After Insertion:\n"); DeleteList(p); printf("After DeleteList:\n"); PrintQueue(p); }
栈操作(链表):
#include <stdio.h> #include <iostream.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <algorithm> #include <vector> using namespace std; typedef struct student { int data; struct student* next; }node; node* CreateStack() { node* S; S = (node*)malloc(sizeof(node)); S->next = NULL; return S; } void Push(node* S,int key) { node* temp = (node*)malloc(sizeof(node)); temp->data = key; temp->next = S->next; S->next = temp; } node* Top(node* S) { if(S->next==NULL){ printf("Empty Stack!\n"); exit(0); } return S->next; } void Pop(node* S) { if(S->next==NULL){ printf("Empty Stack!\n"); exit(0); } node* temp = S->next; S->next = temp->next; free(temp); } void PrintStack(node* L) { node* pos = L; while(pos->next != NULL){ printf("%d ",pos->next->data); pos = pos->next; } printf("\n"); } void main(void) { printf("Enter key value:\n"); int key; node* S = CreateStack(); while(scanf("%d",&key)){ Push(S,key); } PrintStack(S); Pop(S); printf("After one Pop:\n"); PrintStack(S); Push(S,key); printf("After Push %d:\n",key); PrintStack(S); }
栈操作(数组):
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <algorithm>
#include <vector>
using namespace std;
typedef struct S
{
int Capacity;
int TopOfStack;
int* Array;
}Stack;
Stack* CreateStack()
{
Stack* S = (Stack*)malloc(sizeof(Stack));
S->Capacity = 20;
S->TopOfStack = -1;
S->Array = (int*)malloc(sizeof(Stack));
return S;
}
int IsFull(Stack* S)
{
if (S->Capacity-1 == S->TopOfStack)
{
return 1;
}
else return 0;
}
void Push(Stack* S,int key)
{
if (IsFull(S))
{
printf("ERROR: Stack Is Full!\n");
exit(0);
}
else
{
S->Array[++S->TopOfStack] = key;
}
}
int IsEmpty(Stack* S)
{
if(S->TopOfStack == -1) return 1;
else return 0;
}
void Pop(Stack* S)
{
if (IsEmpty(S))
{
printf("ERROR: Stack Is Empty!\n");
exit(0);
}
else
{
--(S->TopOfStack);
}
}
void PrintStack(Stack* S)
{
if (IsEmpty(S))
{
printf("ERROR: Stack Is Empty!\n");
exit(0);
}else{
int index = S->TopOfStack;
while(index>=0){
printf("%d ",S->Array[index--]);
}
printf("\n");
}
}
void main(void)
{
printf("Enter key value:\n");
int key;
Stack* S = CreateStack();
while(scanf("%d",&key)){
Push(S,key);
}
PrintStack(S);
Pop(S);
printf("After one Pop:\n");
PrintStack(S);
Push(S,key);
printf("After Push %d:\n",key);
PrintStack(S);
}
循环链表:
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <algorithm>
#include <vector>
using namespace std;
typedef struct student
{
int data;
struct student* next;
}node;
typedef struct Ring
{
node* L;
int NodeNum;
}ring;
ring* CreateRing()//返回入口地址
{
printf("Enter key value:\n");
int x;
node* L = NULL;
int NodeNum = 0;
node* CurrPos = L;
while(scanf("%d",&x)){
if (CurrPos==NULL)
{
CurrPos = (node*)malloc(sizeof(node));
CurrPos->data = x;
CurrPos->next = CurrPos;
L = CurrPos;
NodeNum++;
}
else
{
node* temp = CurrPos->next;
CurrPos->next = (node*)malloc(sizeof(node));
CurrPos->next->data = x;
CurrPos->next->next = temp;
CurrPos = CurrPos->next;
NodeNum++;
}
}
ring* QRing = (ring*)malloc(sizeof(ring));
QRing->L = L;
QRing->NodeNum = NodeNum;
printf("Ring established!\n");
return QRing;
}
void PrintRing(ring* QRing)
{
node* pos = QRing->L;
do{
printf("%d ",pos->data);
pos = pos->next;
}while(pos!= QRing->L);
printf("\n");
}
void Delete_dist(ring* QRing,int dist)
{
node* L = QRing->L;
int n = QRing->NodeNum;
node* curr = L;
while(n){
node* victim = curr;
for(int m = 0; m<dist-1; m++){
victim = victim->next;
}
node* previc = victim;
victim = victim->next;
previc->next = victim->next;
printf("%d\t",victim->data);
free(victim);
curr = curr->next;
n--;
}
printf("\n");
}
void main(void)
{
ring* p = CreateRing();
PrintRing(p);
Delete_dist(p,3);
PrintRing(p);
}