Bidirectional linked list Insert Delete Basic application Introduction _c language

Source: Internet
Author: User
Double linked list In fact, there's nothing more than a front chain.
Definition of double linked list
Copy Code code as follows:

struct Dnode
{
int data;
struct Dnode *next;
struct Dnode *pre;
};

Definition of a single linked list
Copy Code code as follows:

View Plaincopy
struct Dnode
{
int data;
struct Dnode *next;
};

Others can be like a blog about the same
Copy Code code as follows:

#ifndef Head_h
#define Head_h
#include <iostream>
using namespace Std;
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <fstream>
#include <string>
#include <algorithm>
#include <list>
#include <queue>
#include <vector>
#include <deque>
#include <stack>
#include <bitset>
#include <set>
#include <map>
#endif
struct Dnode
{
int data;
struct Dnode *next;
struct Dnode *pre;
};
Dnode *creat ()
{
Dnode *head,*p,*s;
Head= (Dnode *) malloc (sizeof (Dnode));
P=head;
int temp;
while (cin>>temp&&temp)
{
s= (Dnode *) malloc (sizeof (Dnode));
s->data=temp;
p->next=s;
s->pre=p;
P=s;
}
head=head->next;
p->next=null;
head->pre=null;
return (head);
}
Dnode *insert (dnode *&head,int num)
{
Dnode *p,*s;
s= (Dnode *) malloc (sizeof (Dnode));
s->data=num;
P=head;
while (Null!=p->next&&num>p->data)
{
p=p->next;
}
if (Num<=p->data)
{
if (Null==p->pre)
{
s->next=head;
head->pre=s;
Head=s;
head->pre=null;
}
Else
{
s->pre=p->pre;
p->pre->next=s;
s->next=p;
p->pre=s;
}
}
Else
{
p->next=s;
s->pre=p;
s->next=null;
}
return (head);
}
Dnode *del (dnode *&head,int num)
{
Dnode *p;
P=head;
while (Null!=p->next&&num!=p->data)
{
p=p->next;
}
if (Num==p->data)
{
if (Null==p->pre)
{
head=p->next;
p->next->pre=head;
Free (p);
}
else if (null==p->next)
{
p->pre->next=null;
Free (p);
}
Else
{
p->pre->next=p->next;
p->next->pre=p->pre;
Free (p);
}
}
Else
{
cout<<num<< "Cound not to be found" <<endl;
}
return head;
}
void Display (Dnode *head)
{
Dnode *p;
P=head;
while (NULL!=P)
{
cout<< (p->data) << "";
p=p->next;
}
cout<<endl;
}

Copy Code code as follows:

#include "head.h"
int main ()
{
Dnode *head;
Head=creat ();
Display (head);
#ifndef DEBUG
cout<< "Please input a num to insert:";
#endif
int insert_num;
while (cin>>insert_num&&insert_num)
{
Insert (head,insert_num);
Display (head);
}
#ifndef DEBUG
cout<< "Please input a number to delete:";
#endif
int delete_num;
while (cin>>delete_num&&delete_num)
{
Del (head,delete_num);
Display (head);
}
Return (exit_success);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.