★线性表是一个序列(线性结构),具有一定的顺序
★如果有多个元素,第一个元素没有前驱,最后一个元素没有后继
★线性表强调是有限的
㈠.顺序表
——把线性表的结点按逻辑顺序依次存放在一组地址连续的存储单元里,用这种方法存储的线性表简称顺序表
——在顺序表中,线性表的逻辑顺序与物理顺序一致
——在顺序表中,数据元素之间的关系是以元素在计算机内“物理位置相邻”来体现
㈡.单链表
——把线性表的元素任意存放在存储单元结点中,节点之间按照逻辑顺序通过一个个指针依次链接起来,用这种方式存储的线性表简称单链表
——在单链表中,存储单元结点可以是连续的,也可以是不连续的,甚至是零散分布在内存中任意位置上的
——在单链表中,线性表的逻辑顺序与物理顺序不一定相同
——在单链表中,数据元素之间的关系是通过节点间的指针来实现的
1)不带头结点的单链表
2)带头结点的单链表
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;
}
四. 编译结果
原文:https://www.cnblogs.com/miaowulj/p/12235972.html