Skip to content

Commit 7f5308d

Browse files
committed
Add my linklist and test it
1 parent b7ca39e commit 7f5308d

File tree

3 files changed

+282
-0
lines changed

3 files changed

+282
-0
lines changed

myalg/Makefile

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#----------------options----------------#
2+
#environment
3+
CC = g++
4+
FLAGS_DEBUG = -g
5+
FLAGS_COMPILE = -c -Wall -Wextra -Werror
6+
7+
#directory
8+
SHELL = /bin/sh
9+
DIR = $(PWD)
10+
DIR_OBJ = $(DIR)/obj/
11+
DIR_SRC = $(DIR)/
12+
DIR_BIN = $(DIR)/bin/
13+
DIR_LIB = $(DIR)/lib/
14+
DIR_INC = $(DIR)/include/
15+
16+
#target
17+
TARGET = test
18+
19+
#vpath
20+
#vpath %.cpp $(DIR_SRC)
21+
#vpath %.o $(DIR_OBJ)
22+
23+
#src
24+
SRC=$(wildcard ${DIR_SRC}*.cpp)
25+
26+
#include
27+
INC=-I$(DIR_INC)
28+
29+
#lib
30+
#LIBS depends on your needs
31+
#for instance: LIBS=-lsky which means there is a libsky.a file in DIR_LIB direction
32+
LIBS=
33+
LIB=-L$(DIR_LIB)$(LIBS)
34+
35+
#obj
36+
OBJ=$(patsubst %.cpp,${DIR_OBJ}%.o,$(notdir ${SRC}))
37+
38+
#----------------macro---------------#
39+
.PHONY:dir clean make
40+
#debug
41+
debug:$(OBJ)
42+
@echo "linking..."
43+
@cd $(DIR_OBJ);$(CC) *.o -o $(DIR_BIN)$(TARGET)
44+
@echo "linking end..."
45+
@echo "execute the $(TARGET) in $(DIR_BIN)$(TARGET)"
46+
47+
$(DIR_OBJ)%.o:$(DIR_SRC)%.cpp
48+
@echo "compiling..."
49+
@echo "SRC:$<"
50+
$(CC) $(FLAGS_DEBUG) $(FLAGS_COMPILE) $< -o $@ $(INC)
51+
@echo "compiling end..."
52+
53+
#clean
54+
clean:
55+
@rm -rf $(DIR_OBJ)
56+
@rm -rf $(DIR_BIN)
57+
58+
#objects & executable direction
59+
dir:
60+
@mkdir -p $(DIR_OBJ)
61+
@mkdir -p $(DIR_BIN)
62+
63+
#help
64+
help:
65+
@echo "......------------Makefile------------......"
66+
@echo "Include four commands: #1.de; #2.clean; #3.dir; #4.help"
67+
@echo "#1.de:"
68+
@echo " debug the source file into a executable file that is included in the $(DIR_BIN)!"
69+
@echo "#2.clean:"
70+
@echo " delete the debug files and directions"
71+
@echo "#3.dir:"
72+
@echo " mkdir a new directions for objects and executable file:"
73+
@echo " $(DIR_BIN) for executable file"
74+
@echo " $(DIR_OBJ) for object file"
75+
@echo "#4.help:"
76+
@echo " help for using this makefile to debug a project which includes source files include files"
77+
@echo "......------------End------------......"
78+
79+
#make all the commands and get the executable file
80+
make:
81+
@echo "start debugging..."
82+
@$(MAKE) -s dir
83+
@$(MAKE) -s debug
84+
@echo "finish..."
85+
@echo "******************************"
86+
@echo "* ____ ____ _ ________*"
87+
@echo "* / __ \/ __ \/ | / / ____/*"
88+
@echo "* / / / / / / / |/ / __/ *"
89+
@echo "* / /_/ / /_/ / /| / /___ *"
90+
@echo "*/_____/\____/_/ |_/_____/ *"
91+
@echo "******************************"
92+
@echo "You want the executable file run itself?"
93+
@echo "'Y' for yes,'N' for no"
94+
@read -s -n1 key;\
95+
Y="Y";\
96+
y="y";\
97+
if [ $$key == $$Y ] || [ $$key == $$y ]; then\
98+
cd $(DIR_BIN);\
99+
$(DIR_BIN)$(TARGET);\
100+
exit 0;\
101+
else\
102+
echo "Not Execute the File!";\
103+
fi
104+
@echo "done"
105+

