C++链表基本操作 下载本文

内容发布更新时间 : 2024/5/17 17:47:35星期一 下面是文章的全部内容请认真阅读。

A.outputlist();

B.insertlist(0,Data[0]); //建立链表B首结点 for(i=0;i<10;i++)

B.insertlist(B.gethead()->Data,Data[i]); //在首结点处顺序向后插入 cout<<\链表B:\B.outputlist(); B.deletelist(67); cout<<\删除元素67后\B.outputlist(); }

程序运行结果为

链表A;25,41,16,98,5,67,9,55,1,121 删除元素Data[7]后;

25,41,16,98,5,67,9,1,121

链表B;121,1,55,9,67,5,98,16,41,25, 删除元素67后;

121,1,55,9,5,98,16,41,25, 下面是杨辉三角的代码: #include #include using namespace std; int main() {

const int n=11; int i,j,a[n][n]; for(i=1;i

a[i][i]=1; a[i][1]=1; }

for(i=3;i

for(j=2;j<=i-1;j++)

a[i][j]=a[i-1][j-1]+a[i-1][j]; }

for(i=1;i

for(j=1;j<=i;j++)

cout<

cout<

#include #include struct Node {

int num ; Node *next ; };

Node* Create() //链表创建 {

int n=0;

Node *p1,*p2,*head; p1=p2=new Node; cin>>p1->num; head=NULL;

while (p1->num!=0) {

if (n==1) {

head=p1; } else

p2->next=p1; p2=p1;

p1=new Node; cin>>p1->num; n++; }

p2->next=NULL;

return head; }

int ListLength(Node L) //链表的计数 {

Node p=L; int count=0; while(p->next) {

count++; p=p->next; }

return count; }

int Search(Node &L , int value) //链表的查找 {

Node p=L; int index=0; while(p) {

if(p->num== value) return index; p=p->next;

index++; }

return 0; }

void Print(Node *head) //输出链表 {

Node* p=head; while (p) {

cout<num<<\; p=p->next; }

cout<

void Destruct(Node *head) //清空链表 {

Node *current = head, *temp = NULL; while (current) {

temp = current;

current = current ->next; delete temp; } }

Node *ReverseList(Node *head) //链表逆序(循环方法) {

Node *p,*q,*r;

p=head; //一开始p指向第一个节点 q=p->next; //q指向第二个节点 while (q!=NULL) //如果没到链尾 { //以第一次循环为例

r=q->next; //r暂时存储第三个节点

q->next=p; //没执行此句前,q->next指向第三个节点 //执行之后,q->next指向第一个节点p p=q; //之后p指向第二个节点 q=r; //q指向第三个节点

//即...p=>q=>r...变为 ...p<=q<=r... }

head->next=NULL; //最后原来的链头变为链尾,把它指向NULL。 head=p; //原来的链尾变成链头 return head; }

Node *ReverseList2(Node *head) //链表逆序(递归方法) {

if (!head) {

return NULL; }

Node *temp = ReverseList2 (head->next); if (!temp) {

return head; }

head->next->next = head; head->next = NULL; return temp; }

递归时,head可以分别用 head ,head1,head2 ...headn-1, headn来表示总共n+1个节点 temp = ReverseList2( head->next ); 此句的递归一直将参数传进来的。 Node* head 递归到 headn 然后判断下列语句: else if( !headn->next ) {

return headn; }

将返回值传给temp,此时temp指向链尾 ,由于在此次返回,故此次没有执行最后的else的那部分的语句,返回上一级即是 headn-1 那一级,继续执行 下面的 headn-1->next->next = headn-1;

headn-1->next = NULL; //此两句将最后两个逆序连接, return temp;

//之后返回temp比上一层的temp即是执行temp = ReverseList2( head->next )赋值,因为递归的口都是在这里的,如果说好理解点也可以将temp来编号 同理

在返回temp后,继续执行

headn-2->next->next = headn-2; headn-2->next = NULL; return temp; .....

一直到 head时 即是原链表的第一个节点,在对其head->next = NULL后,此时以 temp 所指向的节点为链头的逆序链表就形成了.

//已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(循环方法) //(保留所有结点,即便大小相同)

Node *Merge(Node *head1 , Node *head2) {

if ( head1 == NULL) return head2 ; if ( head2 == NULL) return head1 ;

if ( head1->num < =head2->num ) {

head = head1 ; head1= head1->next; } else {

head = head2 ;

head2 = head2->next ; }

Node *temp = head ;

while ( head1 != NULL && head2 != NULL) {

if ( head1->num <= head2->num ) {

temp->next = head1 ; head1 = head1->next ;

temp =temp->next;

} else {

temp->next = head2; head2 = head2->next ;

temp =temp->next;

} }

if (head1 == NULL) temp->next = head2; if (head2 == NULL) temp->next = head1; return head; }