Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- class list
- {
- private:
- struct node
- {
- int data;
- node *next;
- };
- public:
- node *head, *tail;
- list()
- {
- head=NULL;
- tail=NULL;
- }
- void displaylist()
- {
- node *temp=new node;
- temp=head;
- while(temp!=NULL)
- {
- printf("%d\n",temp->data);
- temp=temp->next;
- }
- }
- void createnode(int value)
- {
- node *temp=new node;
- temp->data=value;
- temp->next=NULL;
- if(head==NULL)
- {
- head=temp;
- tail=temp;
- temp=NULL;
- }
- else
- {
- tail->next=temp;
- tail=temp;
- }
- }
- int size()
- {
- int count=0;
- node *temp=new node;
- temp=head;
- while(temp!=NULL)
- {
- count=count+1;
- temp=temp->next;
- }
- return count;
- }
- void insert_position(int pos, int value)
- {
- node *pre=new node;
- node *cur=new node;
- node *temp=new node;
- cur=head;
- printf("Head is %d\n", head->data);
- for(int i=1;i<pos;i++)
- {
- pre=cur;
- cur=cur->next;
- if(cur != NULL)
- printf("next is %d\n", cur->data);
- }
- temp->data=value;
- pre->next=temp;
- temp->next=cur;
- if (pos == 1)
- head = temp;
- printf("new head is %d\n", head->data);
- }
- void display(int pos)
- {
- node *temp=new node;
- temp=head;
- for (int i=1; i<pos; i++)
- {
- temp=temp->next;
- }
- printf("%d",temp->data);
- }
- void delete_position(int pos)
- {
- node *current=new node;
- node *previous=new node;
- node *axr=new node;
- current=head;
- if (pos==1)
- {
- head=head->next;
- delete current;
- return;
- }
- for(int i=1;i<pos;i++)
- {
- previous=current;
- current=current->next;
- }
- axr=current;
- previous->next=current->next;
- delete axr;
- }
- void insert_start(int value)
- {
- node *temp=new node;
- temp->data=value;
- temp->next=head;
- head=temp;
- }
- };
- int main()
- {
- int k,x,m,i,n,s;
- list mylist;
- scanf("%d",&n);
- scanf("%d",&k);
- scanf("%d",&k);
- mylist.insert_start(k);
- printf("HEAD %d\n", mylist.head->data);
- for (i=2; i<=n; i++)
- {
- scanf("%d",&k);
- scanf("%d",&x);
- printf("okkkk\n");
- mylist.insert_position(k,x);
- }
- scanf("%d",&m);
- for (i=1; i<=m; i++)
- {
- scanf("%d",&k);
- mylist.delete_position(k);
- }
- scanf("%d",&k);
- s=mylist.size();
- printf("%d ",s);
- mylist.display(k);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement