1. 程式人生 > >單鏈表的簡單操作(c/c++/python)

單鏈表的簡單操作(c/c++/python)

C-----------------------------------

#include <stdio.h>
#include <stdlib.h>
typedef struct list{
	int data;	//資料域 
	struct list *next;	//指標域 
}list;

//初始化連結串列
int initList(list **head);

//隊尾新增一個元素
int appendElem(list **head, int e);

//列印連結串列 
void printList(list *L); 

//獲得一個元素
int getElem(list *head, int i, int *e);

//查詢連結串列中元素值為e的節點是否存在 
int searchElem(list *head, int e); 

//新增一個元素 
int addElem(list **head, int i, int e);

//刪除一個元素 
int deleteElem(list **head, int i, int *e);

//返回單鏈表的長度
int listLength(list *head){
	return head->data;
} 

int main(){
	list *head;	//宣告一個頭節點
	if(!initList(&head)){
		printf("初始化連結串列失敗\n");
	}else{
		//第一個元素下表為0 
		appendElem(&head, 1);
		appendElem(&head, 4);	
		addElem(&head, 0,-2);
		printList(head);
		int e;
		deleteElem(&head, 2, &e);
		printf("e:%d\n", e);
		printList(head);
		getElem(head, 1, &e);
		printf("e:%d\n", e);
		printf("下標:%d\n", searchElem(head, -2));
	}
	return 0;
}

int initList(list **head){
	(*head) = (list *)malloc(sizeof(list));	//頭結點分配儲存空間 
	if(!(*head)) return 0;
	(*head)->data = 0;	//資料域 
	(*head)->next = NULL;
	return 1;
}

int appendElem(list **head, int e){
	list *node = (list *)malloc(sizeof(list));	//生成一個新的節點
	if(!node) return 0;
	node->data = e;
	node->next = NULL; 
	list *p;	//遍歷整個連結串列,一直到結尾
	p = (*head);
	p->data += 1;
	while(p->next != NULL){
		p = p->next;
	} 
	node->next = p->next;
	p->next = node;
	return 1;
}

void printList(list *L){
	list *p = L;
	while(p != NULL){
		printf("%-3d", p->data);
		p = p->next;
	}
	printf("\n"); 
}

int getElem(list *head, int i, int *e){
	list *p = head->next;	//不計算頭節點
	//第i個元素是否存在 
	while(i--&&p != NULL){	 
		p = p->next;
	}
	//p!=NULL存在 
	if(p != NULL){
		*e = p->data;
		return 1;
	}else{ //p=NULL不存在 
		return 0;
	}
}
int searchElem(list *head, int e){
	list *p = head->next;
	//查詢元素
	int i = 0; 
	while(p != NULL && p->data != e){
		p = p->next;
		i++;
	}
	//一直查詢到尾也沒有找到,返回-1 
	if(p == NULL){
		return -1;
	}else{	//找到返回該元素的下標 
		return i;
	}
}
int addElem(list **head, int i, int e){
	list *p = *head;
	//找到第i個節點前面的節點,如果i超過節點數,就把節點放置到最後一個 
	while(i-- && p->next != NULL){
		p = p->next; 
	}
	list *node = (list *)malloc(sizeof(list));
	if(!node) return 0;
	node->data = e;
	//下面幾步的順序是不能輕易改變的,有邏輯關係的
	node->next = p->next;
	p->next = node;
	(*head)->data++;
	return 1; 
} 
int deleteElem(list **head, int i, int *e){
	list *p = *head;
	if(i >= p->data){
		printf("索引超出範圍\n");
		return 0;
	}
	//獲得被刪除元素前面一個元素 
	while(i--){
		p = p->next;
	}
	//該元素前面的元素指標指向該元素後面的元素
	list *delnode = p->next;
	*e = delnode->data;
	p->next = delnode->next;
	free(delnode);
	(*head)->data--;
	return 1;
}

c++------------------------------------------------------------

#include <iostream>
#include <stdio.h>
#include <cstdlib>
using namespace std;
typedef struct Node{
	int data;	//資料域 
	struct Node *next;	//指標域 
}Node;
class myList{
	private:
		Node *head;
	public:
		//初始化連結串列
		int initList();
		
