Data_Struct(LinkList)

  最近在学数据结构,学到链表这节作业有链表,毕竟菜鸟代码基本照看书上算法写的,再加上自己的小修改,这里先记录下来,万一哪天想看了,来看看。

  里面有用到二级指针,还是不太理解,还有就是注释不多,后续有了更好的理解,再来添加

 1 #define TRUE                    1
 2 #define FALSE                    0
 3 #define OK                    1
 4 #define ERROR                    0
 5 #define OVERFLOW                    -2
 6 #define INFEASIBLE                    -1
 7 #define Status                    int
 8 #define ElemType                int
 9
10 typedef struct LNode
11 {
12     ElemType data;
13     struct LNode *next;
14 }LNode;
15
16 typedef LNode LinkList;
17 Status ListInsert_L(LinkList L, int i, ElemType e);//插入函数
18 Status ListDelete_L(LinkList L, int i, ElemType *e);//删除某个元素
19 void CreateList_L(LinkList L, int n);//创建一个带头结点的空链表
20 void MergeList_L(LinkList *La, LinkList *Lb, LinkList **Lc);   //合并两个有序链表         

LinkList.h

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <malloc.h>
  4 #include "LinkList.h"
  5
  6
  7 //新建带头结点的空链表
  8 void CreateList_L(LinkList **L, int n)
  9 {
 10     int i; LinkList *p = NULL;
 11     (*L) = (LinkList*) malloc (sizeof (LNode));
 12     (*L) ->next = NULL;
 13     printf("请输入链表元素 :\n");
 14     for(i = n; i > 0; --i)
 15     {
 16         p = (LinkList*) malloc (sizeof (LNode));//生成新结点
 17         scanf("%d", &p -> data);//输入元素值
 18         p -> next = (*L) -> next;
 19         (*L) -> next = p;//插入到表头
 20     }
 21 }
 22
 23
 24 //插入元素
 25 Status ListInsert_L(LinkList *L, int i, ElemType e)
 26 {
 27     LinkList *p = L;
 28     LinkList *s = NULL;
 29     int j = 0;
 30     while (p->next && j < i - 1)
 31     {
 32         p = p -> next;
 33         ++ j;
 34     }
 35     if(!p || j != i - 1)
 36     return ERROR;
 37     s = (LinkList*) malloc (sizeof(LNode));
 38     s -> data = e;
 39     s -> next = p ->next;
 40     p -> next = s;
 41     return OK;
 42 }
 43
 44
 45 //删除元素
 46 Status ListDelete_L(LinkList *L, int i, ElemType *e)
 47 {
 48     LinkList *p = L;
 49     LinkList *q;
 50     int j = 0;
 51     while(p -> next && j < i - 1)
 52     {
 53         p = p -> next;
 54         ++ j;
 55     }
 56     if(!(p -> next) || j > i - 1)
 57         return ERROR;
 58     q = p -> next;
 59     p -> next = q -> next;
 60     *e= q -> data;
 61     free(q);
 62     return OK;
 63 }
 64
 65
 66
 67
 68 void MergeList_L(LinkList *La, LinkList *Lb, LinkList **Lc)
 69 {
 70     LinkList *pa = La -> next;
 71     LinkList *pb = Lb -> next;
 72     LinkList *pc = NULL;
 73     (*Lc) = pc =  La;
 74     while(pa && pb)
 75     {
 76         if(pa -> data <= pb -> data)
 77         {
 78             pc -> next = pa;
 79             pc = pa;
 80             pa = pa ->next;
 81         }
 82         else
 83         {
 84             pc -> next = pb;
 85             pc = pb;
 86             pb = pb -> next;
 87         }
 88     }
 89         pc -> next = pa ? pa : pb;//插入剩余段
 90         free(Lb);//释放Lb头结点
 91         Lb = NULL;
 92
 93 }
 94
 95
 96 void Free(LinkList *L)
 97 {
 98     if(L && L -> next)
 99          Free( L -> next);
100     free(L);
101     return;
102 }
103
104
105 int main()
106 {
107         int selectn,length0,length1,location,e,counti;
108         LNode *L1 = NULL,*L2 = NULL, *L3 = NULL, *la = NULL, *lc = NULL;
109         printf("请输入新链表长度 :\n");
110         scanf("%d", &length0);
111         CreateList_L(&L1, length0);
112         printf("请选择 : \n");
113         printf("1 : 插入元素\n");
114         printf("2 : 删除元素\n");
115         printf("3 : 合并链表\n");
116         scanf("%d", &selectn);
117         switch (selectn)
118         {
119         case 1 :
120             printf("请输入插入位置 :\n");
121             scanf("%d", &location);
122             printf("请输入要插入元素 :\n");
123             scanf("%d", &e);
124             if(ListInsert_L(L1, location, e) == OK )
125                 {   //逆序输出
126                     counti = 1;
127                     la = L1;
128                     printf("新链表的顺序为 :\n");
129                     while(la -> next)
130                     {
131                         la = la->next;
132                         if(counti++ % 10)
133                             printf("%-5d",la -> data);
134                         else
135                             printf("%-5d\n",la -> data);
136                     }
137                 }
138             else
139                 printf("插入异常\n");
140                 printf("\n");
141             break;
142         case 2 :
143             printf("请输入要删除的位置 :\n");
144             scanf("%d",&location);
145             if(ListDelete_L(L1, location, &e) == OK)
146             {
147                 printf("删除成功\n被删除元素为 : %-5d\n", e);
148             }
149             else
150                 printf("删除异常\n");
151             break;
152         case 3 :
153             printf("请输入链表2 :\n");
154             printf("请输入链表长度 :\n");
155             scanf("%d", &length1);
156             CreateList_L(&L2, length1);
157             MergeList_L(L1, L2, &L3);
158             L2 = NULL;
159             lc = L3 -> next;
160             printf("新链表顺序为 :\n");
161             counti = 1;
162             while(lc)
163             {    if(counti++ % 10)
164                     printf("%-5d", lc -> data);
165                 else
166                     printf("%-5d", lc -> data);
167                 lc = lc -> next;
168             }
169             printf("\n");
170             break;
171         default :
172             printf("ERROR\n");
173             break;
174         }
175         Free(L1);
176         L1    = NULL;
177         L3    = NULL;
178         system("pause");
179         return 0;
180 }
时间: 2024-08-16 02:39:26

