线性表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include "stdio.h" #define max 80 #define Error 0 #define Ok 1 typedef int ElemType; typedef struct { ElemType elem[max]; int last; }SeqList; SeqList *NewSeq() { SeqList *L; L=(SeqList *)malloc(sizeof(SeqList)); L->last=0; return L; } int AddSeq(SeqList *L,int i,ElemType e) { int k; if(L->last>=max) { printf("The List is full!\n"); return(Error); } i--; if(i>L->last) { printf("Error Add Point!\n"); return(Error); } for(k=L->last;k>=i;k--) L->elem[k]=L->elem[k-1]; L->elem[i]=e; L->last++; return(Ok); } int MovSeq(SeqList *L,int i) { if(L->last==0) { printf("The List is empty!\n"); return(Error); } i--; if(i>=L->last) {printf("Error Mov Point!\n"); return(Error); } for(;i<=L->last-1;i++) L->elem[i]=L->elem[i+1]; L->last--; return(Ok); } int PtSeq(SeqList *L) { int i; if(L->last==0) { printf("The List is Empty!\n"); return(Error); } for(i=0;i<L->last;i++) printf("%d ",L->elem[i]); return(Ok); } main() { int i; int o=1; ElemType e; char c; SeqList *L; L=NewSeq(); do {printf("\n********************************************************************************"); printf("\nPlease press your key to choose your order:"); printf("\n press 'a' to Add Element;"); printf("\n press 'm' to Mov Element;"); printf("\n press 'p' to Print List;"); printf("\n press 'q' to Quite the Program.\n-"); loop: c=getch(); switch(c) {case 'a': printf("%c\n",c); printf("Please Enter the Add Point:"); scanf("%d",&i); getchar(); printf("Please Enter the Add Elem :"); scanf("%d",&e); getchar(); AddSeq(L,i,e); break; case 'm' :printf("%c\n",c); printf("Please Enter the Mov Point:"); scanf("%d",&i); getchar(); MovSeq(L,i); break; case 'p' :printf("%c\n",c); printf("The List is:\n"); PtSeq(L); getchar(); break; case 'q': printf("%c\n",c); printf("Are you sure to quit the program?(y/n)-"); while(!(c=='y'||c=='n')) c=getch(); if(c=='y') o=0; else printf("%c\n",c); break; default: goto loop; break; } }while(o); } |
Related Posts
- Linux下Socket通信、共享内存和信号量混合使用的例子
- C语言与PHP与JavaScript通信
- A Sock 5 Proxy Server Written in Golang
- 输出文本中最长的一行
- Go语言文件操作
- PHP和C语言共享内存通信以及信号量互斥
- 删除一个字符串中的空格
- shell编程
- 如何编译安装Go语言
- Java常用链表(列表)操作
Tags: C语言.
Leave a comment