		//隊尾新增一個元素
		int appendElem(int e);
		
		//列印連結串列 
		void printList(); 
		
		//獲得一個元素
		int getElem(int i, int *e);
		
		//查詢連結串列中元素值為e的節點是否存在 
		int searchElem(int e); 
		
		//新增一個元素 
		int addElem(int i, int e);
		
		//刪除一個元素 
		int deleteElem(int i, int *e);
		
		//返回單鏈表的長度
		int listLength(){
			return head->data;
		} 
};
int myList::initList(){
	head = new Node;
	if(!head) return 0;
	head->next = NULL;
	head->data = 0;
	return 1;	
}
int myList::appendElem(int e){
	Node *p = head;
	while(p->next != NULL){
		p = p->next;
	}
	Node *node = new Node;
	if(!node) return 0;
	node->data = e;
	node->next = NULL;
	p->next = node;
	head->data++;
	return 1;
}
void myList::printList(){
	Node *p = head;
	while(p != NULL){
		printf("%-3d", p->data);
		p = p->next;
	}
}
int myList::getElem(int i, int *e){
	Node *p = head->next;
	if(i >= head->data){	//下標在0開始 
		return 0;
	}
	while(i-- && p != NULL){
		p = p->next;
	}
	*e = p->data;
	return 1;
}
int myList::searchElem(int e){
	Node *p = head->next;
	int i = 0;	//下標索引 
	while(p != NULL && p->data != e){
		p = p->next;
		i++;
	}
	if(p == NULL){
		return -1;
	}else{
		return 0;
	}
}
int myList::addElem(int i, int e){
	if(i > head->data){
		return 0;
	}
	Node *p = head;
	while(i-- && p!= NULL){
		p = p->next;
	}
	Node *node = new Node;
	node->data = e;
	node->next = p->next;
	p->next = node;
	head->data++;
	return 1;
}
int myList::deleteElem(int i, int *e){
	if(i > head->data){
		return 0;
	}
	Node *p = head;
	while(i-- && p!= NULL){
		p = p->next;
	}
	Node *delnode = p->next;
	*e = delnode->data;
	p->next = delnode->next;
	delete delnode;
	head->data--;
	return 1;
}
int main(){
	//下標從0開始 
	myList List;
	List.initList();
	List.appendElem(1);
	List.appendElem(4);
	List.addElem(0, -2);
	List.printList();
	int e;
	List.deleteElem(2, &e);
	printf("e:%d\n", e);
	List.printList();
	List.getElem(1, &e);
	printf("e:%d\n", e);
	printf("下標:%d\n", List.searchElem(-2));
	return 0;
}

python---------------------------------------------------

class Node():
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

def initList():
    head = Node(0)
    return head

def appendElem(head:Node, e:int):
    p = head
    while p.next != None:
        p = p.next
    node = Node(e)
    p.next = node
    head.value += 1

def printList(head:Node):
    p = head
    while p != None:
        print("%d "%p.value, end="")
        p = p.next

def getElem(head:Node, i:int):
    p = head.next
    while i != 0:
        i -= 1
        p = p.next
    if p != None:
        return p.value
    else:
        return "error"

def searchElem(head:Node, e:int):
    p = head.next
    i = 0
    while p != None:
        if p.value == e:
            return i
        i += 1
    return -1

def addElem(head:Node, i:int, e:int):
    if i > head.value:
        return "error"
    p = head
    while i !=0:
        p = p.next
        i -= 1
    node = Node(e)
    node.next = p.next
    p.next = node
    head.value += 1

def deleteElem(head:Node, i:int):
    if i > head.value:
        return "error"
    p = head
    while i !=0:
        p = p.next
        i -= 1
    delnode = p.next
    e = delnode.value
    p.next = delnode.next
    head.value -= 1
    return e

if __name__ == '__main__':
    # 不算頭節點的第一個節點下標為1
    head = initList()
    appendElem(head, 1); appendElem(head,4); addElem(head, 0,-2)
    printList(head)
    e = deleteElem(head, 2)
    print("e的值:%d"%e)
    printList(head)
    e = getElem(head, 1)
    print("下標為1的值:%d" % e)
    print("下標:%d\n"%searchElem(head, -2))