Data_Struct(LinkList)的相关文章

LinkList

LinkList:java中jdk1.6之后java.util.LinkList 类中对分装链表的理解: 参考:http://www.cnblogs.com/lintong/p/4374292.html 第一部分:熟悉LinkList中哪些方法和其构造;第二部分熟悉使用JDK中LinkList的API接口 第1部分:LinkList 的介绍: linklist:是一个双向链表,当做堆栈,队列或者双端队列进行操作:当做stack时候只能用push,pop,peek方法:当做队列时候用 add,re

链表中LinkList L与LinkList *L 借鉴

链表中LinkList L与LinkList *L的区别以及(*L).elem,L.elem L->next,(*L)->next的区别typedef struct Node{int elem;struct node * next;}node,*LinkList; 对于LinkList L: L是指向定义的node结构体的指针,可以用->运算符来访问结构体成员,即L->elem,而(*L)就是个Node型的结构体了,可以用点运算符访问该结构体成员,即(*L).elem; 对于Lin

转——链表中LinkList L与LinkList *L的区别

typedef struct Node{ int elem; struct node * next; }node,*LinkList; 对于LinkList L: L是指向定义的node结构体的指针,可以用->运算符来访问结构体成员,即L->elem,而(*L)就是个Node型的结构体了,可以用点运算符访问该结构体成员,即(*L).elem; 对于LinkList *L:L是指向定义的Node结构体指针的指针,所以(*L)是指向Node结构体的指针,可以用->运算符来访问结构体成员,即(

002 -- Circle LinkList 2 -- connect 2 circle link lists

The operation fullfilled codes: // A, B is the rear for 2 LinkList with nodes LinkList Connect(LinkList A,LinkList B) { p = A->next; // use p to store A list head A->next = B->next->next; // A change to point to b1 as the next. free(B->next

1.使用C++封装一个链表类LinkList

 使用C++封装一个链表类LinkList.写出相应一个测试用例 链表需要提供 添加 修改删除 除重 合并 排序创建 销毁等接口. 不能调用库函数或者使用STL等类库 题目延伸***********逆置链表********** LinkNode.h #ifndef LINKNODE_H #define LINKNODE_H #include <iostream> class LinkNode { public: int m_idata; LinkNode* m_pnext; }; #end

Java集合篇二:LinkList

package com.test.collection; /** * 自定义实现LinkList * * 1.双向链表 * 实现原理:底层封装Node节点对象(每个节点存放有3部分内容:1.上个节点的位置,2.当前节点内容,3.下一个节点的位置) * * 2.查询 * LinkList 相较 ArrayList 查询效率低: * 由于LinkList底层存放元素的不是数组,不能直接通过索引进行获取,需要从头或者从尾逐一遍历索引节点对象. * ArrayList直接通过索引获取即可. * * 3.

java学习第15天(Linklist Vector)

根据集合的分类(上一天有说),首先接触的是ArrayList但是和Collection一样,他没有什么特殊的功能,直接跳过,然后是Vector. 一   Vector A:有特有功能 a:添加 public void addElement(E obj) -- add() b:获取 public E elementAt(int index) -- get() 主要用的是第二个获取.举个例子. 简化书写下: v.addElement("hello"); v.addElement("

002 -- Circle LinkList 3 -- Puzzels_Magic Poker and Latin

000--Magic Poker Put the poker with specific sequence, so that they can show as below: count 1, open the first card as A, count 2, the first one place to bottom and open the second one as 2; for 3, the first 2 cards put to the bottom, and open the th

string和stringbuffer的区别 集合的作用 ArrayList vector linklist hashmap hashtable collection和collections

string给定的长度 不可变,当多个字符串联合的时候先转化为stringbuffer然后联合,速度慢,stringbuffer可以改变字符串的长度,当多个字符串连接的时候采用stringbuffer效率比较高. 集合的作用 对数据进行传送,对数据进行增删改查,还可以用来存放不同的对象. import java.util.Vector;import java.util.List;import java.util.Iterator;import java.util.Enumeration; /**