2016 7 25 Linked list

Source: Internet
Author: User
Tags define null

#include <stdio.h>
#include <stdlib.h>

/*
Usingnamespacestd;
&NBSP
Structnode
{
  int  data;//data field
  struct node*next;//pointer field
};
 
/*
Create
* Function: Creates a linked list.
* Input: Data
* return value for each node: pointer head
*//*
Node*create ()
{
  int  n=0;
  NODE*HEAD,*P1,*P2;
  P1=p2=new Node;
  cin>>p1->data;
  Head=null;
while (p1->data!=0)
{
    if (n==0)
   {
     head= P1;
  }
   Else
    p2->next=p1;
    P2=p1;
    p1=new Node;
    cin>>p1->data;
    n++;
}
    p2->next=null;
    return head;
}
 
/*
Insert
* Function function: Inserts an element into a linked list.
* Input: Head link header pointer, p new element insert position, x data field contents in new element
* return value: None

Void Insert (Node*head,int p,int x)
{
   node*tmp=head;//for loop is to prevent the insertion position from exceeding the list length
   for (inti=0;i<p;i++)
   {
     if (tmp==null)
     return-1;
     if (i<p-1)
     tmp=tmp->next;
   
    node*tmp2=new Node;
      tmp2->data=x;
      tmp2->next=tmp->next;
      tmp->next=tmp2;
}
 */
/*
del
* function function: delete element in list
* Input: Head link header pointer, p deleted element position
* Return value: The data field in the deleted element. If delete fails back-1

Intdel (Node*head,int p)
{
   node*tmp=head;
   for (inti=0;i<p;i++)
   {
      if (tmp==null)
       return-1;
      if (i<p-1)
        tmp=tmp->next;
}
   int ret=tmp->next->data;
   tmp->next=tmp->next->next;
   return ret;
}
 
void print (Node*head)
{
    for (node*tmp=head;tmp!=null;tmp=tmp->next )
      printf ("%d", tmp->data);
      printf ("\ n");
}
 
Int main ()
{
  node*head;
  Head=newnode;
  head->data=-1;
  head->next=null;
  return 0;
}
*/
//Example
#include <iostream>
//#define NULL 0
struct student
{
   long num ;
   struct Student*next;
};

int main ()
{
int i,n;
student*p= (struct student*) malloc (sizeof (struct student));
Student*q=p;
printf ("Enter several values");
scanf ("%d", &n);
for (i=1;i<=n;i++)
{
scanf ("%d",& (q->num));
q->next= (struct student*) malloc (sizeof (struct student));
q=q->next;
}
printf ("Value of the first few");
int rank;
scanf ("%d%d",& (q->num), &rank);
Student*w=p;
for (i=1;i<rank-1;i++)
{
w=w->next;
}
q->next=w->next;
w->next=q;
for (i=1;i<=n+1;i++)
{
printf ("%d", p->num);
p=p->next;
}
return 0;
}//pointer back trouble list form loop list */

2016 7 25 Linked list

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.