带表头的单向链表实现(C语言)
2010-01-30 13:55
495 查看
带表头的单向链表实现(C语言)在vs 2008上测试编译并成功执行
头文件linkedlist.h
源文件LinkedList.cpp
运行结果:
Add element "1" successfully.
Add element "2" successfully.
2 1
Element "3" successfully inserted into the list at 2.
2 3 1
The need element is:3
Element removed.
2 1
Destoryed the list.
头文件linkedlist.h
#ifndef _LINKEDLIST_H #define _LINKEDLIST_H typedef int DataType; typedef struct Node { DataType data; struct Node *next; }Node, *PNode; PNode create(); void addElement(PNode head, DataType data); void removeElement(PNode head, int index); DataType elementAt(PNode head, int index); void show(PNode head); void insert(PNode head, int index, DataType data); void removeAll(PNode head); void destroy(PNode head); #endif
源文件LinkedList.cpp
#include <stdio.h> #include <stdlib.h> #include "linkedlist.h" //create a new node PNode create() { PNode node = (PNode)malloc(sizeof(Node)); if(NULL == node) { printf("Allocate memory failed."); return NULL; } node->next = NULL; return node; } //add a new element to the first position of the list void addElement(PNode head, DataType data) { PNode node = create(); if(NULL != node) { node->data = data; node->next = head->next; head->next = node; printf("Add element /"%d/" successfully./n", data); } else printf("Add element failed./n"); } //display all the elements in the list void show(PNode head) { PNode p = head->next; while(NULL != p) { printf("%d ", p->data); p = p->next; } } //remove all elements void removeAll(PNode head) { PNode p = head->next; while(NULL != p) { head->next = p->next; free(p); p = head->next; } printf("All elements are removed./n"); } //destroy the list void destroy(PNode head) { PNode p = head->next; while(NULL != p) { head->next = p->next; free(p); p = head->next; } free(head); printf("Destoryed the list./n"); } //remove the element at index void removeElement(PNode head, int index) { PNode p = head; PNode q = head->next; int count = 1; while(NULL != q) { if(index == count) { p->next = q->next; free(q); q = p->next; printf("Element removed./n"); break; } p = q; q = q->next; count++; } } //get element at index DataType elementAt(PNode head, int index) { PNode p = head->next; int count = 1; while(NULL != p) { if(index == count) return p->data; p = p->next; count++; } return -1; } void insert(PNode head, int index, DataType data) { PNode p = head; int count = 1; while(NULL != p) { if(index == count) { PNode node = create(); node->data = data; node->next = p->next; p->next = node; printf("Element /"%d/" successfully inserted into the list at %d./n",data, index); break; } p = p->next; count++; } } int main() { PNode head = create(); addElement(head, 1); addElement(head, 2); show(head); printf("/n"); insert(head, 2, 3); show(head); printf("/n"); printf("The need element is:%d/n",elementAt(head,2)); removeElement(head, 2); show(head); printf("/n"); destroy(head); return 0; }
运行结果:
Add element "1" successfully.
Add element "2" successfully.
2 1
Element "3" successfully inserted into the list at 2.
2 3 1
The need element is:3
Element removed.
2 1
Destoryed the list.
相关文章推荐
- C语言实现链表之单向链表(三)创建链表
- C语言实现链表之单向链表(十五)测试用例
- 单向链表之释放(C语言实现)
- 单向链表之排序(C语言实现)
- list.c - A linked list by C --- C语言实现的单向链表
- 单向链表的C语言实现与基本操作
- C语言实现,无头结点不带环的单向链表的基本操作
- C语言实现一个简单的单向链表list
- C语言实现链表之单向链表(一)头文件
- C语言实现链表之单向链表(二)结点内存申请及数据初始化
- C语言实现链表之单向链表(四)清空链表
- C语言实现链表之单向链表(十)删除任意结点
- C语言实现链表之单向链表(十四)链表打印和排序
- 单向链表的实现(c语言)
- C语言单向动态链表程序,实现链表的建立,合并,重新排序,链表元素的插入与删除,以及根据元素成员的值进行元素删除。
- 单向链表之有序插入节点(C语言实现)
- 用链表实现的栈(单向链表,表头插入和删除)
- list.c - A linked list by C --- C语言实现的单向链表
- C语言实现单向链表
- 单向链表装载用户自定义数据的c语言实现 linklistpackagenode.h 和 linklistpackagenode.c