请高手给一个用c语言求线性表的建立,插入,删除(不是c++)

nanshi07 2011-09-11 05:15:27
请高手给一个用c语言求线性表的建立,插入,删除(不是c++)。
谢谢了,在线等!
...全文
1595 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yxl00000000 2011-09-18
  • 打赏
  • 举报
回复
对这个真的好萌啊,。、
浩-哥 2011-09-17
  • 打赏
  • 举报
回复
自己照着书本敲、、、任意一本数据结构树上都有这些代码
nanshi07 2011-09-16
  • 打赏
  • 举报
回复
struct stu{
int data[10];
int length;
}stu1;

void jianli(stu *l,int n)
{
int i;
l->length=0;
for(i=0;i<10;i++)
scanf("%d",&l->data[i]);
l->length++;
}

int main()
{
jianli(stu *l,int m)
printf("%d",&l->data[i]);

return 0;
}

--------------------------------
我自己写的线性表建立的代码,有问题。请高手指点一下错误,应该怎么修改?
yosyg 2011-09-15
  • 打赏
  • 举报
回复
代码贴的好长啊
shupo 2011-09-15
  • 打赏
  • 举报
回复
最好还是自己手敲
fyswords 2011-09-15
  • 打赏
  • 举报
回复
作业题?最好还是自己手敲
  • 打赏
  • 举报
回复
找本谭浩强的C语言程序设计,里面有完整例子
孤独小剑 2011-09-15
  • 打赏
  • 举报
回复
一楼是通用的链表实现方法,对任何一种结构体都能实现链表。而且很少会出错了。
liwei3290 2011-09-13
  • 打赏
  • 举报
回复
楼上说的很详细了
qishengjie901026 2011-09-12
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <malloc.h> //Using malloc and realloc
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE true
#define FALSE false
#define ElemType int

typedef struct
{
ElemType *elem; //数据元素的基地址
int length; //线性表的当前长度
int listsize; //当前分配的存储容量
}SqList;

bool InitList_Sq (SqList &L)
{ //malloc和new的区别请看说明1
L.elem =(ElemType*) malloc(LIST_INIT_SIZE*sizeof(ElemType));//分配空间
//L.elem=new ElemType[LIST_INIT_SIZE]; //分配空间
if (!L.elem) return FALSE;
L.length=0; //将当前线性表长度置0
L.listsize=LIST_INIT_SIZE;
return TRUE;//成功返回OK
}

void DestroyList_Sq(SqList &L)
{
if (L.elem) free(L.elem); //释放线性表的存储空间
L.elem=NULL;
L.length=0;
L.listsize=0;
}


void ClearList_Sq (SqList &L) //清空线性表L
{
L.length=0; //将线性表的长度置为0
}

bool ListEmpty_Sq (SqList &L)//判断线性表L是否为空
{
if (L.length==0) return TRUE;
else return FALSE;
}

int ListLength_Sq (SqList &L)//求线性表L的长度
{
return (L.length);
}

ElemType GetElem_Sq (SqList &L, int i)//获取线性表L中的某个数据元素的内容
{
ElemType e;
if (i<0||i>L.length) return FALSE;//失败返回i值不合法;
e=L.elem[i];
return e;

}


int LocateElem_Sq (SqList &L, ElemType e)//在线性表L中检索值为e的第一个数据元素
{
for (int i=1; i<=L.length; ++i)
if (L.elem[i]==e) return i;
return FALSE;
}


bool ListInsert_Sq (SqList &L, int i, ElemType e)//在线性表L中下标为i的数据元素之前插入数据元素e
{

if (i<1||i>L.length+1)
{
cout<<"i值不合法"<<endl;//i值不合法
return FALSE;
}
if (L.length>=L.listsize)
{
L.elem=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));//realloc的返回值请看说明2
if(!L.elem) {cout<<"OVERFLOW"<<endl; return FALSE;}//存储分配失败
L.listsize+=LISTINCREMENT;//增加存储容量
}

for (int j=L.length; j>=i; --j)
L.elem[j+1] = L.elem[j];
L.elem[i]=e; ++L.length; return TRUE;
}


