本章需要掌握:
- 1、单链表怎么定义?
- 2、对单链表来说头插、尾插、查找(位/值)、插入i位置e操作代码。
1、头插、尾插、查找(位/值)、插入i位置e
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
| #include <stdio.h> #include <stdlib.h>
typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList;
void list_head_insert(LNode* &L) { L= (LinkList)malloc(sizeof(LNode)); L->next=NULL; ElemType x; scanf("%d",&x); LNode *s; while(x!=9999) { s=(LinkList)malloc(sizeof(LNode)); s->data=x; s->next=L->next; L->next=s; scanf("%d",&x); } }
void list_tail_insert(LNode* &L) { L= (LinkList)malloc(sizeof(LNode)); L->next=NULL; ElemType x; scanf("%d",&x); LNode *s,*r=L; while(x!=9999) { s=(LinkList)malloc(sizeof(LNode)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; }
void print_list(LinkList L) { L=L->next; while(L!=NULL) { printf("%3d",L->data); L=L->next; } printf("\n"); }
LinkList GetElem(LinkList L,int SearchPos) { int j=0; if(SearchPos<0) { return NULL; } while(L&&j<SearchPos) { L=L->next; j++; } return L; }
LinkList LocateElem(LinkList L,ElemType SearchVal) { while(L) { if(L->data==SearchVal) { return L; } L=L->next; } return NULL; }
bool ListFrontInsert(LinkList L,int i,ElemType InsertVal) { LinkList p= GetElem(L,i-1); if(NULL==p) { return false; } LinkList q; q=(LinkList)malloc(sizeof(LNode)); q->data=InsertVal; q->next=p->next; p->next=q; return true; }
int main() { LinkList L,search;
list_tail_insert(L); print_list(L);
bool ret; ret=ListFrontInsert(L,2,99); print_list(L); return 0; }
|
🌺2、OJ作业
网站:http://oj.lgwenda.com/
1、链表(LinkList)
描述:输入3 4 5 6 7 9999一串整数,9999代表结束,通过头插法新建链表,并输出,通过尾插法新建链表并输出。
输入:3 4 5 6 7 9999,第二行也是3 4 5 6 7 9999,数据需要输入两次
输出:如果输入是3 4 5 6 7 9999,那么输出是7 6 5 4 3,数之间空格隔开,尾插法的输出是3 4 5 6 7
🌺🌺🌺题解ヾ(^▽^)ゞ🌼🌼🌼
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
| #include <stdio.h> #include <stdlib.h>
typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList;
void list_head_insert(LinkList &L) { L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; ElemType x; LinkList s; scanf("%d",&x); while(x!=9999) { s=(LinkList)malloc(sizeof(LNode)); s->data=x; s->next=L->next; L->next=s; scanf("%d",&x); } }
void PrintList(LinkList L) { L = L->next; while (L != NULL) { printf("%d", L->data); L = L->next; if (L != NULL) { printf(" "); } } printf("\n"); }
void list_tail_insert(LinkList &L) { L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; ElemType x; LinkList s,r=L; scanf("%d",&x); while(x!=9999) { s=(LinkList)malloc(sizeof(LNode)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; } int main() { LinkList L; list_head_insert(L); PrintList(L); list_tail_insert(L); PrintList(L); return 0; }
|