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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct Node { ElemType data; struct Node *next; }Node,*LinkedList; LinkedList LinkedListInit() { Node *L; L = (Node *)malloc(sizeof(Node)); if(L == NULL) { printf("申请内存空间失败\n"); } L->next = NULL; return L; } LinkedList LinkedListCreatH_pre() { Node *L,*t; t = L = (Node *)malloc(sizeof(Node)); ElemType x; while(scanf("%d",&x) != EOF) { Node *p; p = (Node *)malloc(sizeof(Node)); p->data = x; t->next = p; p->next=NULL; t = t->next; } return L; } LinkedList LinkedListCreatH_back() { Node *L; L = (Node *)malloc(sizeof(Node)); L->next = NULL; ElemType x; while(scanf("%d",&x) != EOF) { Node *p; p = (Node *)malloc(sizeof(Node)); p->data = x; p->next = L->next; L->next = p; } return L; } LinkedList LinkedListInsert(LinkedList L,int i,ElemType x) { Node *pre=L; int tempi = 0; for (tempi = 1; tempi < i; tempi++) { pre = pre->next; } Node *p; p = (Node *)malloc(sizeof(Node)); p->data = x; p->next = pre->next; pre->next = p; return L; } LinkedList LinkedListFind(LinkedList L,ElemType x) { int i=1; Node *p=L->next; while(p->data != x) { if(p->next==NULL) { printf("没有此数!\n"); return L; } p = p->next; i++; } printf("%d位于第%d个位置!\n",x,i); return L; } LinkedList LinkedListDelete(LinkedList L,ElemType x) { Node *p,*pre=L; p = L->next; while(p->data != x) { if(p->next==NULL) { printf("没有此数!\n"); return L; } pre = p; p = p->next; } pre->next = p->next; free(p); return L; } void menu() { printf("1.输入(前插)\n"); printf("2.输入(后插)\n"); printf("3.插入数据\n"); printf("4.查找数据\n"); printf("5.删除数据\n"); printf("0.退出程序\n"); } int main() { LinkedList list,start; int choice; while(true) { system("CLS"); menu(); scanf("%d",&choice); switch(choice) { case 1: { printf("请输入单链表的数据:"); list = LinkedListCreatH_pre(); for(start = list->next; start != NULL; start = start->next) { printf("%d ",start->data); } printf("\n"); break; } case 2: { printf("请输入单链表的数据:"); list = LinkedListCreatH_back(); for(start = list->next; start != NULL; start = start->next) { printf("%d ",start->data); } printf("\n"); break; } case 3: { int i; ElemType x; printf("请输入插入数据的位置:"); scanf("%d",&i); printf("请输入插入数据的值:"); scanf("%d",&x); LinkedListInsert(list,i,x); for(start = list->next; start != NULL; start = start->next) { printf("%d ",start->data); } printf("\n"); break; } case 4: { ElemType x; printf("请输入要查找的元素的值:"); scanf("%d",&x); LinkedListFind(list,x); for(start = list->next; start != NULL; start = start->next) { printf("%d ",start->data); } printf("\n"); break; } case 5: { ElemType x; printf("请输入要删除的元素的值:"); scanf("%d",&x); LinkedListDelete(list,x); for(start = list->next; start != NULL; start = start->next) { printf("%d ",start->data); } printf("\n"); break; } case 0:return 0; } system("PAUSE"); } return 0; }
|