myalg/include/linklist.h

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
# .---. .-----------
3+
# / \ __ / ------
4+
# / / \( )/ ----- (`-') _ _(`-') <-. (`-')_
5+
# ////// '\/ ` --- ( OO).-/( (OO ).-> .-> \( OO) ) .->
6+
# //// / // : : --- (,------. \ .'_ (`-')----. ,--./ ,--/ ,--.' ,-.
7+
# // / / / `\/ '-- | .---' '`'-..__)( OO).-. ' | \ | | (`-')'.' /
8+
# // //..\\ (| '--. | | ' |( _) | | | | . '| |)(OO \ /
9+
# ============UU====UU==== | .--' | | / : \| |)| | | |\ | | / /)
10+
# '//||\\` | `---. | '-' / ' '-' ' | | \ | `-/ /`
11+
# ''`` `------' `------' `-----' `--' `--' `--'
12+
# ########################################################################################################
13+
#
14+
# Author: edony - edonyzpc@gmail.com
15+
#
16+
# twitter : @edonyzpc
17+
#
18+
# Last modified: 2014-11-28 15:25
19+
#
20+
# Filename: linklist.h
21+
#
22+
# Description: All Rights Are Reserved
23+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
24+
#ifndef __LINKLIST_H__
25+
#define __LINKLIST_H__
26+
#include <stdlib.h>
27+
namespace myalg
28+
{
29+
template <typename T = int>
30+
//define the list member
31+
struct l_mem
32+
{
33+
T mem_data;
34+
int position;
35+
l_mem *next;
36+
};
37+
38+
template <typename T1 = int>
39+
//define the linklist class
40+
class linklist
41+
{
42+
private:
43+
//sieze of the list
44+
int size;
45+
46+
//pointer pointer to member of the list
47+
l_mem<T1> *head;
48+
l_mem<T1> *tail;
49+
50+
public:
51+
//default constructor
52+
linklist()
53+
{
54+
this->size = 0;
55+
this->head = new l_mem<T1>;
56+
head->position = 0;
57+
this->tail = new l_mem<T1>;
58+
head->position = this->size;
59+
}
60+
61+
//copy constructor
62+
linklist(const linklist& other_list)
63+
{
64+
this->size += other_list.getsize();
65+
this->head = new l_mem<T1>;
66+
//this->head = (l_mem*)malloc(sizeof *head);
67+
*head = *(other_list.head);
68+
this->tail = new l_mem<T1>;
69+
//this->tail = (l_mem*)malloc(sizeof *tail);
70+
*tail = *(other_list.tail);
71+
this->size = other_list.getsize();
72+
}
73+
74+
//destructor
75+
~linklist()
76+
{
77+
delete head;
78+
//free(head);
79+
delete tail;
80+
//free(head);
81+
}
82+
83+
//insert the next member after the member ins_pos,
84+
//-1 for the failing to insert and 0 for successing to insert
85+
int linklist_ins_next(l_mem<T1> *ins_pos, T1 ins_data)
86+
{
87+
l_mem<T1> *ins = new l_mem<T1>;
88+
if(ins == NULL) return -1;
89+
ins->mem_data = ins_data;
90+
91+
ins->mem_data = ins_data;
92+
if(ins_pos == NULL)//insert at the head for default
93+
{
94+
if(this->getsize() == 0)
95+
{
96+
this->tail = ins;
97+
}
98+
ins->next = this->head;
99+
this->head = ins;
100+
ins->position = this->head + 1;
101+
this->size++;
102+
this->tail = this->size;
103+
}
104+
else
105+
{
106+
ins->next = ins_pos->next;
107+
ins_pos->next = ins;
108+
if(ins->next == NULL)
109+
{
110+
this->tail = ins;
111+
}
112+
ins->position = ins_pos->position + 1;
113+
this->size++;
114+
this->tail = this->size;
115+
}
116+
return 0;
117+
118+
}
119+
120+
//remove the member
121+
int rm_next(l_mem<T1> *rm_pos)
122+
{
123+
if(rm_pos == NULL || rm_pos->next ==NULL) return -1;//error removement
124+
125+
rm_pos->next = rm_pos->next->next;
126+
this->size--;
127+
l_mem<T1> * tmp = new l_mem<T1>;
128+
tmp = rm_pos->next;
129+
while(tmp != NULL)
130+
{
131+
tmp->position--;
132+
tmp = tmp->next;
133+
}
134+
delete tmp;
135+
return 0;//finish removement
136+
}
137+
138+
//find the mem
139+
int find_mem(T1 find_data, l_mem<T1> * pop_mem)
140+
{
141+
l_mem<T1> * tmp = new l_mem<T1>;
142+
if(tmp == NULL) return -1;
143+
tmp = this->head->next;
144+
while(tmp != NULL)
145+
{
146+
if(tmp->mem_data != find_data)
147+
{
148+
tmp = tmp->next;
149+
}
150+
else
151+
{
152+
pop_mem = tmp;
153+
return 0;
154+
}
155+
}
156+
}
157+
158+
inline int getsize(){return size;}
159+
};
160+
}
161+
#endif
162+

myalg/test_alg.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include "linklist.h"
3+
using namespace std;
4+
using alg::linklist;
5+
6+
int main()
7+
{
8+
linklist * mylist = new linklist;
9+
l_mem<int> * mem1 = new l_mem<int>;
10+
int a = 10;
11+
mylist->linklist->ins->next(a);
12+
mylist->find_mem(a, mem1);
13+
int b =12;
14+
mylist->linklist->ins->next(mem1, b);
15+
}

0 commit comments

Comments
 (0)