单链表及系列操作

单链表
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct Node
{
   int data;
   struct Node *next;
}Node, *LinkList;
void input1(LinkList *head)
{
   LinkList p,r;
   int x;
   cin>>x;
   *head=(LinkList)malloc(sizeof(Node));
   r=*head;
   while(x!=-1)
   {
      p=(Node *)malloc(sizeof(Node));
      p->data=x;
      r->next=p;
      r=p;
      cin>>x;
   }
   r->next=NULL;
}
void input2(LinkList *head)
{
   LinkList p;
   *head=(LinkList)malloc(sizeof(Node));
   (*head)->next=NULL;
   int x;
   cin>>x;
   while(x!=-1)
   {
      p=(LinkList)malloc(sizeof(Node));
      p->data=x;
      p->next=(*head)->next;
      (*head)->next=p;
      cin>>x;
   }
}
void print(LinkList head)
{
   LinkList p;
   p=head;
   int cnt=1;
   while(p->next!=NULL)
   {
      cout  << p->next->data << ' ';
      if(cnt%5==0)
         cout << endl;
      p=p->next;
      cnt++;
   }
   cout << endl;
}
void printsum(LinkList head)
{
   LinkList p;
   p=head;
   int sum=0;
   while(p->next!=NULL)
   {
      sum+=p->next->data;
      p=p->next;
   }
   cout << sum << endl;
}
int find(LinkList head,int pos)
{
   LinkList p;
   p=head->next;
   int j=1;
   while(p!=NULL&&j<pos)
   {
      p=p->next;
      j++;
   }
   return p->data;
}
void inser(LinkList *head,int pos,int e)
{
   LinkList p,s;
   int j=1;
   p=*head;
   while(p&&j<pos)
   {
      p=p->next;
      j++;
   }
   s=(LinkList)malloc(sizeof(Node));
   s->data=e;
   s->next=p->next;
   p->next=s;
}
void dele(LinkList *head,int pos)
{
   LinkList p,q;
   int j=1;
   p=*head;
   while(p->next!=NULL&&j<pos)
   {
      p=p->next;
      j++;
   }
   q=p->next;
   p->next=q->next;
   free(q);
}
void clearr(LinkList *head)
{
   LinkList p,q;
   p=(*head)->next;
   while(p)
   {
      q=p->next;
      free(p);
      p=q;
   }
   (*head)->next=NULL;
}
int main()
{
    LinkList head=NULL;
    input1(&head);
    print(head);
    printsum(head);
    cout << find(head,2) << endl;
    inser(&head,1,10);
    print(head);
    dele(&head,1);
    print(head);
    return 0;
}

#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct Node
{
   int data;
   struct Node *next;
}Node;
Node *initLinkList()
{
   Node *head;
   head=(Node *)malloc(sizeof(Node));
   head->next=NULL;
   return head;
}
void CreatLinkList(Node *head)
{
   Node *p,*r;
   int x;
   cin>>x;
   r=head;
   while(x!=-1)
   {
      p=(Node *)malloc(sizeof(Node));
      p->data=x;
      p->next=NULL;
      r->next=p;
      r=p;
      cin>>x;
   }
}
void printLinkList(Node *head)
{
   Node *p;
   p=head->next;
   while(p!=NULL)
   {
      cout << p->data << ' ';
      p=p->next;
   }
   cout << endl;
}
void insertLinkList(Node *head,int x)
{
   Node *pre,*r,*q;
   q=(Node *)malloc(sizeof(Node));
   q->data=x;
   q->next=NULL;
   pre=head;
   r=head->next;
   while(r->data<x&&r!=NULL)
   {
      pre=r;
      r=r->next;
   }
   pre->next=q;
   q->next=r;
}
void deleteLinkList(Node *head)
{
   Node *p,*q;
   p=head->next;
   while(p!=NULL)
   {
      q=p;
      p=p->next;
      free(q);
   }
   head->next=NULL;
}
Node *findLinkList(Node *head,int x)
{
   Node *p;
   p=head->next;
   while(p!=NULL)
   {
      if(p->data==x)
         return p;
      p=p->next;
   }
   return NULL;
}
int main()
{
    Node h; h.next=NULL;
    //Node *h;h=initLinkList();
    CreatLinkList(&h);
    insertLinkList(&h,6);
    //deleteLinkList(&h);
    printLinkList(&h);
    return 0;
}
#include<cstdio>
#include<cstdlib>
#include<iostream>

using namespace std;
typedef struct Node
{
   int data;
   struct Node *next;
}Node;
void creatLinkList(Node *h)
{
   Node *p,*r;
   int x;
   cin>>x;
   r=h;
   while(x!=-1)
   {
      p=(Node *)malloc(sizeof(Node));
      p->data=x;
      p->next=NULL;
      r->next=p;
      r=r->next;
      cin>>x;
   }
}
void printLinkList(Node *h)
{
   Node *p;
   p=h->next;
   while(p!=NULL)
   {
      cout << p->data << ' ';
      p=p->next;
   }
   cout << endl;
}
void deletesdata(Node *h,int x)//删除某一元素
{
   Node *p,*pre,*c;
   pre=h;
   p=h->next;
   while(p!=NULL)
   {
      if(p->data==x)
      {
         c=p;
         pre->next=p->next;
         p=p->next;
         free(c);
      }
      else
      {
         pre=p;
         p=p->next;
      }
   }
}
void deleteparddata(Node *h)//删除重复元素
{
   Node *p;
   p=h->next;
   while(p!=NULL)
   {
      deletesdata(p,p->data);
      p=p->next;
   }
}
void ten_to_eight(Node *h,int n)//十进制转八进制
{
   Node *p,*r;
   r=h;
   while(n!=0)
   {
      int t;
      t=n%8;
      p=(Node *)malloc(sizeof(Node));
      p->data=t;
      p->next=NULL;
      p->next=r->next;
      r->next=p;
      n/=8;
   }
}
void mergedata(Node *h1,Node *h2,Node *h3)//链表的合并
{
   Node *a,*b,*c,*p,*r;
   a=h1->next;
   b=h2->next;
   r=h3;
   while(a!=NULL)
   {
      r->next=a;
      r=r->next;
      a=a->next;
   }
   while(b!=NULL)
   {
      int flag=0;
      p=h3->next;
      while(p!=NULL)
      {
         if(b->data==p->data)
         {
            flag=1;
            break;
         }
         p=p->next;
      }
      if(flag==0)
      {
         b->next=NULL;
         r->next=b;
         r=r->next;
      }
      b=b->next;
   }
}

int main()
{
   Node h;
   h.next=NULL;
   //int n;
   //creatLinkList(&h);
   //deleteparddata(&h);
   //printLinkList(&h);
   //cin>>n;
   //ten_to_eight(&h,n);
   /*Node h1,h2,h3;
   h1.next=NULL;
   h2.next=NULL;
   h3.next=NULL;

   creatLinkList(&h1);
   creatLinkList(&h2);
   mergedata(&h1,&h2,&h3);*/
   creatLinkList(&h);
   printLinkList(&h);
   return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值