用链表实现vector

来源:互联网 发布:相片合成软件下载 编辑:程序博客网 时间:2024/06/05 20:06

用链表实现vector的一些功能,部分代码如下,以后还要修改,现在先做一个修正

// keshan.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdio.h>#include <malloc.h>#define NULL 0#define LEN sizeof(struct Node)struct Node{int value;Node * next;};//创建单向链表Node * create_link(){Node *head = (Node *)malloc(LEN);head->next = NULL;head->value = 0;return head;}//末尾增加元素,相当于push_backvoid push_link(Node *head ,int key){Node * p  = head;while(p->next!=NULL){p = p->next;}p->next = (Node *)malloc(LEN);p = p->next;p->value = key;p->next = NULL;}//求链表的长度,相当于vector的lengthint length_link(Node * head){Node * p = head;int len = 1;while(p->next!=NULL){p = p->next;len++;}return len;}//查询链表,相当于vector中的atint at_link(Node * head,int index){Node * p = head;while(p->next!=NULL){}}//打印链表void print_link(Node *head,const char * str){printf("%s\n",str);Node *p = head;printf("%d\n",head->value);while(p->next!=NULL){p = p->next;printf("%d\n",p->value);}}//清空链表void clear_link(Node *head){}int _tmain(int argc, _TCHAR* argv[]){//创建Node * head = create_link();print_link(head,"创建的链表为:");//增加元素push_link(head,5);push_link(head,6);print_link(head,"增加两个元素后为:");//访问元素int len = length_link(head);printf("the length is %d\n",len);//清空//clear_link(head);//print_link(head,"清空之后的链表为");getchar();return 0;}


0 0
原创粉丝点击