1.请问这个链表主函数怎么写
哥们你这是数据结构上的吧。
学数据结构要灵活。
这里给你一个建立链表的程序:(以SDUTOJ 2116题为例题) #include typedef struct test { int a; struct test *next; }Test; typedef struct test1 { int a; struct test1 *next; struct test1 *front; }Test1;//单链表创建 Test *Create() { Test *head,*p1,*p2; head = p1 = p2 = NULL; p1 = (Test*)malloc(sizeof(Test)); scanf("%d",&p1->a); while(p1->a != 0) { if(head == NULL) { head = p1; } else { p2->next = p1; } p2 = p1; p1 = (Test*)malloc(sizeof(Test)); scanf("%d",&p1->a); } p2->next = NULL; return head; }//创建双向链表 Test1 *Create2() { Test1 *head,*p1,*p2; head = p1 = p2 = NULL; p1 = (Test1*)malloc(sizeof(Test1)); scanf("%d",&p1->a); p1->front = NULL; p1->next = NULL; while(p1->a != 0) { if(head == NULL) { head = p1; } else { p2->next = p1; p1->front = p2; } p2 = p1; p1 = (Test1*)malloc(sizeof(Test1)); scanf("%d",&p1->a); p1->front = NULL; p1->next = NULL; } p2->next = NULL; return head; }//只遍历一次单链表,找出中间元素 void SearchCenter(Test *head) { Test *p1,*p2; p1 = p2 = head; while(p2->next != NULL) { p2 = p2->next; if(p2->next != NULL) p2 = p2->next; p1 = p1->next; } printf("链表中间结点为:%d\n",p1->a); }//双向链表输出 void output1(Test1* head) { Test1 *p1,*p2; p1 = head; while(p1!= NULL) { printf("%d,",p1->a); p2 = p1; //p2用来保存最后一个结点 p1 = p1->next; } //从最后一个结点往前输出 while(p2 != NULL) { printf("%d,",p2->a); p2 = p2->front; } printf("\n"); } void output(Test* head) { Test *p1; p1 = head; while(p1 != NULL) { printf("%d,",p1->a); p1 = p1->next; } printf("\n"); }//单链表翻转//Next用来保存原链表中p结点的下一个结点 Test *reverse1(Test *head) { Test *p,*back,*Next; if(head == NULL || head->next == NULL) return head; back = head; p = back->next; back->next = NULL; while(p->next != NULL) { Next = p->next; p->next = back; back = p; p = Next; } p->next = back; head = p; return head; } void main() { Test *head = NULL; Test1 *head1 = NULL; head1 = Create2(); output1(head1); head = Create(); printf("初始链表为:\n"); output(head); SearchCenter(head); head = reverse1(head); output(head); } 可以参考这个的方法写。 下: (1)先创建一个新结点,并用指针p指向该结点。 (2)将q指向的结点的next域的值(即q的后继结点的指针)赋值给p指向结点的next域。 (3)将p的值赋值给q的next域。 通过以上3步就可以实现在链表中由指针q指向的结点后面插入p所指向的结点。可以通过图1-5形象地展示出这一过程。 图1-5 向链表插入结点过程 下面给出代码描述: 1.void insertList(LinkList *list,LinkList q,ElemType e) /*当链表为空时*/ 10. else 16.} 上面的这段代码描述了如何在指针q指向的结点后面插入结点的过程。其过程包括以下几步。 (1)首先生成一个新的结点,大小为sizeof(LNode),用LinkList类型的变量p指向该结点。将该结点的数据域赋值为e。 (2)接下来判断链表是否为空。如果链表为空,则将p赋值给list,p的next域的值置为空。否则,将q指向的结点的next域的值赋值给p指向结点的next域,这样p指向的结点就与q指向结点的下一个结点连接到了一起。 (3)然后再将p的值赋值给q所指结点的next域,这样就将p指向的结点插入到了指针q指向结点的后面。 其实通过上面这段算法描述可以看出,应用这个算法同样可以创建一个链表。这是因为当最开始时链表为空,即list==NULL,该算法可自动为链表创建一个结点。在下面的创建其他结点的过程中,只要始终将指针q指向链表的最后一个结点,就可以创建出一个 链表。 注意:函数insertList()的参数中有一个LinkList *list,它是指向LinkList类型的指针变量,相当于指向LNode类型的指针的指针。这是因为在函数中要对list,也就是表头指针进行修改,而调用该函数时,实参是&list,而不是list。因此必须采取指针参数传递的办法,否则无法在被调函数中修改主函数中定义的变量的内容。以下的代码也有类似的情况。 下:(1)先创建一个新结点,并用指针p指向该结点。 (2)将q指向的结点的next域的值(即q的后继结点的指针)赋值给p指向结点的next域。(3)将p的值赋值给q的next域。 通过以上3步就可以实现在链表中由指针q指向的结点后面插入p所指向的结点。可以通过图1-5形象地展示出这一过程。 图1-5 向链表插入结点过程 下面给出代码描述:1.void insertList(LinkList *list,LinkList q,ElemType e) /*当链表为空时*/ 10. else 16.} 上面的这段代码描述了如何在指针q指向的结点后面插入结点的过程。其过程包括以下几步。 (1)首先生成一个新的结点,大小为sizeof(LNode),用LinkList类型的变量p指向该结点。将该结点的数据域赋值为e。 (2)接下来判断链表是否为空。如果链表为空,则将p赋值给list,p的next域的值置为空。 否则,将q指向的结点的next域的值赋值给p指向结点的next域,这样p指向的结点就与q指向结点的下一个结点连接到了一起。(3)然后再将p的值赋值给q所指结点的next域,这样就将p指向的结点插入到了指针q指向结点的后面。 其实通过上面这段算法描述可以看出,应用这个算法同样可以创建一个链表。这是因为当最开始时链表为空,即list==NULL,该算法可自动为链表创建一个结点。 在下面的创建其他结点的过程中,只要始终将指针q指向链表的最后一个结点,就可以创建出一个 链表。注意:函数insertList()的参数中有一个LinkList *list,它是指向LinkList类型的指针变量,相当于指向LNode类型的指针的指针。 这是因为在函数中要对list,也就是表头指针进行修改,而调用该函数时,实参是&list,而不是list。因此必须采取指针参数传递的办法,否则无法在被调函数中修改主函数中定义的变量的内容。 以下的代码也有类似的情况。 #include const int NULL =0;template struct Node { T data; //数据域 struct Node * next; //指针域,在这里可省略};template class LinkList{public: LinkList(){first = new Node ; first->next = NULL;}//无参构造函数 LinkList(T a [], int n);//有参构造函数,使用含有n个元素的数组a初始化单链表 ~LinkList(); //析构函数 int GetLength(); //获取线性表的长度 Node * Get(int i); //获取线性表第i个位置上的元素结点地址 int Locate (T x); //查找线性表中值为x的元素,找到后返回其位置 void Insert (int i, T x);//在线性表的第i个位置上插入值为x的新元素 T Delete(int i); //删除线性表第i个元素,并将该元素返回 void PrintList(); //按次序遍历线性表中的各个数据元素 Node * GetFirst(){return first;}private: Node * first; //头指针};#include "linklist.h"#include "iostream"using namespace std;/*template LinkList::LinkList(T a [], int n) //头插法建立单链表{ first = new Node ; first ->next = NULL; //构造空单链表 for (int i=n-1;i>=0;i--) { Node * s = new Node ;//①建立新结点 s->data = a[i]; //②将a[i]写入到新结点的数据域 s->next = first->next; //③修改新结点的指针域 first->next = s; //④修改头结点的指针域,将新结点加入到链表中 }}*/template LinkList::LinkList(T a [], int n) //尾插法建立单链表/*① 在堆中建立新结点:Node * s = new Node ;② 将a[i]写入到新结点的数据域:s->data = a[i];③ 将新结点加入到链表中: r->next = s;④ 修改尾指针:r = s;*/{ first = new Node ; Node * r = first; for (int i=0;i * s = new Node ;//①建立新结点 s->data = a[i]; //②将a[i]写入到新结点的数据域 r->next = s; //③将新结点加入到链表中 r = s; //④修改尾指针 } r->next = NULL; //终端结点的指针域设为空}template LinkList::~LinkList() //析构函数{ Node * p = first; //初始化工作指针p while (p) //要释放的结点存在 { first = p; //暂存要释放的结点 p = p -> next; //移动工作指针 delete first; //释放结点 }}template Node * LinkList::Get(int i) //获取线性表第i个位置上的元素{ Node * p = first -> next; //初始化工作指针 int j = 1; //初始化计数器 while ( p && j != i ) //两个条件都满足,则继续循环 { p = p -> next; //工作指针后移 j++; } if (!p) throw "查找位置非法"; //工作指针已经为空 else return p; //查找到第i个元素}template int LinkList::Locate (T x) //查找线性表中值为x的元素,找到后返回其位置{ Node * p = first ->next; //初始化工作指针 int j=1; while (p) { if (p->data == x) return j; //找到被查元素,返回位置 p = p->next; j++; } return -1; //若找不到,返回错误标识-1}template void LinkList::Insert (int i, T x)//在线性表的第i个位置上插入值为x的新元素{ Node * p = first; //初始化工作指针 if (i != 1) p = Get(i-1); //若不是在第一个位置插入,得到第i-1个元素的地址。 Node * s = new Node ;//建立新结点 s->data = x; s->next = p->next; p->next = s; //将新结点插入到p所指结点的后面}template T LinkList::Delete(int i) //删除线性表第i个元素,并将该元素返回{ Node * p = first; //初始化工作指针 if (i != 1) p = Get(i-1); //若不是在第一个位置插入,得到第i-1个元素的地址。 Node * q = p->next; p->next = q->next; T x = q->data; delete q; return x;}template void LinkList::PrintList() //按次序遍历线性表中的各个数据元素{ Node * p = first->next ; //初始化工作指针 while (p){ cout data next; }}。 #include #include struct node { int data; struct node *next; }; struct node *createlist(); /*声明函数*/ struct node *createlist() /*函数体,返回struct node *类型的头结点*/ { int x; struct node *h,*s,*r; h=(struct node *)malloc(sizeof(struct node)); r=h; scanf("%d",&x); while(x!=-1) { s=(struct node *)malloc(sizeof(struct node)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; return h; /*返回头结点*/ } void main() { struct node *head; head=createlist(); /*获取函数的返回值,也就是头结点*/ } /*请输入待建立的表长 : 5请输入5个元素用空格分开 : 56 54 34 12 76成功建立表!插入元素10。 56 10 54 34 12 76删除第3个元素。56 10 34 12 76Press any key to continue*/#include using namespace std;#define MaxSize 100typedef int datatype;typedef struct { datatype data[MaxSize]; int last;}SeqList;void Init_SeqList(SeqList*L) { int i; cout > L->last; cout last last;i++) { cin >> L->data[i]; } cout last == MaxSize - 1) { cout L->last + 2)) { cout last;j >= i - 1;j--) L->data[j + 1] = L->data[j]; L->data[i - 1] = x; L->last++; return 1;}int Delete_SeqList(SeqList *L,int i) { int j; if((i L->last + 1)) { cout last;j++) L->data[j - 1] = L->data[j]; L->last--; return 1;}int Locate_SeqList(SeqList *L,datatype x) { int i = 0; while((i last) && (L->data[i] != x)) i++; if(i >= L->last) return -1; else return 0;}void Display_SeqList(SeqList *L) { if(L == NULL) cout last;i++) printf("%d ",L->data[i]); cout 追问: 运行不了啊。 追答: 代码上边的/* 。 . */中的内容是在VC下的运行结果,应该可以的。 评论0 0 0。2.链表的各函数在主函数中怎么联系起来,就是写的子函数在主函数中要
3.单链表,求最大值的算法中的主函数应该怎么写
4.单链表,求最大值的算法中的主函数应该怎么写
5.c++数据结构中链表的主函数中代码实现怎么写
6.C语言:链表的常用操作,完成下列子函数,并写主函数调用
7.如何建立单向链表函数(写的出主函数,但不懂得如何定义成函数)
8.线性表的主函数要怎么写