★ linear list is a sequence (linear structure), having a certain order
★ If there are multiple elements, the first element has no predecessor, successor is not the last element
★ emphasized linear table is limited
One. The basic structure of the table stored in linear
(I) The order of the table
- the table node linear order of the logical addresses stored in a set of contiguous memory locations, the table referred to as a linear sequence table stored in this way
- in the order table, the logical order of the linear form consistent with the physical order
- in the order table, the relationship between the data elements of the element's "physical location adjacent," embodied in the computer
. (Ii) The single list
- the linear table element is stored in the storage unit of any node, the node between the logical sequence by a linked pointers sequentially, using this linear table stored short single linked list
- in a single linked list , the node storage unit may be continuous or may be discontinuous, even scattered anywhere in memory on the
- in the single linked list, the linear form of the logical order and not necessarily the same physical order
- in single list, the relationship between data elements is achieved by a pointer between nodes
1) does not lead to a single linked list node
2) single chain First Node
II. Abstract data type definitions
struct list;
list_init(); free(struct list list);
clear(struct list list); isempty(struct list list);
count(struct list list); add(struct list list,int index,int e);
remove(struct list list,int index); get(struct list list,int index);
set(struct list list,int index,int e); lookup(struct list *list,int e);
三.代码实现
list.h
#ifndef __LIST_H__
#define __LIST_H__
struct list;
struct list *list_init();
void list_free(struct list *list);
void list_clear(struct list *list);
int list_isempty(struct list *list);
int list_count(struct list *list);
void list_add(struct list *list,int index,int e);
int list_remove(struct list *list,int index);
int list_get(struct list *list,int index);
void list_set(struct list *list,int index,int e);
int list_lookup(struct list *list,int e);
#endif
main.c
#include <stdlib.h>
#include <stdlib.h>
include <list.h>
int main(){
struct list *lista=NULL;
struct list *listb=NULL;
lista=list_init();
list_add(lista,0,0);
list_add(lista,0,2);
list_add(lista,0,5);
listb=list_init();
list_add(listb,0,2);
list_add(list,0,1314);
list_add(listb,0,5);
int i,j,flag;
for(i=0;i<list_count(listb);j++){
falg=1;
for(j=0;i<list_count(lista);j++){
if(list_get(listb,i)==list_get(lista,j)){
flag=0;
break;
}
}
if(flag==1){
list_add(lista,list_count(lista),list_get(listb,i));
}
}
for(i=0;i<list_count(lista);i++){
printf(“%d\n”,list_get(lista,i));
}
list_free(lista);
list_free(listb);
return 0;
}
list.c
顺序表
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "list.h"
#define LIST_INIT_SIZE 1
#define LIST_INCR_SIZE 1
struct list{
int *bottom;
int count;
int size;
};
struct list *list_init()
{
struct list *list=NULL;
list=(struct list *)malloc(sizeof(struct list));
if(list==NULL) return NULL;
assert(list!=NULL);
list->bottom=NULL;
list->count=0;
list->size=0;
list->bottom=(int *)malloc(sizeof(int)*LIST_INIT_SIZE);
if(list->bottom==NULL){
free(list);
return NULL;
}
assert(list->bottom!=NULL);
memset(list->bottom,0,sizeof(int)*LIST_INIT_SIZE);
list->size=LIST_INIT_SIZE;
return list;
}
void list_free(struct list *list)
{
assert(list->bottom!=NULL);
assert(list!=NULL);
free(list->bottom);
free(list);
}
void list_clear(struct list *list)
{
assert(list->bottom!=NULL);
assert(list!=NULL);
list->count=0;
}
int list_isempty(struct list *list)
{
assert(list->bottom!=NULL);
assert(list!=NULL);
return (list->count==0);
}
int list_count(struct list *list)
{
assert(list->bottom!=NULL);
assert(list!=NULL);
return list->count;
}
void list_add(struct list *list,int index,int e)
{
assert(list->bottom!=NULL);
assert(list!=NULL);
if(list->count==list->size){
list->bottom=(int *)realloc(list->bottom,sizeof(int)*(list->size+LIST_INCR_SIZE));
}
list->size+=LIST_INCR_SIZE;
int i;
for(i=list->count-1;i>index-1;i--){
list->bottom[i+1]=list->bottom[i];
}
list->bottom[index]=e;
list->count++;
return;
}
int list_remove(struct list *list,int index)
{
assert(list->bottom!=NULL);
assert(list!=NULL);
assert(index>=0&&index<list->count);
int i;
int result=-1;
result=list->bottom[index];
for(i=index;i<list->count-1;i++){
list->bottom[i]=list->bottom[i+1];
}
list->count--;
return result;
}
int list_get(struct list *list,int index)
{
assert(list->bottom!=NULL);
assert(list!=NULL);
assert(index>=0&&index<list->count);
int result=-1;
result=list->bottom[index];
return result;
}
void list_set(struct list *list,int index,int e)
{
assert(list->bottom!=NULL);
assert(list!=NULL);
assert(index>=0&&index<list->count);
list->bottom[index]=e;
return;
}
int list_lookup(struct list *list,int e)
{
int i;
for(i=0;i<list->count;i++){
if(list->bottom[i]==e){
return 1;
}
}
return 0;
}
单链表(不带头结点)
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "list.h"
struct list_node{
int data;
struct list_node *next;
};
struct list{
struct list_node *head;
int count;
};
struct list *list_init()
{
struct list *list=NULL;
list=(struct list *)malloc(sizeof(struct list));
if(list==NULL) return NULL;
assert(list!=NULL);
list->count=0;
list->head=NULL;
return list;
}
void list_free(struct list *list)
{
assert(list!=NULL);
struct list_node *p=NULL;
while(list->head!=NULL){
p=list->head;
list->head=p->next;
free(p);
}
assert(list->head==NULL);
free(list);
}
void list_clear(struct list *list)
{
assert(list!=NULL);
struct list_node *p=NULL;
while(list&&list->head!=NULL){
p=list->head;
list->head=p->next;
free(p);
}
assert(list->head==NULL);
list->count=0;
}
int list_isempty(struct list *list)
{
assert(list!=NULL);
return (list->count==0);
}
int list_count(struct list *list)
{
assert(list!=NULL);
return list->count;
}
void list_add(struct list *list,int index,int e)
{
assert(list!=NULL);
assert(index>=0&&index<=list->count);
int i;
struct list_node *p=NULL;
struct list_node *node=NULL;
node=(struct list_node *)malloc(sizeof(struct list_node));
if(node==NULL){
perror("malloc node struct error!");
exit(0);
}
node->data=e;
node->next=NULL;
if(index==0){
node->next=list->head;
list->head=node;
list->count++;
return;
}
p=list->head;
for(i=0;i<index-1;i++){
p=p->next;
}
node->next=p->next;
p->next=node;
list->count++;
return;
}
int list_remove(struct list *list,int index)
{
assert(list!=NULL);
assert(index>=0&&index<list->count);
int i;
int result=-1;
struct list_node *p=NULL;
struct list_node *node=NULL;
if(index==0){
node=list->head;
list->head=node->next;
result=node->data;
list->count--;
return result;
}
p=list->head;
for(i=0;i<index-1;i++){
p=p->next;
}
node=p->next;
p->next=node->next;
result=node->data;
list->count--;
return result;
}
void list_set(struct list *list,int index,int e)
{
assert(list!=NULL);
assert(index>=0&&index<list->count);
int i;
struct list_node *p=NULL;
p=list->head;
for(i=0;i<index;i++){
p=p->next;
}
p->data=e;
return;
}
int list_get(struct list *list,int index)
{
assert(list!=NULL);
assert(index>=0&&index<list->count);
struct list_node *p=NULL;
p=list->head;
int i,result=-1;
for(i=0;i<index;i++){
p=p->next;
}
result=p->data;
return result;
}
int list_lookup(struct list *list,int e)
{
assert(list!=NULL);
int i;
struct list_node *p=NULL;
p=list->head;
for(i=0;i<list->count;i++){
if(p->data==e){
return 1;
break;
}
p=p->next;
}
return 0;
}
单链表(带头结点)
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "list.h"
struct list_node{
int data;
struct list_node *next;
};
struct list{
struct list_node *head;
int count;
};
struct list *list_init()
{
struct list *list=NULL;
list=(struct list *)malloc(sizeof(struct list));
if(list==NULL) return NULL;
assert(list!=NULL);
list->head=NULL;
list->count=0;
list->head=(struct list_node *)malloc(sizeof(struct list_node));
if(list->head==NULL){
free(list);
return NULL;
}
assert(list->head!=NULL);
list->head->data=-1;
list->head->next=NULL;
return list;
}
void list_free(struct list *list)
{
assert(list->head!=NULL);
assert(list!=NULL);
struct list_node *p=NULL;
while(list&&list->head!=NULL){
p=list->head;
list->head=p->next;
free(p);
}
assert(list->head==NULL);
free(list);
return;
}
void list_clear(struct list *list)
{
assert(list->head!=NULL);
assert(list!=NULL);
struct list_node *p=NULL;
while(list->head->next!=NULL){
p=list->head->next;
list->head->next=p->next;
free(p);
}
assert(list->head->next==NULL);
list->count=0;
}
int list_isempty(struct list *list)
{
assert(list->head!=NULL);
assert(list!=NULL);
return (list->count==0);
}
int list_count(struct list *list)
{
assert(list->head!=NULL);
assert(list!=NULL);
return list->count;
}
void list_add(struct list *list,int index,int e)
{
assert(list->head!=NULL);
assert(list!=NULL);
assert(index>=0&&index<=list->count);
int i;
struct list_node *p=NULL;
struct list_node *node=NULL;
node=(struct list_node *)malloc(sizeof(struct list_node));
if(node==NULL){
perror("malloc node struct error!\n");
exit(1);
}
node->data=e;
node->next=NULL;
p=list->head;
for(i=-1;i<index-1;i++){
p=p->next;
}
node->next=p->next;
p->next=node;
list->count++;
return;
}
int list_remove(struct list *list,int index)
{
assert(list->head!=NULL);
assert(list!=NULL);
assert(index>=0&&index<list->count);
int i;
int result=-1;
struct list_node *p=NULL;
struct list_node *node=NULL;
p=list->head;
for(i=-1;i<index-1;i++){
p=p->next;
}
node=p->next;
p->next=node->next;
list->count--;
result=node->data;
return result;
}
void list_set(struct list *list,int index,int e)
{
assert(list->head!=NULL);
assert(list!=NULL);
struct list_node *p=NULL;
int i;
p=list->head;
for(i=-1;i<index;i++){
p=p->next;
}
p->data=e;
return;
}
int list_get(struct list *list,int index)
{
assert(list->head!=NULL);
assert(list!=NULL);
struct list_node *p=NULL;
int result=-1;
int i;
p=list->head;
for(i=-1;i<index;i++){
p=p->next;
}
result=p->data;
return result;
}
int list_lookup(struct list *list,int e)
{
assert(list->head!=NULL);
assert(list!=NULL);
struct list_node *p=NULL;
int i;
p=list->head;
for(i=-1;i<list->count-1;i++){
p=p->next;
if(list->head->data==e){
return 1;
break;
}
}
return 0;
}
四. 编译结果