linkedlist

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
38
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
using namespace std;

struct LinkedList{
    int value;
    struct LinkedList *next;
};
typedef struct LinkedList node;
node *head = NULL;

void add_end(int val) {
    node *newnode, *temp;
    newnode = (node *) malloc(sizeof(node));
    newnode->value = val;
    newnode->next = NULL;

    if(head==NULL) {
        head = newnode;
        return;
    }

    temp = head;
    while(temp->next!=NULL)
        temp = temp->next;
    temp->next = newnode;

}

void add_begin(int val)
{
    node *temp;
    temp = (node*)malloc(sizeof(node));
    temp->value = val;
    temp->next = head;
    head = temp;
}

void insert_after(int pos_val, int val)
{
    node *temp, *temp2, *newnode;
    newnode = (node*)malloc(sizeof(node));
    newnode->value = val;

    temp = head;
    int i=0;
    while(i<pos_val) {
        temp = temp->next;
        temp2 = temp->next;
        i++;
    }
    temp->next=newnode;
    newnode->next=temp2;

}

void delete_begin()
{
    node *temp;
    temp=head;
    head=head->next;
    free(temp);
}

void delete_end()
{
    node *temp, *pre_node;
    temp=head;
    while(temp->next!=NULL) {
        pre_node=temp;
        temp=temp->next;
    }
    pre_node->next = NULL;
    free(temp);
}

void delete_pos(int pos)
{
    node *
}


void display()
{
    node *temp;
    temp=head;
    while(temp!=NULL) {
        printf("%d ", temp->value);
        temp = temp->next;
    }
    printf("\n");
}


int main()
{
    node *temp;
    temp = (node *)malloc(sizeof(node));
    temp->value = 10;
    temp->next = NULL;
    head = temp;

    add_end(20);
    add_end(30);
    add_end(40);
    add_end(50);
    display();

    add_begin(5);
    display();

    add_begin(-5);
    display();

    insert_after(2, 100);
    display();

    insert_after(6, 200);
    display();

    add_end(250);
    display();

    delete_begin();
    display();
    delete_begin();
    display();

    delete_end();
    display();

    delete_end();
    display();
    

}

/**

char Stack[100];
int top=-1;

void Push(char x)
{
    top++;
    Stack[top]=x;
}

char Pop()
{
    char x = Stack[top];
    top--;
    return x;
}


int main()
{
    char str[100];
    gets(str);
    int i=0;
    while(str[i]!='\0')
    {
        if(str[i]=='(')
        {
            Push('(');
            i++;
            continue;
        }
        if(str[i]==')' && Stack[top]=='(') {
            Pop();
            i++;
        }
        else{
            printf("Invalid");
            return 0;
        }
    }
    if(top==-1)
        printf("Valid");
    else {
        printf("Invalid");
    }


}
*/