
本文共 14339 字,大约阅读时间需要 47 分钟。
������������
������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
//������������������struct List{ int data; //��������� struct List *next; //���������};//���������������������typedef struct List{ int data; //��������� struct List *next; //��������������� struct Lsit *front; //���������������};typedef struct List* pElem;typedef struct List eElem;
������������������������������������������������������������������������������������������������
���������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������
pElem CreateList(){ pElem head = (pElem)malloc( sizeof(eElem) ); assert(head != NULL ); //������������������head -> next = head -> front = NULL; //������������������������������ return head;}
���������������������
���������������������������������������������������������tmp->next = head->next���������������������������������������������������������
void InsertElem( pElem head, int data ){ if ( head == NULL ){ printf("The list is empty.\n"); //������������������������������ return; } pElem tmpHead = head; //������������������������������������ if( tmpHead->next ==NULL ){ //��������������������������������������� pElem addition = (pElem)malloc( sizeof(eElem) ); assert( addition != NULL ); addition->data = data; //��������������� addition->next = tmpHead->next; //������������������ tmpHead->next = addition; addition->front = tmpHead; //������������������front��������������� } else{ //��������������������������������������� pElem addition = (pElem)malloc( sizeof(eElem) ); assert( addition != NULL ); addition -> data = data; //��������������� tmpHead->next->front = addition; //������������������������������front������ addition->front = tmpHead; //������������������front��������������������� addition->next = tmpHead->next; //���������������next ������������������������������������������ tmpHead->next = addition; //���������������next������������������������ }}
������������������������addition���������������������������������������������if���������������������������������������������
���������������������
���������������������next������������������������������front���������������
void IlluList( pElem head ){ printf("-----------------------------\n"); if( head == NULL ){ printf("The list is empty.\n"); return; } pElem tmpHead = head; while( tmpHead->next != NULL ){ tmpHead = tmpHead->next; //��������������������������������������������������������������������������� printf("%d\n", tmpHead->data); } //������tmpHead������������������������������������������ while( tmpHead->front->front != NULL ){ printf("%d\n", tmpHead->data); //��������������������������� tmpHead = tmpHead->front; } printf("%d\n", tmpHead->data); return;}
������������������������������������tmpHead���������������������������������������������������������������front������������������������������������������������tmpHead->front !=NULL���������������������������������������������������������������������������������������������������������������������������������������������������������tmpHead->front->front !=NULL
������������������
���������������������������������������������������������������������������������������������������������������������������������������
���������������������������������addition������������������addition���������������������head������������������������������NULL���������������������������������
void DeleteElem( pElem head, int data ){ if( head == NULL ){ printf("The list is empty.\n"); return; } pElem tmpHead = head; while( tmpHead->next != NULL ){ tmpHead = tmpHead->next; if( tmpHead->data == data ){ tmpHead->front->next = tmpHead->next; //������������������������������������next������������������������������������������ tmpHead->next->front = tmpHead->front; //���������������������������������front������������������������������������������ break; } } free(tmpHead); //������������}
������������
������������������������������������������������������������������������������������������������������������������������������������
void DestroyList( pElem head ){ pElem tmp; while( head->next != NULL ){ tmp = head; //������������������ head = head->next; free(tmp); } free(head);}
���������tmp���������������head���������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������
���������������������������������������26���������������������������������������������������3������������������
- DEFGHIJKLMNOPQRSTUVWXYZABC
���������������������������������������������-3������������������
XYZABCDEFGHIJKLMNOPQRSTUVW
#include
#include #define OK 1#define ERROR 0typedef char ElemType;typedef int Status;typedef struct DualNode{ ElemType data; struct DualNode *prior; struct DualNode *next;}DualNode, *DuLinkList;Status InitList(DuLinkList *L){ DualNode *p, *q; int i; *L = (DuLinkList)malloc(sizeof(DualNode)); if( !(*L) ) { return ERROR; } (*L)->next = (*L)->prior = NULL; p = (*L); for( i=0; i < 26; i++ ) { q = (DualNode *)malloc(sizeof(DualNode)); if( !q ) { return ERROR; } q -> data = 'A'+i; q -> prior = p; q -> next = p -> next; p -> next = q; p = q; } p->next = (*L)->next; (*L)->next->prior = p; return OK;}void Caesar(DuLinkList *L, int i){ if( i > 0 ) { do { (*L) = (*L)->next; }while( --i ); } if( i < 0 ) { do { (*L) = (*L)->next; }while( ++i ); }}int main(){ DuLinkList L; int i, n; InitList(&L); printf("������������������������"); scanf("%d", &n); printf("\n"); Caesar(&L, n); for( i=0; i < 26; i++ ) { L = L->next; printf("%c", L->data); } printf("\n"); return 0;}
������������
������(Stack)���������������������(Last in first out ,LIFO)������������������������������������������������������������������
������������������������������������������(top)������������������������������(bottom)���
������������������������
typedef struct{ ElemType *base; ElemType *top; int stackSize;}sqStack;
base���������������������������������top���������������������������������stackSize���������������������������������������������
���������������
#define STACK_INIT_SIZE 100initStack(sqStack *s){ s -> base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType) ); if( !s->base ) exit(0); s -> top = s -> base; //������������������������������ s -> stackSize = STACK_INIT_SIZE;}
������������
- ������������������������������������������������������������
- ���������������������������������������������������������������������top������������+1������������������������
#define STACKINCREMENT 10Push(sqStack *s, ElemType e){ //��������������������������� if( s->top - s->base >= s->stackSize ) { s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType)); if( !s->base ) exit(0); s->top = s->base + s->stackSize; //������������ s->stackSize = s->stackSize + STACKINCREMENT; //������������������������ } *(s->top) = e; s->top++;}
������������
���������������������������������������������������������������������������
���������������������������������������������������������-1
Pop(sqStack *s, ElemType *e){ if( s->top == s->base ) //��������� return; *e = *--(s->top);}
���������������
������������������������������������������������������������������������������
���������s->top������������������s->base���������������s->base������s->top������������������������������������
ClearStack(sqStack *s){ s->top = s->base;}
���������������
���������������������������������������������������������������������������������������������������������������
DestroyStack(sqStack *s){ int i, len; len = s->stackSize; for( i=0; i < len; i++ ){ free( s->base ); s->base++; } s->base = s->top = NULL; s->stackSize = 0;}
������������������������
���������������������������������������������������������������������������������s.top - s.base������
���������������������������������������������������������������������������s.stackSize, ���������������������������������������������
int StackLen(sqStack s){ return (s.top - s.base );}
������������
������������������������������������������������������������������������������
#include
#include #include #define STACK_INIT_SIZE 20#define STACKINCREMENT 10typedef char ElemType;typedef struct{ ElemType *base; ElemType *top; int stackSize;}sqStack;void InitStack(sqStack *s){ s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if( !s->base ) { exit(0); } s->top = s->base; s->stackSize = STACK_INIT_SIZE;}void Push(sqStack *s, ElemType e){ if( s->top - s->base >= s->stackSize ) { s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType) ); if( !s->base ) { exit(0); } } *(s->top) = e; s->top++;}void Pop(sqStack *s, ElemType *e){ if( s->top == s->base ) { return; } *e = *--(s->top);}int StackLen(sqStack s){ return (s.top - s.base);}int main(){ ElemType c; sqStack s; int len, i, sum = 0; InitStack(&s); printf("������������������������������#���������������������\n"); scanf("%c", &c); while( c != '#' ) { Push(&s, c); scanf("%c", &c); } getchar(); //���'\n'������������������ len = StackLen(s); printf("������������������������%d\n", len); for(i = 0; i < len; i++ ) { Pop(&s, &c); sum = sum + (c-48) * pow(2, i); } printf("���������������������������%d\n", sum); return 0;}
������������������������
- ������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������
���������������Push���������������������������e���������������s���top���������������
Status Push(LinkStack *s, ElemType e){ LinkStackPtr p = (LinkStackPtr) malloc (sizeof(StackNode)); p->data = e; p->next = s->top; s->top = p; s->count++; return OK;}
������������
������������p���������������������������������������������������������������������������������p������
Status Pop(LinkStack *s, ElemType *e ){ LinkStackPtr p; if( StackEmpty(*s)) //��������������������� return ERROR; *e = s->top->data; p = s->top; s->top = s->top->next; free(p); s->count--; return OK;}
(������)������������������
- ������������������������������������������������
- ���������������������������
- ���������������----------->������������������
- a+b-------------->a b +
- a+(b-c) --------------------> a b c - +
- a+(b-c)*d --------------------> a b c - d * +
- a+d*(b-c)-----------------> a d b c - * +
#include#include #include #define STACK_INIT_SIZE 20#define STACKINCREMENT 10#define MAXBUFFER 10typedef double ElemType;typedef struct{ ElemType *base; ElemType *top; int stackSize;}sqStack;InitStack(sqStack *s){ s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if( !s->base ) exit(0); s->top = s->base; s->stackSize = STACK_INIT_SIZE;}Push(sqStack *s, ElemType e){ // ������������������������������������������ if( s->top - s->base >= s->stackSize ) { s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType)); if( !s->base ) exit(0); s->top = s->base + s->stackSize; s->stackSize = s->stackSize + STACKINCREMENT; } *(s->top) = e; // ������������ s->top++;}Pop(sqStack *s, ElemType *e){ if( s->top == s->base ) return; *e = *--(s->top); // ������������������������������������������}int StackLen(sqStack s){ return (s.top - s.base);}int main(){ sqStack s; char c; double d, e; char str[MAXBUFFER]; int i = 0; InitStack( &s ); printf("���������������������������������������������������������������������������������������������#������������������: \n"); scanf("%c", &c); while( c != '#' ) { while( isdigit(c) || c=='.' ) // ������������������ { str[i++] = c; str[i] = '\0'; if( i >= 10 ) { printf("���������������������������������������\n"); return -1; } scanf("%c", &c); if( c == ' ' ) { d = atof(str); Push(&s, d); i = 0; break; } } switch( c ) { case '+': Pop(&s, &e); Pop(&s, &d); Push(&s, d+e); break; case '-': Pop(&s, &e); Pop(&s, &d); Push(&s, d-e); break; case '*': Pop(&s, &e); Pop(&s, &d); Push(&s, d*e); break; case '/': Pop(&s, &e); Pop(&s, &d); if( e != 0 ) { Push(&s, d/e); } else { printf("\n������������������������\n"); return -1; } break; } scanf("%c", &c); } Pop(&s, &d); printf("\n���������������������������%f\n", d); return 0;}
发表评论
最新留言
关于作者