ElemType ListDelete_Sq(SqList &L, int i, ElemType e)// 将线性表L中第i个数据元素删除
{
if (ListEmpty_Sq(L)) return FALSE;
if (i<1 || i>L.length)
{
cout<<"i值不合法"<<endl;//i值不合法
return FALSE;
}
e=L.elem[i];
for (int j=i+1; j<=L.length; j++)
L.elem[j-1]=L.elem[j];
L.length--; return e;
}
//------------------算法2.1--------------------------------------
void union_Sq(SqList &La,SqList &Lb)
{
//说明
//算法的功能是将所有在线性表Lb中但不在La中的数据元素插入到La中
for(int i=1;i<=7;++i)
{
ElemType e=GetElem_Sq(Lb,i);
if(!LocateElem_Sq(La,e)) ListInsert_Sq(La,La.length+1,e);
}
}

//-----------------算法2.2--------------------------------
void MergeList_Sq(SqList &La,SqList &Lb,SqList &Lc)
{
int i=1;
int j=1;
int k=0;
ElemType e1,e2;
while(i<=La.length && j<=Lb.length)//La和Lb均非空
{

e1=GetElem_Sq(La,i);e2=GetElem_Sq(Lb,j);
if(e1<e2) {ListInsert_Sq(Lc,++k,e1);++i;}
else {ListInsert_Sq(Lc,++k,e2);++j;}
}
while(i<=La.length)
{e1=GetElem_Sq(La,i++);ListInsert_Sq(Lc,++k,e1);}//注意i
while(j<=Lb.length)
{e2=GetElem_Sq(Lb,j++);ListInsert_Sq(Lc,++k,e2);}


}

//---------------------------主函数-------------------------------------
void main()
{
SqList la,lb;
InitList_Sq(la);
InitList_Sq(lb);
//线性表La中的元素为[3,5,8,11],线性表Lb中的元素为[2,6,8,9,11,15,20]
ListInsert_Sq (la, 1, 3);ListInsert_Sq (la, 2, 5);ListInsert_Sq (la, 3, 8);ListInsert_Sq (la, 4, 11);
ListInsert_Sq (lb, 1, 2);ListInsert_Sq (lb, 2, 6);ListInsert_Sq (lb, 3, 8);ListInsert_Sq (lb, 4, 9);ListInsert_Sq (lb, 5, 11);ListInsert_Sq(lb, 6, 15);ListInsert_Sq(lb,7,20);

//------------------算法2.1的测验--------------------------------------
/* union_Sq(la,lb);
for(int i=1;i<=la.length;++i)
{
ElemType e=la.elem[i];
cout<<e<<endl;
}
*/

//------------------算法2.2的测验--------------------------------------
/* SqList lc;
InitList_Sq(lc);
MergeList_Sq(la,lb,lc);
for(int i=1;i<=lc.length;++i)
{
ElemType e=lc.elem[i];
cout<<e<<endl;
}
*/
}
这是严蔚敏《数据结构》线性表一章所有基本操作的实现,里面有你要的内容!
nanshi07 2011-09-11
  • 打赏
  • 举报
回复
请问一句代码,我没看懂:
type nodeptr=^nodeptr;
以上这句是什么意思?
adongshahua 2011-09-11
  • 打赏
  • 举报
回复
学习了..
nanshi07 2011-09-11
  • 打赏
  • 举报
回复
.。。。太长了。。要最简单的。。。。。
孤独小剑 2011-09-11
  • 打赏
  • 举报
回复
有个经典的通用链表实现方法,这个还可以扩展到树的实现。是linux内核中提供的一个链表的实现,很经典,推荐楼主可以看看。

#ifndef NMN_LIST_H
#define NMN_LIST_H

#include <stdio.h>

struct list_head {
struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}

static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}

static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}

static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}

static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
}

static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

#define list_entry(ptr, type, member) \
container_of(ptr, type, member)

#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)

#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))

#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))

#endif /* NMN_LIST_H */

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