1. 程式人生 > >單鏈表及系列操作

單鏈表及系列操作

#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;
}